10 Lecture Ten
10 Lecture Ten
Polymorphism
2
3
4
Pointers to Base
Class
// pointers to base class
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{width=a; height=b;}
};
5
Pointers to Base
Class
class CTriangle: public CPolygon 20
{ 10
public:
int area (){return (width*height/2);}
};
int main ()
{
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
6
Pointers to Base
Class
• In the previous example, we assign references to rect and trgl
to two pointers (ppoly1 and ppoly2) of type CPolygon, and
because both are objects of classes derived from CPolygon,
both are valid assignment operations.
9
Virtual Members
// virtual members
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{width=a; height=b;}
virtual int area (){return (0);}
};
10
Virtual Members
class CTriangle: public CPolygon
{ 20
public: 10
int area (){return(width*height/2);}
}; 0
int main ()
{
CRectangle rect;
CTriangle trgl;
CPolygon poly;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
CPolygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
cout << ppoly3->area() << endl;
return 0;
}
11
Virtual Members
13
Abstract Base
Classes
• We could leave that area() member function without
implementation at all in the base class. This is done
by appending =0 (equal to zero) to the function
declaration.
// abstract class CPolygon
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area () =0;
}; 14
Abstract Base
Classes
• Notice how we appended =0 to virtual int area ()
instead of specifying an implementation for the
function.
16
Abstract Base
Classes
• A class that cannot instantiate objects is not totally useless. We
can create pointers to it and take advantage of all its
polymorphic abilities.
CPolygon poly; // not valid for the abstract base class
17
Abstract Base
Classes
// abstract base class
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{width=a; height=b;}
virtual int area (void) =0;
};
18
Abstract Base
Classes
class CTriangle: public CPolygon
{ 20
public: 10
int area (void){return (width*height/2);}
};
int main ()
{
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
return 0;
}
19
Polymorphism
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{width=a; height=b;}
virtual int area (void) =0;
void printarea (void)
{cout<<this->area()<<endl;}
};
int main ()
{
CPolygon * ppoly1 = new CRectangle;
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
22
23