C++ Program to Find Volume,Surface Area, Density of Cuboid


Write a C++ program to find volume, surface area and density of cuboid using class. Define a class called CuboidSolid with data members length,breadth,height and mass of type float. Define getVolume(), getSurafaceArea(), getDensity() as member functions of the class.

float getVolume() - returns volume of cuboid.
float getSurfaceArea() - returns surface area of cuboid.
float getDensity() - returns density of cuboid.

Also write a main function to illustrate the use of CuboidSolid class.


Formula to find volume of cuboid = l3
Formula to find surface are of cuboid = 2lb+2lh+2hb
Formula to find density of cuboid = mass/volume

Where l=length, b=breadth and h=height of cuboid.

C++ Program / Source Code:

Here is the source code of C++ program to calculate volume, surface area and density of cuboid.
/* Aim: Write a program to calculate volume, surface area and density of cuboid */

using namespace std;

//Cuboid class
class CuboidSolid
 float l,b,h,mass;


//parameterized constructor to initialize length, breadth, height and mass
 void setter(float x,float y,float z,float w)

//function to display data
 void getter()
 cout<<endl<<"Length: "<<l<<endl<<"Breadth: "<<b<<endl<<"Height: "<<h<<endl<<"Mass: "<<mass<<endl;

//function to return volume
 float getVolume()
 return (l*b*h);

//function to return surface area
 float getSurfaceArea()

//function to return density
 float getDensity()
  float vol=l*b*h;

int main()
 CuboidSolid C;
 float tmp;

 C.setter(12,6,7,10); // set values of l,b,h and mass
 C.getter(); // display the l,b,h, and mass
 cout<<"The volume of cuboid is "<<C.getVolume()<<endl;
 cout<<"Surface area of cuboid is "<<C.getSurfaceArea()<<endl;
 cout<<"Density of cuboid is "<<C.getDensity()<<endl;

/* Output of above code :

Length: 12
Breadth: 6
Height: 7
Mass: 10
The volume of cuboid is 504
Surface area of cuboid is 372
Density of cuboid is 0.0198413