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

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.
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.
Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
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...
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m...
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m absorbed by a rabbit, where the rabbit gets 2 pills containing n1 and n2 grams of medicine, respectively, of which the rabbit absorbs 60% and 35%, respectively, and n1 and n2 are input by a user via the keyboard.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT