In order to declare an element of a class which we are going to redefine in derived classes we must precede it with the keyword virtual so that the use of pointers to objects of that class can be suitable.
Take a look at the following example:
// virtual members
#include <iostream.h>
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b) { width =a; height = b; }
virtual int area(void) { return (0); }
};
class CRectangle: public CPolygon {
public:
int area(void) { return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area(void) { return (width * height / 2); }
};
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);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
cout << ppoly3->area() << endl;
return 0;
}
Result--------------------------------------
20
10
0
-------------------------------------------
Now the three classes (CPolygon, CRectangle and CTriangle) have the same members: width, height, set_values() and area().
area() has been defined as virtual because it is later redefined in derived classes. You can verify if you want that if you remove this word (virtual) from the code and then you execute the program the result will be 0 for the three polygons instead of 20, 10, 0. That is because instead of calling the corresponding area(0 function for each object (CRectangle::area(), CTriangle::area() and CPolygon::area(), respectively), CPolygon::area() will be called for all of them since the calls are via a pointer to CPolygon.
Therefore what the word virtual does is to allow that a member of a derived class with the same name as one in the base class be suitably called when a pointer to it is used, as in the above example.
Note that in spite of its virtuality we have also been able to declare an object of type CPolygon and to call its area() function, that always returns 0 as the result.
From. The cplusplus.com tutorial
Take a look at the following example:
// virtual members
#include <iostream.h>
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b) { width =a; height = b; }
virtual int area(void) { return (0); }
};
class CRectangle: public CPolygon {
public:
int area(void) { return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area(void) { return (width * height / 2); }
};
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);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
cout << ppoly3->area() << endl;
return 0;
}
Result--------------------------------------
20
10
0
-------------------------------------------
Now the three classes (CPolygon, CRectangle and CTriangle) have the same members: width, height, set_values() and area().
area() has been defined as virtual because it is later redefined in derived classes. You can verify if you want that if you remove this word (virtual) from the code and then you execute the program the result will be 0 for the three polygons instead of 20, 10, 0. That is because instead of calling the corresponding area(0 function for each object (CRectangle::area(), CTriangle::area() and CPolygon::area(), respectively), CPolygon::area() will be called for all of them since the calls are via a pointer to CPolygon.
Therefore what the word virtual does is to allow that a member of a derived class with the same name as one in the base class be suitably called when a pointer to it is used, as in the above example.
Note that in spite of its virtuality we have also been able to declare an object of type CPolygon and to call its area() function, that always returns 0 as the result.
From. The cplusplus.com tutorial