(Multilevel Inheritance):-
Derive a class named as BOX from RECTANGLE class. Take necessary
data & member functions for this box class to calculate the volume of the
box. All the data members are initialized through the constructors. Show the
result by accessing the area method of circle and rectangle and the volume
method of box class through the objects of box class.
Source code
#include <iostream>
using namespace std;
class CIRCLE{
private:
double radius;
public:
CIRCLE(double rad){
radius=rad;
}
void Area_circle(){
double area=3.142*radius*radius;
cout<<"\nArea of the circle: "<<area;
}
};
class RECTANGLE: public CIRCLE{
private:
double length;
double width;
public:
RECTANGLE(double l,double w): CIRCLE(l){
length=l;
width=w;
}
void Area_rect(){
double area=length*width;
cout<<"\nArea of the rectangle: "<<area;
}
};
class BOX: public RECTANGLE{
private:
double length;
double width;
double height;
public:
BOX(double l, double w, double h):RECTANGLE(l,w){
length=l;
width=w;
height=h;
}
void box_volume(){
double volume=length*width*height;
cout<<"\nVolume of the box: "<<volume;
}
};
int main()
{
BOX b(7,5,6);
b.Area_circle();
b.Area_rect();
b.box_volume();
return 0;
}
Sample Output
Comments
Leave a comment