Create a base class called Solid. Use this class to store two float type values. Derive two classes called Cylinder and Cone from the base class. Add to the base class, a member function get data to initialize base class data members and another member function display to compute and display the volume of figures.
#include <cmath>
#include <iomanip>
using namespace std;
const float PI = 3.14;
class Solid {
float r;
float h;
public:
Solid() { }
virtual ~Solid() { }
void getData(){
float r = 0;
float h = 0;
}
float display(){
return PI * pow(r, 2) * h;
return (PI / 3.0) * pow(r, 2) * h;
}
};
class Cylinder : public Solid {
float r;
float h;
public:
Cylinder(float r = 0, float h = 0): r(r), h(h) { }
float volume() { return PI * pow(r, 2) * h;}
virtual ~Cylinder() { }
};
class Cone : public Solid {
float r;
float h;
public:
Cone(float r = 0, float h = 0): r(r), h(h) { }
float volume() { return (PI / 3.0) * pow(r, 2) * h;}
virtual ~Cone() { }
};
int main()
{
Cylinder cylinder(5.5, 2.0);
Cone cone(7.0, 5.3);
cout << "Cylinder Volume is " << cylinder.volume()<<"\n";
cout << "Cone Volume is " << cone.volume()<<"\n";
}
Comments
Leave a comment