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,
#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();
}
Comments
Leave a comment