CPP

Function Overloading  in C++

  • You can have multiple definitions for the same function name in the same scope.
  • The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
  • You cannot overload function declarations that differ only by return type.

Example:

#include <iostream>

class printData
{
 public:
 void print(int i) {
 cout << "Printing int: " << i << endl;
 }
 void print(double f) {
 cout << "Printing float: " << f << endl;
 }
void print(char* c) {
 cout << "Printing character: " << c << endl;
 }
};

void main(void)
{
 printData pd;
 // Call print to print integer
 pd.print(9);
 // Call print to print float
 pd.print(100.200);
 // Call print to print character
 pd.print("Hello C++");
 getch();
}

Output:

Printing int: 9
Printing float: 100.200
Printing character: Hello C++

 




Subscribe us on Youtube

Share This Page on


Ask Question