Answer to Question #314760 in C++ for zizou

Question #314760

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-20T06:20:11-0400


#include <iostream>
using namespace std;


class Shape
{
private:
	double width, height;
public:
	//  constructor
	Shape(double newWidth, double newHeight){
		this->width=newWidth;
		this->height=newHeight;
	}
	double getWidth() const
	{
		return width;
	}
	double getHeight() const
	{
		return height;
	}
};


class Rectangle: public Shape
{
public:
   
    Rectangle(double width, double height):Shape(width,height)
    {


    }
    double area()
    {
        return (getWidth()*getHeight());
    }
};


class Triangle: public Shape
{
public:
    Triangle(double base, double height): Shape(base,height)
    {


    }
    double area()
    {
        return (getWidth()*getHeight())/2.0;
    }
};


int main ()
{
	Rectangle rectangle(5.0,3.0);
	Triangle triangle(2.0,5.0);
	cout <<"Area of rectangle is: "<< rectangle.area() << endl;
	cout <<"Area of triangle is: "<< triangle.area() << endl;


	system("pause");
	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