In: Computer Science
in C++
Compile and run the program for the following values: r = 2 Cm, h = 10 Cm.
The answer should be: The cross section area of the cylinder is 3.8955634 c
The side area of the cylinder is 19.474819 inch-sqr
Did you get the same answer? Explain the reason for such an error and fix the problem.
Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The total surface area is the sum of side area and cross section area. For the above test values the total area must be: 23.370382 inch-sqr In the above program, we used two different function names to distinguish between the function that computes the cross area and the one that computes the side area of a cylinder. Using overloading we can give both functions the same name but ask them to do two different things. The decision on which function to be chosen is made based on: 1) difference in the number of arguments, 2) difference between types of parameters, and 3) based on the difference in number and type of parameters (both 1 and 2). Here is the new version of the same program written using overloading.
// This program illustrates the local and global variables and call-by-value.
// This program computes the side area and the cross section area of a cylinder
#include
#include
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions as PI
const double conversion = 0.3937; // This is the Cm to inch conversion factor
double area(double r); // Function declaration for function that computes cross section area
double area(double r, double h); // Function declaration for function that computes side area
int main(void) { double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << area(r) << " inch-sqr endl;
cout << "The side area of the cylinder is " << area(r,h) << " inch-sqr \n\n";
return 0;
}
double area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * conversion; // converting r to inch return 2*PI*pow(r,2);
}
double area(double r, double h)
{
double area; //variable local to Side_area function
h = h * conversion; // converting h to inch
r = r * conversion; // converting r to inch
area = 2*PI*r*h;
return area;
}
Note that we were able to use name overloading because of the fact that to compute cross section area, we only needed radius, r, as an argument and to compute the side area, we needed both the radius and height of the cylinder. Thus, here we used the difference between number of parameters to implement name overloading. Could we use overloading to compute the surface area and volume of a sphere? Explain your answer. The surface area of a sphere is S = 4*PI*r2 and the volume is V = (4.0/3.0)*PI*r3.
The surface area of a sphere is S = 4*PI*r2 and the volume is V = (4.0/3.0)*PI*r3.
lets simply name the function as calculate();
The usage of overloading depends on the number of paramaters and its types.Hence we could draft three cases whether name overloading can be done or not.
CASE 1 : Only 'r' radius as a parameter with the same type ie double.
double calculate(double r); --> surafce area
double calculate(double r); --> volume
"The overloaded function definitions has to be either differed by number of formal paramaters or its types".
If r with double type alone is used as formal parameter for two different definitions there will be confusion in executing the proper definitions.
Hence not possible.
CASE 2 : Only 'r' radius as a parameter with the different type ie one take double and other take int.
double calculate(int r); --> surafce area
double calculate(double r); --> volume
"The overloaded function definitions has to be either differed by number of formal paramaters or its types".
Here the number of paramaters is same for both the definitions ie 1,
But the type of the parameter differs one is int and other is double.
Therefore at this case the name over loading is possible.
CASE 3 : One more parameter in addition to 'r' radius as a parameter.
Lets assume we pass the pi value also as a parameter for any one of the definitions.
double calculate(double r,double pi); --> surafce area
double calculate(double r); --> volume
"The overloaded function definitions has to be either differed by number of formal paramaters or its types".
Here we have different number of formal parameters for the different name of the functions.
Hence the name overloading is possible at this case.