(Hierarchical Inheritance):-
Derive a class named as CYLINDER from CIRCLE class. Take necessary
data & member functions for this class to calculate the volume of the
cylinder. Show the result by accessing the area method of circle and
rectangle through object of rectangle class and the area of circle and volume
method of cylinder class through the objects of cylinder class.
#include<iostream>
using namespace std;
class Circle
{
protected:
float r ;
public:
void E(void)
{
cout <<"Enter the radius: ";
cin >>r;
}
void Dis(void)
{
cout <<"The area = " << (22/7 * r*r);
}
};
class Rectangle
{
protected:
float x, y ;
public:
void E_l(void)
{
cout << "\nEnter the length : ";
cin >>x;
cout << "Enter the breadth : " ;
cin >>y;
}
void Display(void)
{
cout <<"The area = "<<(x * y);
}
};
class Cylinder : public Circle, public Rectangle
{
public:
void volume(void)
{
cout <<"\nThe volume of the cylinder is: "
<< (22/7* r*r*x) ;
}
};
int main()
{
Circle c ;
cout <<"\nCircle dimensions\n" ;
c.E( );
c.Dis( );
Rectangle r ;
cout << "\n\nRectangle dimensions";
r.E_l( );
r.Display( );
Cylinder m ;
cout <<"\n\nCylinder dimensions\n";
m.E( );
m.E_l( );
m.volume( );
return 0;
}
Comments
Leave a comment