With the help of examples, we will learn about function overloading in C++ in this tutorial.
- 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.
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
- function overloading
- 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
#include<iostream>
using namespace std;
class over{
public:
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:";
cin>>x;
cout<<"Enter Second Number:";
cin>>y;
over obj;
obj.add(x,y);
cout<<"\n\n\n-----FOR THREE DIGIT ADDITION-----";
cout<<"\n\nEnter first Number:";
cin>>v;
cout<<"Enter Second Number:";
cin>>e;
cout<<"Enter Third Number:";
cin>>z;
obj.add(v,e,z);
}
Output:
Enter first Number:77
Enter Second Number:99
Addition of 77+99=176
-----FOR THREE DIGIT ADDITION-----
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.