In: Computer Science
Design a header file named gshapes.h that contains a namespace named Shapes. The Shapes namespace should contain definitions of at least six inline user-defined functions, each of which computes and returns either volume, or surface area for at least three different geometric shapes of your choice (e.g., cone, pyramid, cylinder, sphere, ...). The gshapes.h header file should be saved within the same project folder that also contains your main program files.
Design the main program that includes the gshapes.h header file and uses the functions from the Shapes namespace. The program will display a menu with at least three geometric shapes' options for the user to select. When the user selects a shape, the program will first obtain the necessary input values for the selected shape, and then call two functions from the Shapes namespace to compute and display the volume and surface area for that geometric shape respectively. The program will then loop back to the main menu for the user to select and process another shape, or to exit the program.
Screenshot
Program
gshapes.h
#ifndef NUM_H
#define NUM_H
namespace Shapes {
#define PI 3.14
//Functions for cone
inline double getSurfaceAreaOfCone(double
radius,double slant) {
return (PI*radius*slant) +
(PI*radius*radius);
}
inline double getVolumeOfCone(double radius, double
height) {
return
(double(1)/3)*PI*radius*radius*height;
}
//Functions for cylinder
inline double getSurfaceAreaOfCylinder(double radius,
double height) {
return (2*PI*radius*radius)+ (2 *
PI*radius*height);
}
inline double getVolumeOfCylinder(double radius,
double height) {
return
PI*radius*radius*height;
}
//Functions for sphere
inline double getSurfaceAreaOfSphere(double radius)
{
return (4 *
PI*radius*radius);
}
inline double getVolumeOfSphere(double radius) {
return ((double)4/3)*PI *
radius*radius*radius;
}
}
#endif
Main.cpp
//Header files
#include <iostream>
#include<iomanip>
#include "gshapes.h"
using namespace Shapes;
//Function prototype
int menu();
int main()
{
//Variables for inputs
int radius, height, slant;
//Get option
int opt = menu();
//Double value formatting
std::cout << std::setprecision(2);
//Loop unti quit
while (opt != 4) {
std::cout << "\n";
//For cone
if (opt == 1) {
std::cout
<< "Enter radius in cm: ";
std::cin
>> radius;
std::cout
<< "Enter height in cm: ";
std::cin
>> height;
std::cout
<< "Enter slant in cm: ";
std::cin
>> slant;
std::cout
<< "Surface area of Cone = " <<
getSurfaceAreaOfCone(radius, slant)
<<" cm^2"<< std::endl;
std::cout
<< "Volume of Cone = " <<
getVolumeOfCone(radius,height)
<<" cm^3\n"<< std::endl;
}
//For cylinder
else if (opt == 2) {
std::cout
<< "Enter radius in cm: ";
std::cin
>> radius;
std::cout
<< "Enter height in cm: ";
std::cin
>> height;
std::cout
<< "Surface area of Cylinder = " <<
getSurfaceAreaOfCylinder(radius,height)
<< " cm^2" << std::endl;
std::cout
<< "Volume of Cylinder = " <<
getVolumeOfCylinder(radius, height)
<< " cm^3\n" << std::endl;
}
//For sphere
else if (opt == 3) {
std::cout
<< "Enter radius in cm: ";
std::cin
>> radius;
std::cout
<< "Surface area of Sphere = " <<
getSurfaceAreaOfSphere(radius)
<< " cm^2" << std::endl;
std::cout
<< "Volume of Sphere = " <<
getVolumeOfSphere(radius)
<< " cm^3\n" << std::endl;
}
//Repetition
opt = menu();
}
//End
std::cout << "\n GOOD
BYE!!!\n";
return 0;
}
//Function to display menu
//Return user option
int menu() {
int opt;
std::cout << "USER OPTION:-\n";
std::cout << "1. Surface area and volume of
cone\n"
<< "2. Surface area and
volume of Cylinder\n"
<< "3. Surface area and
volume of Sphere\n"
<<"4. Quit\n";
std::cout << "Enter your option:-";
std::cin >> opt;
while (opt < 1 || opt>4) {
std::cout << "ERROR!!!Option
must be 1-4\n";
std::cout << "Enter your
option:-";
std::cin >> opt;
}
return opt;
}
------------------------------------------------------------
Output
USER OPTION:-
1. Surface area and volume of cone
2. Surface area and volume of Cylinder
3. Surface area and volume of Sphere
4. Quit
Enter your option:-1
Enter radius in cm: 2
Enter height in cm: 5
Enter slant in cm: 2
Surface area of Cone = 25 cm^2
Volume of Cone = 21 cm^3
USER OPTION:-
1. Surface area and volume of cone
2. Surface area and volume of Cylinder
3. Surface area and volume of Sphere
4. Quit
Enter your option:-2
Enter radius in cm: 5
Enter height in cm: 8
Surface area of Cylinder = 4.1e+02 cm^2
Volume of Cylinder = 6.3e+02 cm^3
USER OPTION:-
1. Surface area and volume of cone
2. Surface area and volume of Cylinder
3. Surface area and volume of Sphere
4. Quit
Enter your option:-3
Enter radius in cm: 5
Surface area of Sphere = 3.1e+02 cm^2
Volume of Sphere = 5.2e+02 cm^3
USER OPTION:-
1. Surface area and volume of cone
2. Surface area and volume of Cylinder
3. Surface area and volume of Sphere
4. Quit
Enter your option:-4
GOOD BYE!!!