Same data types and count but signature different
Function overloading when function data types are same, count is also same but signature is different.
Learning Objectives
- Understanding overloading with same type and count of parameter with different signature due to different order of parameters.
Source Code
|
Run Output
Code Understanding
void action(char c,int n) {
cout<<“Output from char c, int n”<<endl;
for(int i=0;i<n;i++) cout<<c;
cout<<endl; }
Function with two data inputs with signature as char as 1st and int as 2nd parameter.
void action(int n,char c) {
cout<<“Output from int n, char c”<<endl;
for(int i=0;i<n;i++) cout<<c;
cout<<endl; }
Function with two data inputs with signature as int as 1st and char as 2nd parameter. So the signature is different and there is not ambiguity in calling a function.
int main() {
action(‘*’,5);
This will call the function with char 1st and int as 2nd parameter
action(5,’*’);
This will call the function with int 1st and char as 2nd parameter
return 0;
}
Notes
- The signature of a function means specification of parameters related to its type, count and exact order from left to right.
- Function with similar data types but different order of parameters may be avoided if it can lead to confusion while implementation.
Suggested Filename(s): func-ol4.cpp
sunmitra| Created: 26-Aug-2018 | Updated: 28-Aug-2018|