Question

In: Computer Science

IN C++ Modify the above program to compute the side area, total area, and volume of...

IN C++

Modify the above program to compute the side area, total area, and volume of a cylinder and the area and volume of a sphere, depending on the choice that the user makes. Your program should ask users to enter 1 to choose cylinder or 2 for sphere, and display an "invalid choice error" for other values.

For a cylinder, we want to compute:

Side area: (2*PI*r) * h

Total Area: 2*(PI*r2) + Side area

Volume: (PI*r2)*h

For a sphere, we want to compute:

Surface area: 4*PI*r2

Volume: (4.0/3.0)*PI*r3.

Use overloading whenever possible. Provide comments for any functions that are overloaded and explain why they could be overloaded

Solutions

Expert Solution

In this program, we are going to overload two function

  1. volume
  2. totalArea

The code for the program looks is attached below.

#include<bits/stdc++.h>
using namespace std;
#define Pi 2*acos(0.0)

//Overloading on volume for Sphere
long double volume(double r){
        r = r*r*r;
        return (4.0/3.0)*Pi*r;
}
//Overloading on voulme for Cylinder
long double volume(double r, double h){
        return Pi*r*r*h;
}

// sideArea for Calculating the Side Area of Cylinder
long double sideArea(double r, double h){
        return (2.0)*Pi*r*h;
}

// Overloadig on totalArea for Cylinder
long double totalArea(double r, double sideArea){
        r = r*r;
        return ((2.0)*Pi*r)+(sideArea);
}

// Overloading on totalArea for surface Area of Sphere
long double totalArea(double r){
        return (4.0)*Pi*r*r;
}

int main(){
        int x;
        cin>>x;
        if(x == 1){
                cout<<"For cylinder enter the value of radius and height respectively\n";
                long double rCy,hCy;
                cin>>rCy>>hCy;
                long double sideAreaCy, totalAreaCy, volumeCylinder;
                sideAreaCy = sideArea(rCy, hCy); // No function Overloading is used
                totalAreaCy = totalArea(rCy, sideAreaCy); // Using totalArea as Fucntion Overloading
                volumeCylinder = volume(rCy, hCy); //volume of Cylinder  ----> Function Overloading
                cout<<"Side Area of Cylinder : "<<sideAreaCy<<endl; // Print the side Area of Cylinder
                cout<<"Total Area of Cylinder : "<<totalAreaCy<<endl; // Print the total Area of Cylinder
                cout<<"Volume of Cylinder : "<<volumeCylinder<<endl; //Print the volume of Cylinder
        }else if( x == 2){
                cout<<"For Sphere enter the value of radius \n";
                long double rSp;
                cin>>rSp;
                long double surfaceArea,  volumeSphere;
                surfaceArea = totalArea(rSp); //Surface Area of Sphere Using totalArea Function -----> Function Overloading
                volumeSphere = volume(rSp); //Volume of Sphere using voulme Function ---------> Function Overloading
                cout<<"Surface Area of Sphere : "<<surfaceArea<<endl; //Print Surface Area of Sphere
                cout<<"Volume of Sphere : "<<volumeSphere<<endl; // Print the Volume of Sphere
        }else{
                cout<<"invalid choice error\n";
        }
        return 0;
}

Output


Related Solutions

For this C++ program, Write and modify the code to compute and display the class average...
For this C++ program, Write and modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows: 1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace the variable “double average” by “double average[NUMSTUDENTS].” This is necessary since you need to save the entered grades specially to compute the standard deviations. 2. After completing the display of the average grade of all...
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,...
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 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.
3. For this week’s assignment, you are asked to modify the above Java program to include...
3. For this week’s assignment, you are asked to modify the above Java program to include the following: - When the user clicks on the help menu, a drop-down list will appear with an item called About, then when the user clicks on it, the window will show some instructions about the functionality of the menu, e.g, what the edit menu does, etc. - When the user clicks on the File menu, a drop-down list will appear with one item...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall average test grade. E.g if there are 3 test each student must take and the user enters the following set of test scores for the two students…: 30, 40, 50 for the first student 50, 60, 70 for the second student …then program will print the average for each student (i.e. 40 for the first student and 60 for the second student – the...
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.
please complete the following program in c++: In this homework assignment you will modify the program...
please complete the following program in c++: In this homework assignment you will modify the program you have developed in Homework Assignment 1 by implementing the following: 1- Your program will ask the user to choose to either Compute and Display Surface Areas of Hemispheres and their Average (Choice 1) or to Compute and Display Volumes of Hemispheres and their Average (Choice 2). The program will only perform the chosen operation. (Hint: use a character variable to read the character...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT