C++ program to illustrate Hierarchical Inheritance











//base class is Figure
//classes Square and Circle are derived from Figure
//to calculate Area

#include <iostream>
const float PI=3.1416;
using namespace std;
class Figure //base class
{
    protected:
        float dim1;
    public:
        void read()
        {
            cin>>dim1;
        }
};

class Square:public Figure //derived class
{
    public:
        float Area()
        {
            return dim1*dim1;
        }
};

class Circle:public Figure //derived class
{
    public:
        float Area()
        {
            return PI*dim1*dim1;
        }
};

int main()
{
    Square s;
    cout<<"For square, enter length: ";
    s.read();
    Circle c;
    cout<<"For circle, enter radius: ";
    c.read();
    cout<<endl<<"The area of square is "<<s.Area();
    cout<<endl<<"The area of circle is "<<c.Area();
    return 0;
}

No comments:

Post a Comment