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

Reflection of female hacker coding working hackathon at laptop
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:

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

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:

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.
mla apa chicago
Your Citation
Bolton, David. "Let's Examine Overloading in C/C++/C#." ThoughtCo, Apr. 5, 2023, thoughtco.com/verloading-in-c-candand-c-958121. Bolton, David. (2023, April 5). Let's Examine Overloading in C/C++/C#. Retrieved from https://www.thoughtco.com/verloading-in-c-candand-c-958121 Bolton, David. "Let's Examine Overloading in C/C++/C#." ThoughtCo. https://www.thoughtco.com/verloading-in-c-candand-c-958121 (accessed June 9, 2023).