Question

In: Computer Science

Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube,...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere...
Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere volume = and the sphere surface area = , where r is the radius. 1. Read r from the user using cin statement. 2. Calculate the volume (Vol) and the surface area (Area) of a sphere. 3. Print r, Vol, and Area in a suitable messages.
Write a program that reads the radius of a sphere and calculates it's volume and surface...
Write a program that reads the radius of a sphere and calculates it's volume and surface area. Format results to 4 decimal places. The radius=3.2 Your program should output volume and surface area.  Repeat same steps for program2, but this time use the following input values: a= ꟷ2.4 , b = 4.5 . Your program should output sum, Fun1, and Fun2. It's for C++
8. Complete the program that calculates the volume of a cube. If the side length entered...
8. Complete the program that calculates the volume of a cube. If the side length entered for the cube is negative, the program should display an error message saying the length should be positive. If the side length entered for the cube is greater than 100, the program should print a message saying the side is too big. Otherwise the program should calculate and print the volume of the cube of the given side length. Sample program runs are shown...
1) Write a functional program in Java that can calculate the volume and surface area of...
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube 2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube 3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional...
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional object. Use the following guidelines to write your program: Create a word problem that involves calculating the volume and surface area of a three-dimensional object. Choose one of the following: Cube: surface area 6 s2 , volume s3 Sphere: surface area 4πr2 , volume (4.0/3.0) π r3 Cylinder: surface area 2π r2 + 2 π r h, volume π r2 h Cone: surface area...
Write a C++ program for the following problem: Calculate and print the area and volume of...
Write a C++ program for the following problem: Calculate and print the area and volume of a cone inside a While  loop that goes from 1 to 20 with a step of .5. (the step is 1/2 or Point 5, so you go 10, 10.5,11, 11.5) Note: Your loop variable will need to be a double data type Use two decimal places on all numbers that are double data type. This will be a table with 3 columns. Don't worry about...
Write a program that calculates the area and circumference of a circle. It should ask the...
Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159 Sample Output: Enter the number...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle...
know the mathematical relationship between surface area and volume, and explain how surface to area volume...
know the mathematical relationship between surface area and volume, and explain how surface to area volume affects heat loss. Which can be smaller endotherms or ectotherms  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT