Following program demonstrates the parent class provides an interface to the base class to implement a function called area():
#include <iostream>
using namespace std;
class Shape // Base class
{
public:
virtual float area() = 0; // Pure Virtual Function
float radious(float r)
{
ar = r;
}
float SetLength(float l)
{
length = l;
}
float SetBreadth(float b)
{
breadth=b;
}
float SetSideSquare(float s)
{
side=s;
}
protected:
float ar;
float length;
float breadth;
float side;
};
class Circle: public Shape // Derived class
{
public:
float area()
{
return (3.14*ar*ar);
}
};
class Rectangle: public Shape //Derived class
{
public:
float area()
{
return (length*breadth);
}
};
class SideSquare: public Shape //Derived class
{
public:
float area()
{
return (side*side);
}
};
int main()
{
Circle cr;
cr.radious(3);
cout << "Area of Circle : " << cr.area() << endl; // Print the area of circle.
Rectangle rect;
rect.SetLength(3);
rect.SetBreadth(6);
cout << "Area of Rectangle : " << rect.area() << endl; // Print the area of rectangle.
SideSquare sq;
sq.SetSideSquare(2);
cout << "Area of Square : " << sq.area() << endl; // Print the area of Square.
return 0;
}