Write a program to declare Class Shape Derived two class: Triangle and Rectangle make Area function as Virtual Function

/*
    Class Shape
    Derived two class: Triangle and Rectangle
    Area function as Virtual
*/
#include<iostream>
using namespace std;

class Shape
{
    protected:
            int d1,d2;

    public:
           virtual void area()=0;

            void accept()
            {
                cout<<"\n\n Enter base and height of triangle: ";
                cin>>d1>>d2;
            }

};

class Triangle:public Shape
{
    public:
            void area()
            {
                accept();
                cout<<"\n\n Area of Triangle: "<<0.5*d1*d2;
            }
};

class Rectangle:public Shape
{
    public:
            void accept()
            {
                cout<<"\n\n Enter length and breath of rectangle: ";
                cin>>d1>>d2;
            }
            void area()
            {
                cout<<"\n\n Area of Triangle: "<<d1*d2;
            }
};

int main()
{
    Rectangle r;
    Triangle t;

    r.accept();
    r.area();

    t.area();

    return 0;
}

Previous
Next Post »