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 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.
[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...
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...
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.  
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.
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Find the total volume, total surface area and the longest line for the following limits: 0...
Find the total volume, total surface area and the longest line for the following limits: 0 ≤ r ≤ 5, 0 ≤ θ ≤ π, 90° ≤ ϕ ≤ 200° 0 ≤ r ≤ 2, 20° ≤ θ ≤ 120° , 0 ≤ ϕ ≤ 180°
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT