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.
#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;
}
Comments
Leave a comment