C++ program to find the volume of shapes using class

Learn, how can we find the volume of shapes using the class and object approach?
Submitted by Shubh Pachori, on September 04, 2022

Example:

Enter Sphere's radius :10
Volume of Sphere :4190.48

Enter Cylinder's radius :11
Enter Cylinder's height :12
Volume of Cylinder :4563.43

Enter Cube's side :13
Volume of Cube :2197

Enter Cuboid's length :14
Enter Cuboid's height :15
Enter Cuboid's breadth  :16
Volume of Cuboid :3360

C++ code to find the volume of shapes using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Shape {
  // protected data member
 protected:
  float volume;
};

// class Sphere inheriting class Shape
class Sphere : public Shape {
  // protected data member
 protected:
  float radius;

  // public member functions
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "Enter Sphere's radius : ";
    cin >> radius;
  }

  // Volume() function to calculate 
  // volume of the class
  double Volume() {
    // calculating volume
    volume = (4 * 22 * radius * radius * radius) / (3 * 7);

    // returning volume
    return volume;
  }
};

// class Cylinder inheriting class Shape
class Cylinder : public Shape {
  // protected data members
 protected:
  float radius, height;

  // public member functions
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "Enter Cylinder's radius : ";
    cin >> radius;

    cout << "Enter Cylinder's height : ";
    cin >> height;
  }

  // Volume() function to calculate Volume of the class
  double Volume() {
    // calculating volume
    volume = (22 * radius * radius * height) / 7;

    // returning volume
    return volume;
  }
};

// class Cube inheriting class Shape
class Cube : public Shape {
  // protected data member
 protected:
  float side;

  // public member functions
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "Enter Cube's side :";
    cin >> side;
  }

  // Volume() function to calculate Volume of the class
  double Volume() {
    // calculating volume
    volume = side * side * side;

    // returning volume
    return volume;
  }
};

// class Cuboid inheriting class Shape
class Cuboid : public Shape {
  // protected data members
 protected:
  float length, height, breadth;

  // public member functions
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "Enter Cuboid's length : ";
    cin >> length;

    cout << "Enter Cuboid's height : ";
    cin >> height;

    cout << "Enter Cuboid's breadth : ";
    cin >> breadth;
  }

  // Volume() function to calculate 
  // volume of the class
  double Volume() {
    // calculating volume
    volume = length * breadth * height;

    // returning volume
    return volume;
  }
};

int main() {
  // create a object
  Sphere S;

  // float type variable to store volume
  float sphere;

  // calling get() function to 
  // input radius of sphere
  S.get();

  // calling Volume() function to 
  // calculate volume of the sphere
  sphere = S.Volume();

  cout << "Volume of Sphere : " << sphere << endl;
  cout << endl;

  //------------------------------------------------------------------------------

  // create a object
  Cylinder Cy;

  // float type variable to store volume
  float cylinder;

  // calling get() function to input radius and height of cylinder
  Cy.get();

  // calling Volume() function to calculate volume of the cylinder
  cylinder = Cy.Volume();

  cout << "Volume of Cylinder :" << cylinder << endl;
  cout << endl;

  //-----------------------

  // create a object
  Cube Cu;

  // float type variable to store volume
  float cube;

  // calling get() function to input sides of cube
  Cu.get();

  // calling Volume() function to 
  // calculate volume of the cube
  cube = Cu.Volume();

  cout << "Volume of Cube : " << cube << endl;
  cout << endl;

  //----------------------------

  // create a object
  Cuboid C;

  // float type variable to store volume
  float cuboid;

  // calling get() function to input length,
  // breadth and height of cuboid
  C.get();

  // calling Volume() function to 
  // calculate volume of the cuboid
  cuboid = C.Volume();

  cout << "Volume of Cuboid : " << cuboid << endl;
  cout << endl;

  return 0;
}

Output:

Enter Sphere's radius : 23
Volume of Sphere : 50985.5

Enter Cylinder's radius : 32
Enter Cylinder's height : 5
Volume of Cylinder : 16091.4

Enter Cube's side : 23
Volume of Cube : 12167

Enter Cuboid's length : 32
Enter Cuboid's height : 23
Enter Cuboid's breadth : 10
Volume of Cuboid : 7360

Explanation:

In the above code, we have created a parent class Shape, and a protected float type data member volume to calculate and store the volume of various shapes. A base class Sphere inherits the parent class Shape and its elements and this base class has two public member functions get() and Volume(), to store attributes and to calculate the volume of the shape. Same as above there are three more base classes Cylinder, Cube and Cuboid with two public member functions get() and Volume() to store the attributes and to calculate the volume of the shape.

In the main() function, we are creating objects of these base classes S of class Sphere, Cy of class Cylinder, Cu of class Cube and C of class Cuboid, then initialize some float variables to store volume’s of particular shapes and then calling functions to perform operations like to input attributes and to calculate the volume of the shapes. In the end, returning results.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.