- : Overloading in C++ :-
When two or more members having same name but different parameters or datatypes are called Overloading in C++.
There are two types of overloading in C++
1) Function Overloading
2) Operator Overloading
[Note :- We Can Overload METHODS, CONSTRUCTORS and INDEX PROPERTIES.]
1) Function Overloading
When Two or More Functions having same FunctionName but different numbers or types parameters is called Function Overloading. Function with same name can be redefined by either different types(DataType) of parameters or different number of parameters.
These differences in parameters or datatypes are made for compiler to differentiate the function even they are having same FunctionName.
Let's Understand the Function Overloading through an example :-
_____________________________________________________________
A program for Function Overloading with different number of parameters
#include<iostream>
using namespace std ;
void Add(int X, int Y) {
cout<< X+Y <<endl ;
}
void Add(int X, int Y, int Z) {
cout<< X+Y+Z <<endl ;
}
int main() {
Add(10,20) // call function, having 2 parameters.
Add(10,20,50) ; //call function, having 3 parameters.
return 0 ;
}
_____________________________________________________________
Output :-
30
80
_____________________________________________________________
Output:-
_____________________________________________________________
A program for Function Overloading with different types (DataType) of parameters
#include<iostream>
using namespace std ;
void Add(int X, int Y) {
cout<< X+Y <<endl;
}
void Add(double X, int Y) {
cout<< X+Y <<endl ; ;
}
int main() {
Add(10,20)
Add(10.7,20)
return 0 ;
}
_____________________________________________________________
Output :-
30
30.7
_____________________________________________________________
Output:-
_____________________________________________________________