Function Overloading in C++





  • C++ allows to create more than one function with the same name in the same scope. This mechanism is called function overloading.

  • In function overloading the functions name are same but the argument list of each function must be different.

  • To overload function there are three ways:

    1. Define a new function with the same name with different types of argument.
    2. Define a new function with the same name with different number of argument.
    3. Define a new function with the same name with different types and different number of argument.

NOTE: Return type of function, doesn't play any role in function overloading.

Example:

#include<iostream>
using namespace std;

void add(int x , int y) {
int sum = x + y;
cout<<"RESULT : ";
cout<< sum<< endl;
}

void add(int x , float y) {
float sum = x + y;
cout<<"ANSWER : ";
cout<< sum<< endl;
}

void add(int x , int y, int z) {
int sum = x + y + z;
cout<<"SUM : ";
cout<< sum<< endl;
}
int main() {
add(10,20);
add(10,12.3f);
add(10,20,30);
return 0;
}
OUTPUT :
RESULT : 30
ANSWER : 22.3
SUM : 60 

In the above example you can see there are three function with same name add with different arguments. At the time of function call it automatically mapped with suitable function and call that function.

Next topic is default argument





 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved