A program to demonstrate the concept of abstract base class in C++

#include<iostream>
#include<conio.h>
using namespace std;
class Shape
{
protected:
float dim1, dim2;
public:
void setdata(float d1, float d2)
{
dim1=d1;
dim2=d2;
}
virtual float area()=0;
};
class Triangle: public Shape
{
public:
float area()
{
return(dim1*dim2*1/2);
}
};
class Rectangle: public Shape
{
public:
float area()
{
return(dim1*dim2);
}
};
main()
{
float aot, aor;
Shape *s;
Triangle t;
Rectangle r;
t.setdata(4.52, 3.25);
r.setdata(8.72, 6.36);
s=&t;
aot=s->area();
s=&r;
aor=s->area();
cout<<“Area of triangle:”<<aot<<endl;
cout<<“Area of rectangle:”<<aor;
getch();
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *