In: Computer Science
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube, Sphere, Prism, Cylinder, Cone), where each shape has its own class (with a volume function, surface area function, constructor, as well as get and set functions), and the shape dimensions are private class members. Include input and display functions.
The code is given below:
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class Cube //Class Cube
{
private: float s;
public:
float getVolume()
{
return (s *s *s);
}
float getSurfaceArea()
{
return (6 *s *s);
}
Cube() {}
void setter(float x)
{
s = x;
}
void getter()
{
cout << "Length: " << s << endl;
}
void display(float d1, float d2)
{
cout << "The Volume of the cube: " << d1 << "cm3" << endl;
cout << "The Surface Area of the cube: " << d2 << "cm²" << endl;
}
float input()
{
float i;
cout << "Enter the side of cube: ";
cin >> i;
return i;
}
};
class Sphere //Class Sphere
{
private: float d;
public:
float getVolume()
{
return ((3.14 *d *d *d) / 6);
}
float getSurfaceArea()
{
return (3.14 *d *d);
}
Sphere() {}
void setter(float x)
{
d = x;
}
void getter()
{
cout << "Diameter: " << d << endl;
}
void display(float d1, float d2)
{
cout << "The Volume of the sphere: " << d1 << "cm3" << endl;
cout << "The Surface Area of the sphere: " << d2 << "cm²" << endl;
}
float input()
{
float i;
cout << "Enter the diameter of sphere: ";
cin >> i;
return i;
}
};
class Prism //Class Prism
{
private: float a, b, c, h3, l;
public:
float getVolume()
{
return (((b *h3) / 2) *l);
}
float getSurfaceArea()
{
return (((a + b + c) *l) + (b *h3));
}
Prism() {}
void setter(float v, float w, float x, float y, float z)
{
a = v;
b = w;
c = x;
h3 = y;
l = z;
}
void getter()
{
cout << "Side a: " << a << endl;
cout << "Side b: " << b << endl;
cout << "Side c: " << c << endl;
cout << "Height: " << h3 << endl;
cout << "Length: " << l << endl;
}
void display(float d1, float d2)
{
cout << "The Volume of the cube: " << d1 << "cm3" << endl;
cout << "The Surface Area of the cube: " << d2 << "cm²" << endl;
}
};
class Cylinder //Class Cylinder
{
private: float r, h;
public:
float getVolume()
{
return (3.14 *r *r *h);
}
float getSurfaceArea()
{
return ((2 * 3.14 *r *h) + (2 * 3.14 *r *r));
}
Cylinder() {}
void setter(float x, float y)
{
r = x;
h = y;
}
void getter()
{
cout << "Radius: " << r << endl;
cout << "Height: " << h << endl;
}
void display(float d1, float d2)
{
cout << "The Volume of the cylinder: " << d1 << "cm3" << endl;
cout << "The Surface Area of the cylinder: " << d2 << "cm²" << endl;
}
};
class Cone //Class cone
{
private: float r2, h2;
public:
float getVolume()
{
return ((3.14 *r2 *r2 *h2) / 3);
}
float getSurfaceArea()
{
float val1 = h2 *h2 + r2 * r2;
float val2 = sqrt(val1);
return (3.14 *r2 *(r2 + val2));
}
Cone() {}
void setter(float x, float y)
{
r2 = x;
h2 = y;
}
void getter()
{
cout << "Radius: " << r2 << endl;
cout << "Height: " << h2 << endl;
}
void display(float d1, float d2)
{
cout << "The Volume of the cone: " << d1 << "cm3" << endl;
cout << "The Surface Area of the cone: " << d2 << "cm²" << endl;
}
};
int main() //Main function
{
char op;
float s, d, a, b, c, h3, l, r, h, r2, h2;
Cube C1;
Sphere S1;
Prism P1;
Cylinder C2;
Cone C3;
cout << "1.Cube\n2.Sphere\n3.Prism\n4.Cylinder\n5.Cone\nEnter you choice: ";
cin >> op;
switch (op)
{
case '1':
s = C1.input();
C1.setter(s);
C1.getter();
C1.display(C1.getVolume(), C1.getSurfaceArea());
break;
case '2':
d = S1.input();
S1.setter(d);
S1.getter();
S1.display(S1.getVolume(), S1.getSurfaceArea());
break;
case '3':
cout << "Enter the side a of prism: ";
cin >> a;
cout << "Enter the side b of prism: ";
cin >> b;
cout << "Enter the side c of prism: ";
cin >> c;
cout << "Enter the height of prism: ";
cin >> h3;
cout << "Enter the length of prism: ";
cin >> l;
P1.setter(a, b, c, h3, l);
P1.getter();
P1.display(P1.getVolume(), P1.getSurfaceArea());
break;
case '4':
cout << "Enter the Radius of cylinder: ";
cin >> r;
cout << "Enter the Height of cylinder: ";
cin >> h;
C2.setter(r, h);
C2.getter();
C2.display(C2.getVolume(), C2.getSurfaceArea());
break;
case '5':
cout << "Enter the Radius of cone: ";
cin >> r2;
cout << "Enter the Radius of height: ";
cin >> h2;
C3.setter(r2, h2);
C3.getter();
C3.display(C3.getVolume(), C3.getSurfaceArea());
break;
default:
cout << "Please enter a valid option 1-5 only!";
}
}
Some of the screenshots of output are given below:
Cube
Cylinder