Let's Examine Overloading in C/C++/C#

Programmers overload functions, operators and methods

Reflection of female hacker coding working hackathon at laptop
A hacker works to Overload a computer in C++. Hero Images / Getty Images

Function overloading allows functions in computer languages such as C, C++, and C# to have the same name with different parameters. Operator overloading allows operators to work in the same manner. In C#, method overloading works with two methods that accomplish the same thing but have different types or numbers of parameters.

An Example of Function Overloading

Rather than have a differently named function to sort each type of array, such as:

Sort_Int( Int Array Type) ;
Sort_Doubles( Double Array Type) ;

You can use the same name with different parameter types as shown here:

Sort( Int Array Type ) ;
Sort( Double Array Type ) ;

The compiler is then able to call the appropriate function depending on the parameter type. Overload resolution is the term given to the process of selecting the appropriate overload function. 

Operator Overloading

Similar to function overloading, operator overloading allows programmers to redefine operators such as +, - and *. For example, in a class for complex numbers where each number has a real and imaginary part, overloaded operators allow code such as this to work:

complex c = a + b;

As long as + is overloaded for the type complex.

Advantages of Overloading When Writing Code

  • You end up with code that is easier to read
  • Overloading is convenient and intuitive
  • Avoids clunky syntax 
  • Consistency in naming and notation
  • Works well in templates and other constructs when you don't know the variable type at the time you are writing the code.