Create a class Shape and two additional classes (each derived from Shape) named Rectangle and Triangle. The member attributes and functions for each class are given below:
Shape:
private:
int width;
int height;
public:
virtual int getArea();
Rectangle:
public:
virtual int getArea();
Triangle:
public:
virtual int getArea();
Driver Program:
Rectangle r1(3,2);
Triangle t1(3,2);
Shape *s1 = &r1;
s1->getArea();
Shape *s2 = &t1;
s2->getArea();
class Shape
{
public:
int width;
int height;
void getdimension(int w, int h)
{
width = w;
height = h;
}
};
class Rectangle:public Shape
{
public:
void getArea(void)
{
int Area;
Area = width*height;
cout<<"\n\tArea of Rectangle = "<<Area;
}
};
class Triangle:public Shape
{
public:
void getArea()
{
int Area;
Area = width*height/2;
cout<<"\n\tArea of Triangle = "<<Area;
}
};
//Driver Program:
int main()
{
Rectangle r1;
Triangle t1;
r1.getdimension(3,2);
t1.getdimension(3,2);
r1.getArea();
t1.getArea();
return(0);
}
Comments
Thank you veryhelpful
Leave a comment