Answer to Question #284945 in C++ for Shayan

Question #284945

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();



1
Expert's answer
2022-01-05T16:25:33-0500
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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Shayan khan
05.01.22, 23:44

Thank you veryhelpful

Leave a comment

LATEST TUTORIALS
New on Blog