-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathFunction_Overloading.cpp
47 lines (37 loc) · 1.05 KB
/
Function_Overloading.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
#include <bits/stdc++.h>
using namespace std;
//Very simple examples are used for better understanding the concepts
void add(int a, int b) {
cout << a + b << endl;
}
void add(double a, double b) {
cout << a + b << endl;
}
void add(int a) {
cout << 1 + a << endl;
}
/*
ALERT !!! (ERROR)
int add(int a) {
return a+1;
}
*/
int main() {
/*
Why Function Overloading (Static Polymorphism) is required at all ?
Ans: Same functionality but takes different argument types
Overload Resolution ?
Ans: Function selection by compiler is called Overload Resolution (done in compile time)
Note:
- Binding happens at compile time.
- Two functions having same signature but different return types cannot be overloaded (ALERT !!!)
- Overloading allows "Static Polymorphism" (Explained in Polymorphism section)
Types :
- Same # of parameters but of different types
- Different # of parameters
*/
add(1, 2);
add(1.3, 2.3);
add(3);
return 0;
}