Answer to Question #231990 in C++ for mahnoor

Question #231990

Write a program that defines a shape class with a constructor that gives value to width and height. The define two sub-classes triangle and rectangle, that calculate the area of the shape area ().

 

In the main, define two variables a triangle and a rectangle and then call the area() function in this two varibles.


1
Expert's answer
2021-09-01T23:56:59-0400
#include <iostream>




using namespace std;


class Shape
{
protected:
	float width, height;
public:
	void set_data (float a, float b)
	{
		width = a;
		height = b;
	}
};


class Rectangle: public Shape
{
public:
	float area ()
	{
		return (width * height);
	}
};


class Triangle: public Shape
{
public:
	float area ()
	{
		return (width * height / 2);
	}
};


int main (){


	Rectangle rectangle;
	Triangle triangle;
	rectangle.set_data (5,3);
	triangle.set_data (2,5);
	cout <<"The area of the rectangle is:   " <<rectangle.area() << endl;
	cout << "The area of the triangle is:   "<<triangle.area() << endl;
	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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS