C++ Overloading

In C++, we can easily create more than one function with a similar name but different parameters. Let’s say we have the following function:

We can create more functions with the same name:

Above, we have different types and numbers of parameters for functions with similar names.

The 1st will add two integers and the 2nd two doubles. This is called Function Overloading. For the same action i.e., addition here, by overloading, we do not need to use different function names.

Let us see two examples:

  • We will add numbers of different types (int and double) when the number of parameters is the same
  • We will add integers when the number of parameters is different:

Add numbers of different types (int and double)

Let us see the 1st example. We will overload the demoAdd() function and add two integers and then two doubles:

Output

Add integers of similar types but different counts of parameters

Let us see another example where we will add integers when the count of parameters is different:

Output

C++ Pointers
C++ File Handling
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading