Problem on Base class-Create a base class shape

Create a base class shape. Use this class to store two double type values that could be used to compute area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base a member function getdata() to initialize base class data member and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine it the derived class to suit their requirements.

#include<iostream.h>
 class Shape
 {
     protected:
    double l,b;
    public:
    void getdata(double l1,double b1)
    {
        l=l1;
        b=b1;
    }
        void virtual display_area()=0;
 };
 class triangle:public Shape
 {
     public:
    void display_area()
    {
        cout<<"\nArea of Triangle:"<<(0.5*l*b);
    }
 };
 class Rectangle:public Shape
 {
     public:
    void display_area()
    {
        cout<<"\nArea of Rectangle:"<<l*b;
    }
 };
 void main()
 {
    Shape * s1;
    triangle t1;
        s1=&t1;
    s1->getdata(20,10);
    s1->display_area();

    Rectangle r1;
    s1=&r1;
    s1->getdata(5,4);
    s1->display_area();
 }

Leave a Reply