#include<iostream>
using namespace std;
class shape
{
protected:
string colour;
public:
void input(string x) //simple member function
{
colour=x;
cout<<"colour is:"<<colour<<endl;
}
virtual double area()=0; //pure virtual function
};
class rectangle:public shape //inherit with shape class
{
private:
int length,width;
public:
void rectan(int x,int y)
{
length=x;
width=y;
}
double area() //define the pure virtual function
{
return width*length;
}
};
class triangle:public shape //define the pure virtual function
{
int base,height;
public:
void tring(int x,int y)
{
base=x;
height=y;
}
double area()
{
return base*height/2;
}
};
int main()
{
shape *obj1; //making the pointer of base class shape
rectangle obj2; //making the object of rectangle class
obj1=&obj2;//pointer of shape class take the object of rectangle class
obj2.rectan(3,4);
obj1->area();
obj2.input("green");
cout<<"the area of the rectangle is:"<<obj1->area()<<endl;
triangle obj3;//making the object of triangle class
obj1=&obj3;//pointer of shape class take the object of triangle class
obj3.tring(4,8);
obj1->area();
cout<<"the area of the triangle is:"<<obj1->area()<<endl;
}
No comments:
Post a Comment