C++ Program to Create an Abstract Class Solid which Implements Shapes by Inheriting from Solid Class

«
»
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.

  1. /*
  2.  * C++ Program to Create an Abstract class Solid which Implements
  3.  * Cylinder, Cone and Sphere by Inheriting from Solid Class
  4.  */
  5. #include <iostream>
  6. #include <cmath>
  7. #include <iomanip>
  8.  
  9. const float PI = 3.14;
  10.  
  11. class Solid {
  12.     float radii;
  13.     float height;
  14.     public:
  15.         Solid() { }
  16.         virtual float area() = 0;
  17.         virtual float volume() = 0;
  18.         virtual float getRadius() = 0;
  19.         virtual float getHeight() = 0;
  20.         virtual ~Solid() { }
  21. };
  22.  
  23. class Sphere : public Solid {
  24.     float radii;
  25.     public:
  26.         Sphere(float r = 0): radii(r) { }
  27.         float area() { return 4 * PI * pow(radii, 2); }
  28.         float volume() { return (4 * PI / 3) * pow(radii, 3); }
  29.         float getRadius() { return radii; }
  30.         float getHeight() { }
  31.         virtual ~Sphere() { }
  32. };
  33.  
  34. class Cylinder : public Solid {
  35.     float radii;
  36.     float height;
  37.     public:
  38.         Cylinder(float r = 0, float h = 0): radii(r), height(h) { }
  39.         float area() { return 2 * PI * radii * (radii + height); }
  40.         float volume() { return PI * pow(radii, 2) * height;}
  41.         float getRadius() { return radii; }
  42.         float getHeight() { return height; }
  43.         virtual ~Cylinder() { }
  44. };
  45.  
  46. class Cone : public Solid {
  47.     float radii;
  48.     float height;
  49.     public:
  50.         Cone(float r = 0, float h = 0): radii(r), height(h) { }
  51.         float area() { return PI * radii * ( sqrt(pow(radii,2 ) + pow(height, 2)) + radii ); }
  52.         float volume() { return (PI / 3.0) * pow(radii, 2) * height;}
  53.         float getRadius() { return radii; }
  54.         float getHeight() { return height; }
  55.         virtual ~Cone() { }
  56. };
  57.  
  58. int main()
  59. {
  60.     Sphere sphere(5.0);
  61.     Cylinder cylinder(5.0, 5.0);
  62.     Cone cone(5.0, 5.0);
  63.  
  64.     std::cout << "Sphere :\nRadius = " << sphere.getRadius()
  65.               << std::endl;
  66.     std::cout << "Area of Sphere     = " << sphere.area()
  67.               << std::endl;
  68.     std::cout << "Volume of Sphere   = " << sphere.volume()
  69.               << std::endl;
  70.     std::cout << "Cylinder :\nRadius = " << cylinder.getRadius()
  71.               << ", Height = " << cylinder.getHeight()
  72.               << std::endl;
  73.     std::cout << "Area of Cylinder   = " << cylinder.area()
  74.               << std::endl;
  75.     std::cout << "Volume of Cylinder = " << cylinder.volume()
  76.               << std::endl;
  77.     std::cout << "Cone :\nRadius = " << cone.getRadius()
  78.               << ", Height = " << cone.getHeight()
  79.               << std::endl;
  80.     std::cout << "Area of Cone       = " << cone.area()
  81.               << std::endl;
  82.     std::cout << "Volume of Cone     = " << cone.volume()
  83.               << std::endl;
  84. }

$ 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.