Derive two classes triangle and rectangle from the base class shape having data members length and breadth. Create functions to find the area of the triangle and the rectangle by runtime polymorphism

#include<iostream.h>
#include<conio.h>
class shape
{
   protected:
      double x;
      double y;
   public:
      virtual void display(){}
      void getdata(){cout<<"\nEnter x and y\n";cin>>x>>y;}
};
class triangle:public shape
{
   public:
      void display(){cout<<"\nArea  of triangle="<<(x*y*0.5);}
};
class rectangle: public shape
{
   public:
      void display(){cout<<"\nArea of rectangle="<<(x*y);}
};
void main()
{
   clrscr();
   shape *ptr;
   triangle t;
   rectangle r;
   ptr=&t;
   ptr->getdata();
   ptr->display();
   ptr=&r;
   ptr->getdata();
   ptr->display();
   getch();
}

Please make modifications

No comments:

Post a Comment