Activity 3-4 - Function Overloading

"function name overloading is possible when you have different argument types, different number of arguments, or both"

In the program P33_2.cpp, 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.

// P34_1.cpp 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
using namespace std;

const double PI = 3.14159;  // This variable is defined globally, known to all functions in this program 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 <Enter> ";
      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.

Exercise 3.8
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.

Exercise 3.9
Modify program P34_1.cpp 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.

Call your new program Ex39.cpp.