Function overloading in C++
What is function overloading in C++
On this page we will discuss about function overloading in C++ .Function overloading is the process in which we use the same name for two or more functions defined in the class. The only difference between these functions is that they have different types of parameters or a different number of parameters.Function Overloading in C++ Language
- A function is a set of statements that allow us to structure program in segments of code to perform individual tasks.
- For example a function created as ”int addition(int a, int b)” can be used anywhere in our program where we need to add two integer type numbers.
- Function overloading is the process in which we use the same name for two or more functions defined in the class.
- The only difference between these functions is that they have different types of parameters or a different number of parameters.
- We have to use different type or different number of arguments because it is the only way by which compiler can differentiate between two or more overloaded function.
- It is done at compile time hence it is also known as compile time polymorphism.
Advantages of function overloading in C++
As we have discussed above what is function overloading, now lets look at some of its advantage :-
- Using the function overloading concept, we can make more than one function with the same name
- Function overloading helps us to save the memory space, consistency, and readability of our code.
- Function overloading speeds up the execution of our code.
- Function overloading helps the application to load the class method based on the type of parameter.
- Function overloading makes code re-usability easy, thus it also helps to save memory.
- Function overloading makes code maintenance easy.
C++ programming code to show function overloading
#include <iostream> using namespace std; int area(int s) { cout<<"Area of square="<<s*s <<endl; return 0; } int area(int l, int b)//Second definition. { cout<<"Area of reactangle=" <<l*b<<endl; return 0; } int main() { area(12);//Passing value for first defination. area(20,10);//Passing value for second defination. }
Area of square=144 Area of rectangle=200
In the above program, we have defined area() function two times. First function to calculate area of square using one integers value as parameters , second function to calculate area of rectangle using two integer value as parameters. In this way we have overloaded area() function by using different number of parameters.
Disadvantages of function Overloading in C++
After learning things about function overloading lets look at some of its disadvantage:-
- Function overloading process cannot be applied to the function whose declarations differ only by its return type
void area() int area(); // error
Member function declared with the same name and the same parameter types cannot be overloaded if any one of them is a static member function declaration.
static void area(); void area(); // error
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment