Answer to Question #235211 in C++ for SOM

Question #235211

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.


1
Expert's answer
2021-09-12T14:23:31-0400
#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";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS