When we first introduced constructors we gave sample code with a few constructors with different arguments...
energyBill();
energyBill(double, double, int, string, int);
energyBill(const energyBill &);
This is an example of function overloading. Functions are overloaded when they have the
same name but different argument lists. The parameters given to a function determine which one to use.
Function overloading isn’t just for constructors...
int getMax( int x, int y );
char getMax( char first, char second );
double getMax( double red, double blue );
string getMax( string first, string second );
int mxNum = getMax(19, 4);
// calls int getMax(int, int );
char mxChar = getMax('A', 'V'); // calls char getMax(char, char);
The definitions/logic of the functions are supposed to be different.
If they were identical, it may be easier to use function templates instead. Later ...
The overloading is tied to the name and the argument lists have to differ.
There will be problems if:
We provide default values for the function arguments so don’t need them when we call ...
void calculation(int num = 1);
void calculation(char ch = 'A');
We have two functions with the same name and argument list, but different return types.
int getMax( int x, int y );
float getMax( int x, int y );