Assume that circle is defined using radius and cylinder is defined using radius and height. Write a circle class as base class and inherit the cylinder class from it. Develop classes such that user can compute the area of circle objects and volume of cylinder objects. Area of circle is pie*radius*radius, while volume of cylinder is pie*(radius)^2*height.
Output :
#include<iostream.h>
#include<conio.h>
class circle
{
public:
float rad;
float area(float rad)
{
return 3.14*rad*rad;
}
};
class cylinder : public circle
{
public:
float rad2,height;
float volume(float rad2, float height)
{
return 3.14*(rad2*rad2)*height;
}
};
void main()
{
float r,r2,h;
clrscr();
cylinder obj;
cout<<"Radius of Circle : ";
cin>>r;
cout<<"\nArea of Circle : "<<obj.area(r);
cout<<"\n=======================\n\nRadius of Cylinder : ";
cin>>r2;
cout<<"\nHeight of Cylinder : ";
cin>>h;
cout<<"\n\nVolume of Cylinder : "<<obj.volume(r2,h);
getch();
}
Output :
Actually, we must have same radius in both case, thats why using Inheritance otherwise its meaningless !!
ReplyDeleteIn between thank you Dhruv Dabhi !