Answer to Question #314759 in C++ for zizou

Question #314759

write a programme that implements the following:

1- a class called shape which includes the height and the width as private attributes,

2- a derived class called rectangle which inherits the attributed from the shape and have a method called area which finds the area of the rectangle

3- a derived class called triangle which inherits the attributes from the shape and have a method called area that finds the area of the triabgle,


1
Expert's answer
2022-03-20T09:50:32-0400
#include <iostream>

using namespace std;

class Shape
{
	float height;
	float weight;
public:
	Shape(float _height, float _weight)
	:height(_height), weight(_weight){}
	float GetHeight() { return height; }
	float GetWeight(){ return weight; }
};


class Rectangl:public Shape
{
public:
	Rectangl(float _height, float _weight)
	:Shape(_height, _weight){}
	float Area()
	{
		return GetHeight()*GetWeight();
	}
};

class Triangle :public Shape
{
public:
	Triangle(float _height, float _weight)
	:Shape(_height, _weight) {}
	float Area()
	{
		return (0.5*GetHeight()*GetWeight());
	}
};

int main()
{
	Rectangl rt(10, 20);
	cout << "The Area of rectangle with height=10 and weight=20 is " << rt.Area()<<endl;
	Triangle tr(10, 20);
	cout << "The Area of triangle with height=10 and weight=20 is " << tr.Area();
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog