Polymorphism in c++ – Examples to understand Plymorphism in c++

Pointers to base class:

// pointers to base class
<dfn>#include <iostream></dfn>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};

class Rectangle: public Polygon {
public:
int area()
{ return width*height; }
};

class Triangle: public Polygon {
public:
int area()
{ return width*height/2; }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << <kbd>'\n'</kbd>;
cout << trgl.area() << <kbd>'\n'</kbd>;
return 0;
}

 

Virtual members: can be redefined in a derived class

// virtual members
<dfn>#include <iostream></dfn>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return 0; }
};

class Rectangle: public Polygon {
public:
int area ()
{ return width * height; }
};

class Triangle: public Polygon {
public:
int area ()
{ return (width * height / 2); }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon poly;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
Polygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << <kbd>'\n'</kbd>;
cout << ppoly2->area() << <kbd>'\n'</kbd>;
cout << ppoly3->area() << <kbd>'\n'</kbd>;
return 0;
}

Abstract base classes

// abstract class CPolygon
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area () =0;
};
// abstract base class
<dfn>#include <iostream></dfn>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0;
};

class Rectangle: public Polygon {
public:
int area (void)
{ return (width * height); }
};

class Triangle: public Polygon {
public:
int area (void)
{ return (width * height / 2); }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << <kbd>'\n'</kbd>;
cout << ppoly2->area() << <kbd>'\n'</kbd>;
return 0;
}

>In this example, objects of different but related types are referred to using a unique type of pointer (

Polygon*

) and the proper member function is called every time, just because they are virtual.>

// pure virtual members can be called
<cite>// from the abstract base class</cite>
<dfn>#include <iostream></dfn>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area() =0;
void printarea()
{ cout << this->area() << <kbd>'\n'</kbd>; }
};

class Rectangle: public Polygon {
public:
int area (void)
{ return (width * height); }
};

class Triangle: public Polygon {
public:
int area (void)
{ return (width * height / 2); }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}