Overloading refers to the use of same thing for different purposes. C++ also permits overloading of functions. This means that we can use the same function name to create functions that perform a variety of different tasks. This is know as function polymorphism in OOP.
Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.
Function overloading can be considered as an example of polymorphism feature in C++.
Following is a simple C++ example to demonstrate function overloading.
Following is a simple C++ example to demonstrate function overloading.
Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.
Function overloading can be considered as an example of polymorphism feature in C++.
Following is a simple C++ example to demonstrate function overloading.
Following is a simple C++ example to demonstrate function overloading.
#includeusing namespace std; void display(int i) { cout << " Here is int " << i << endl; } void display(double f) { cout << " Here is float " << f << endl; } void display(char const *c) { cout << " Here is char* " << c << endl; } int main() { display(10); display(10.10); display("ten"); return 0; }
The output for above program as follows
Here is int 10
Here is float 10.1
Here is char* ten
A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.
In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action.
1. C++ program for function overloading with no of parameters vary
2. C++ program for function overloading with different parameter types
A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.
In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action.
1. C++ program for function overloading with no of parameters vary
#includeusing namespace std; class Cal { public: static int add(int a,int b){ return a + b; } static int add(int a, int b, int c) { return a + b + c; } }; int main(void) { Cal C; // class object declaration. cout< }
#includeusing namespace std; int mul(int,int); float mul(float,int); int mul(int a,int b) { return a*b; } float mul(double x, int y) { return x*y; } int main() { int r1 = mul(6,7); float r2 = mul(0.2,3); std::cout << "r1 is : " <
Function Overloading and Ambiguity
When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.When the compiler shows the ambiguity error, the compiler does not run the program.
Causes of Function Overloading:
- Type Conversion.
- Function with default arguments.
- Function with pass by reference.
We will discuss the above topics in next post...we visiting
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.