-
Notifications
You must be signed in to change notification settings - Fork 7
/
functionOverloading.cpp
50 lines (40 loc) · 1.86 KB
/
functionOverloading.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int sum(int, int);
int sum(int, int, int);
int main() {
/*
In C, we could not have more than one function having the same name
In C++ we can do so using function overloading
Function Overloading:
--> More than one function with same name and difference in arguments (and they need to follow some rules obviously)
--> Compile time polymorphism
IMP: int sum(int,int);
float sum(int,int);
This will give error. Type or count of arguments has to be different
One of the important tasks of compiler is to bind function call with the actual function definition.
This is called Early Binding.
Steps:
1. When the compiler reaches a function call line, it checks the function name of the function call.
2. All the functions defined in the program with the same name are candidates for being binded.
3. Based on some rules it then checks which function to bind
Rules:
1. The compiler tries to find exact match to function call (same number and type of arguments)
2. If no match is found, it tries to find match by promotion:
char, unsigned char, short --> Promoted to int
float --> Promoted to double
3. If, no match is found, it tries to find match via standard conversion
If the function call does not fit in any of the rules, then an error occurs.
*/
int a = 2, b = 3, c = 5;
cout << "Sum of " << a << " and " << b << " is: " << sum(a, b) << endl; // Follows rule 1
cout << "Sum of " << a << ", " << b << " and " << c << " is: " << sum(a, b, c) << endl; // Follows rule 1
cout << "Sum of 'a' & 'b' is:(Prints sum of ASCII) " << sum('a', 'b') << endl; // Follows rule 2
cout << "Sum of 1.1 & 2.9 is:(Prints sum of floor values) " << sum(1.1, 2.9) << endl; // Follows rule 3
}
int sum(int x, int y) {
return x + y;
}
int sum(int x, int y, int z) {
return x + y + z;
}