Write a Program to perform Pointer to derived Class

/*
    Pointer to derived Class
*/
#include<iostream>
using namespace std;

class Polygon
{
    protected:
    int width,height;

    public:
            void accept()
            {
                cout<<"\n\n Enter height and width: ";
                cin>>height>>width;
            }
};

class Rectangle:public Polygon
{
    public:
           void area()
           {
               cout<<"\n\n Rectangle Area: "<<width*height;
           }
};

class Triangle:public Polygon
{
    public:
           void area1()
           {
               cout<<"\n\n Triangle Area: "<<0.5*width*height;
           }
};

int main()
{
    cout<<"\n\n For Rectangle\n";
    Rectangle r,*pr=&r;
    pr->accept();
    cout<<"\n\n For Triangle\n";
    Triangle t,*pt=&t;
    pt->accept();
    pr->area();
    pt->area1();

    return 0;
}

Previous
Next Post »