Question

In: Computer Science

Design a header file named gshapes.h that contains a namespace named Shapes. The Shapes namespace should...

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.

Solutions

Expert Solution

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!!!


Related Solutions

Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method...
Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method with the signature void message (string, ostream &) that prints a string to the output stream. a method with the signature void message (double, ostream &) that prints a double to the output stream. Create a testNamespaces.cpp that uses the yourname namespace, and invokes the message() string method with a message of your choice and cout as input parameters, and invokes the message double...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this header file, declare an Employee class with the following features: Private members a std::string for the employee’s first name a std::string for the employee’s last name an unsigned int for the employee’s identification number a std::string for the city in which the employee works Public members A constructor that takes no arguments A constructor that takes two arguments, representing: the employee’s first name the...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
Write scores of 20 students on a quiz to a file. The file should be named...
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT