/* This program demonstrates * 1. Function Overloading * 2. Default arguments * 3. const arguments * * Function Overloading * While it is not possible to have multiple variables with the same name in the * same scope, it is possible to have multiple functions with the same name * as long as * 1. They have a different number of parameters * 2. If they have the same number of parameters, then the two functions should * have at least 1 parameter of a different (incompatible) type. * 3. If they have the same number and types of parameters, then the ordering * of the parameters should provide a way to distinguish between the functions * If we fail on all 3, then then the compiler is unable to decide which function * is being called. This is called "ambiguous invocation" and it might not be a * syntax error, but it is not guaranteed to call the right function and might * give you the wrong answer. * * Default arguments * If we happen to have a function that takes a parameter, but that parameter * happens to have a particular value MOST (if not all) of the time, then we can * make the parameter default. * We make a parameter default by specifying a value for the parameter during * function DECLARATION. * If a function has a default parameter, it can be left out during the function * call. If a value is specified for the parameter, then the given value is used * in the function. If the parameter is left out, then the default value is used. * The default parameter should be at the very end of the parameter list. This * is because we cannot skip over parameters in a function call. For example, * we cannot have a function call like doStuff(par1, par2, , par3); * If we decide to leave out a parameter, it should be at the end. * * A function could have multiple default parameters, but they should all be at * the end of the parameter list. * Also, if a function has multiple default parameters, we can leave out all * of the parameters, or a continuous list of parameters from the end. * * We should also make sure the addition of a default parameter to an overloaded * function doesn't cause any ambiguous function calls. * * const Arguments * Sometimes, we do not want an argument to change within the function, irrespective * of whether it was passed by value or by reference. In such cases, the formal * parameter can be qualified as "const". This will prevent accidental changes to * the parameter in the function, even if the actual parameter in the place of call * were not a constant. */ #include #include using namespace std; // We declare 3 functions called "volume" // However, each of them takes a different number of parameters // 1. find the volume of a sphere - pi is a default argument here double volume(double radi, double pi = 3.14); /* In the previous line, we declare the function volume with pi as a default parameter. * If the function call in main passed a value for pi, the passed value is used. * If the function call left out the parameter that corresponds to the parameter * pi, then 3.141 is used as the value of pi. * Please note that pi is the last parameter. */ // 2. find the volume of a cylinder or cone double volume(double radius, double height, const double pi, char shape); /* In the previous line, we have marked the third parameter as "const". That makes * pi a constant for this function alone. * Unlike a default argument, the "const" qualifier has to be placed before the * parameter in both the function declaration and the function definition */ // 3. find the volume of a 4d rectangular prism double volume( double l, double w, double h, double d4); int main() { //demonstrating overloading, const and default arguments double radius, height; cout<<"Enter the radius: "; cin>>radius; cout<<"Enter height: "; cin>>height; double pi = 3.141; // variable here. Can be chnaged later if required // calls the sphere's volume function with the default parameter cout<<"Volume (pi - default) "<< volume (radius)<