Doubt in polymorphism

I have seen from a website that if we are two derived classes inherited from one base class and if there is function in base class that we want to call for both the derived classes, then we have to use polymorphism. We have to take the reference to the class objects to the pointers and by writing pointer name and function name wewill be able to call the function differently for both the derived classes. See the following example:

#include <iostream>
using namespace std;

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

class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};

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

int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = &rect;
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}


Now my question is that if the above example demonstrates the polymorphism then can't we call the set_value function by simly writing it as rect.set_value(4,5); and trgl.set_value(5,4);
And if we can call the function as above then what is the use of polymorphism? Please help. Thanx.
It should work fine. The triangles and rectangles will both inherit the set_values function, so calling them as you've typed (i.e. - rect.set_values(4,5)) will be fine.

You're not really using polymorphism here. You're using inheritance.

An example of polymorphism would be if CRectangle and CTriangle each had their own implementation of set_values and the implementation in CPolygon was virtual. Then, thanks to dynamic binding, the correct version of set_values would be called at run time.

If I haven't explained that well, I'm happy to provide an example.
Last edited on
Here only inheritance has been used, there is no polymorphism.
you can call it directly,
1
2
3
4
CPolygon * ppoly1 = &rect;
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);

but this code was meant to demonstrate a beautiful principle:
a parent/base class object can hold a reference to a child class object, but only those methods or variables which are in base class can be accessed using such parent objects.Its not properly brought out.
Your example demonstrates inheritance only, and not polymorphism. 'morph' means changing. You can achieve that with virtual functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CPolygon {
protected:
int width, height;
public:
virtual int area () = 0;
};

class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};

class CTriangle: public CPolygon {
public:
int area ()
{ return (width * height / 2); }
};
Now you can pass a pointer to CPolygon without knowin that it might be a CTriangle

can't we call the set_value function by simly writing it as rect.set_value(4,5); and trgl.set_value(5,4);
You always can. That has nothing to do with polymorphism.

Topic archived. No new replies allowed.