C++ Function Overloading | Example

With the help of examples, we will learn about function overloading in C++ in this tutorial.

 Function Overloading
  • The word “Polymorphism” is made up of the words “poly” and “morphs,” both of which denote multiple forms. It is a Geek Word
  • Three key ideas are used in object-oriented programming: polymorphism, encapsulation, and inheritance.
  • Real-World Illustration of Polymorphism

Let’s look at a polymorphism case study from the real world.

In a classroom, a woman conducts herself as a teacher; in a home, as a mother or daughter; and in a market, as a customer. In this case, a single person is acting differently depending on the circumstances.

Function Overloading

Overloading in C++ is the process of creating two or more members with the same name but different numbers or types of parameters.

We can overload in C++.

  • Methods
  • Constructors
  • Indexed properties

It’s because these members only have certain parameters.

In C++, overloading comes in several forms

  1. function overloading
  2. Operator overloading

Function Overloading

  • The feature of object-oriented programming known as “function overloading” allows for the existence of multiple functions with the same name but different parameters.
  • The act of adding additional tasks to a function name is known as function overloading.
  • When using function overloading, the “Function” name and the arguments ought to be distinct.
  • These differences are the only ones that the compiler can use to distinguish between the functions.
  • In C++, function overloading is an example of a polymorphism feature.
  • Function overloading has the benefit of improving program readability by eliminating the need for multiple names for the same action.

For example:

// same name different arguments
int geektocode() { }
int geektocode(int a) { }
float geektocode(double a) { }
int geektocode(int a, double b) { }

Here, each of the four functions is overloaded.
You’ll notice that none of these 4 functions have the same return types. Different return types may or may not be present in overloaded functions, but distinct arguments are a requirement. For instance,

// Error code
int geektocode(int a) { }
double geektocode(int b){ }

Both of these functions are identical in terms of name, type, and the number of arguments. The compiler will therefore produce an error.

Example 1: Overloading Using Different Types of Parameters

using namespace std;
class over{
	void add(int a , int b){
		cout<<"\nAddition of "<<a<<"+"<<b <<"="<<a+b;
	void add(int a, int b, int c){
		cout<<"\nAddition of "<<a<<"+"<<b<<"+"<<c <<"="<<a+b+c;
int main(){
	int x,y,z,v,e;
	cout<<"\nEnter first Number:";
	cout<<"Enter Second Number:";
	over obj;
	cout<<"\n\n\n-----FOR THREE DIGIT ADDITION-----";
	cout<<"\n\nEnter first Number:";
	cout<<"Enter Second Number:";
	cout<<"Enter Third Number:";
Enter first Number:77
Enter Second Number:99

Addition of 77+99=176


Enter first Number:55
Enter Second Number:77
Enter Third Number:88

Addition of 55+77+88=220

NOTE: Many of the functions in the standard library in C++ are overloaded. The sqrt() function, for instance, accepts double, float, int, etc. as parameters. Due to C++’s overloaded sqrt() function, this is feasible.

Leave a Reply

Your email address will not be published. Required fields are marked *