This C++ program which creates an abstract class solid from which cylinder, sphere and cone have been inherited . A class is called an abstract class when it contains one or more purely virtual function. An abstract class can never be instantiated but inherited. The ‘Solid’ class is implemented as an abstract class from which three other classes, i.e. ‘Sphere’, ‘Cone’ and ‘Cylinder’ have been inherited and instantiated.
Here is the source code of the C++ program which creates an abstract class solid from which cylinder, sphere and cone have been inherited. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Create an Abstract class Solid which Implements
* Cylinder, Cone and Sphere by Inheriting from Solid Class
*/
#include <iostream>
#include <cmath>
#include <iomanip>
const float PI = 3.14;
class Solid {
float radii;
float height;
public:
Solid() { }
virtual float area() = 0;
virtual float volume() = 0;
virtual float getRadius() = 0;
virtual float getHeight() = 0;
virtual ~Solid() { }
};
class Sphere : public Solid {
float radii;
public:
Sphere(float r = 0): radii(r) { }
float area() { return 4 * PI * pow(radii, 2); }
float volume() { return (4 * PI / 3) * pow(radii, 3); }
float getRadius() { return radii; }
float getHeight() { }
virtual ~Sphere() { }
};
class Cylinder : public Solid {
float radii;
float height;
public:
Cylinder(float r = 0, float h = 0): radii(r), height(h) { }
float area() { return 2 * PI * radii * (radii + height); }
float volume() { return PI * pow(radii, 2) * height;}
float getRadius() { return radii; }
float getHeight() { return height; }
virtual ~Cylinder() { }
};
class Cone : public Solid {
float radii;
float height;
public:
Cone(float r = 0, float h = 0): radii(r), height(h) { }
float area() { return PI * radii * ( sqrt(pow(radii,2 ) + pow(height, 2)) + radii ); }
float volume() { return (PI / 3.0) * pow(radii, 2) * height;}
float getRadius() { return radii; }
float getHeight() { return height; }
virtual ~Cone() { }
};
int main()
{
Sphere sphere(5.0);
Cylinder cylinder(5.0, 5.0);
Cone cone(5.0, 5.0);
std::cout << "Sphere :\nRadius = " << sphere.getRadius()
<< std::endl;
std::cout << "Area of Sphere = " << sphere.area()
<< std::endl;
std::cout << "Volume of Sphere = " << sphere.volume()
<< std::endl;
std::cout << "Cylinder :\nRadius = " << cylinder.getRadius()
<< ", Height = " << cylinder.getHeight()
<< std::endl;
std::cout << "Area of Cylinder = " << cylinder.area()
<< std::endl;
std::cout << "Volume of Cylinder = " << cylinder.volume()
<< std::endl;
std::cout << "Cone :\nRadius = " << cone.getRadius()
<< ", Height = " << cone.getHeight()
<< std::endl;
std::cout << "Area of Cone = " << cone.area()
<< std::endl;
std::cout << "Volume of Cone = " << cone.volume()
<< std::endl;
}
$ a.out Sphere : Radius = 5 Area of Sphere = 314 Volume of Sphere = 523.333 Cylinder : Radius = 5, Height = 5 Area of Cylinder = 314 Volume of Cylinder = 392.5 Cone : Radius = 5, Height = 5 Area of Cone = 189.516 Volume of Cone = 130.833
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Check C++ Books
- Practice Programming MCQs
- Apply for C++ Internship
- Check Computer Science Books