Search This Blog

Sunday 28 February 2016

Single Inheritance

Practical-19

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 radius*radius, while volume of cylinder is pie*(radius) ^ 2*height.


#include<iostream>
#include<math.h>
using namespace std;
class circle
{
protected:
  int radius;
  int height;
public:
  void getdata();
 };
 void circle::getdata()
 {
     cout<<"Enter radius";
     cin>>radius;
     cout<<"Enter height";
     cin>>height;
 }
class cylinder:public circle
{
    float volume;
    float area;
    public:
    void volume1();
    void display();
};
void cylinder::volume1()
{
    cout<<radius<<height;
    area=radius*radius;
    volume=(M_PI*(area)*height);
}
void cylinder :: display()
{
    cout<<"area="<<area<<"\n";
    cout<<"Volume="<<volume<<"\n";
}
int main()
{
    cylinder c2;
    c2.getdata();
    c2.volume1();
    c2.display();
    return 0;
}

No comments:

Post a Comment