Wednesday, April 7, 2021

simple program using inheritance (OOP) (58)

#include<iostream>

using namespace std;

class circle

{

protected:

double radius;

public:

circle() //here i define a default constructure

{

radius=0;

}

circle(double x) //here i define a parameterized constructure

{

radius=x;

}

double getarea(); //here i declear a function that return a area of circle

};

double circle::getarea() //here i define a function that return a area of circle

{

return 3.14*radius*radius;

}

class cylinder:public circle //here i inherit the class cylinder to circle class

{

private:

double height;

public:

cylinder() //here i define a default constructure

{

radius=0;

height=0;

}

cylinder(double x,double y) //here i define a parameterized constructure

{

radius=x;

height=y;

}

double getvolume(); //here i declear a function that return a volume of cylinder

};

double cylinder::getvolume() //here i define a function that return a volume of cylinder

{

return 3.14*radius*radius*height;

}

int main()

{

double a,b;

cout<<"please enter the radius of the circle here:"; 

cin>>a;

cout<<endl;

circle obj1(a);

cout<<"the area of the circle is:"<<obj1.getarea()<<endl;

cout<<endl;

cout<<"please enter the radius of the cylinder here:";

cin>>a;

cout<<endl;

cout<<"please enter the  height of the cylinder here:";

cin>>b;

cout<<endl;

cylinder obj2(a,b);

cout<<endl;

cout<<"the volume of the cylinder is:"<<obj2.getvolume()<<endl;

} 

No comments:

Post a Comment