// virtual members #include using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } // A member of a class that can be redefined in its derived classes is known as // a virtual member. In order to declare a member of a class as virtual, // we must precede its declaration with the keyword virtual. 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 *poly1 = ▭ Polygon *poly2 = &trgl; Polygon *poly3 = &poly; poly1->set_values (4,5); poly2->set_values (4,5); poly3->set_values (4,5); cout << "The area of rectangel is " << poly1->area() << endl; cout << "The area of triangle is " << poly2->area() << endl; cout << "The area of polygon is " << poly3->area() << endl; getchar(); return 0; }