Based on the inheritance hierarchy/tree, write a C++ program to get the width, length and
height from users and call the calculateArea() member function of the Calculate
drived class to determine and return the volume of the user’s input. Before the program
ends, display all values read from users and the calculated area.
#include<iostream>
using namespace std;
class Rectangl
{
float length;
float width;
public:
Rectangl(){}
void AssignR()
{
cout << "Please, enter a length of rectangle: ";
cin >> length;
cout << "Please, enter a width of rectangle: ";
cin >> width;
}
float calculateArea()
{
return length*width;
}
void DisplayR()
{
cout << "\nLength of rectangle is " << length
<< "\nWidth of rectangle is " << width;
}
};
class Volume:public Rectangl
{
float height;
float vol;
public:
Volume(){}
Volume(float _height):height(_height){}
void Assign()
{
cout << "Please, enter a height of rectangle: ";
cin >> height;
}
void calculateVolume()
{
vol = calculateArea()*height;
}
void Display()
{
cout << "\nHeight is " << height
<< "\nVolome is " << vol;
}
};
int main()
{
Volume v;
v.AssignR();
v.Assign();
v.calculateVolume();
v.DisplayR();
v.Display();
}
Comments
Leave a comment