C++ program to implement Run Time Polymorphism with Pure Virtual Function

//base class Shape with dim as data member and constructor to initialize its data
//derived classes Square and Circle to FindPerimeter() as member functions
//dim as side for Square and dim as radius for Circle
//Implement Run Time Polymorphism

#include <iostream>
using namespace std;
class Shape
{
    protected:
        float dim;
    public:
        Shape()
        {
            dim=0;
        }
        Shape(float d)
        {
            dim=d;
        }
        virtual void show()=0;
};
class Square:public Shape
{
    public:
        Square(float d):Shape(d)
        {
        }
        void show()
        {
            cout<<"Area of Square is "<<4*dim<<endl;
        }
};
class Circle:public Shape
{

    public:
        Circle(float d):Shape(d)
        {
        }
        void show()
        {
            cout<<"Area of Circle is "<<2*3.1416*dim<<endl;
        }
};
int main()
{
    Square s(5);
    Circle c(7);
    Shape *sh;
    sh=&s;
    sh->show();
    sh=&c;
    sh->show();
    return 0;
}

No comments:

Post a Comment