Function overloading
Function Overloading means two or more functions
can have the same name.
The number of arguments and the data type of the
arguments will be different.
When a function is called it is selected based on
the argument list.
Example:
#include <iostream> using namespace std; int add(int a, int b, int c) { return(a + b + c); } //function overloading same name but different argument float add(float d, float e) { return (d + e); } int main() { int a,b,c; float d,e,sum; cout << "Enter three integers\n"; cin >> a >> b >>c; sum = add(a, b, c); cout << "Sum of integers is " << sum << "\n"; cout << "Enter two floating point numbers\n"; cin >> d >> e; sum = add(d, e); cout << "Sum of floating point numbers is " << sum << "\n"; return 0; }
Output:- Enter three integers
2
2
2
Sum of integers is 6
Enter two floating point numbers
2.5
2.5
Sum of floating point numbers is 5
Difference between Overloading and overriding
Overloading can occur without inheritance
Overriding occurs when one class is inherited from another
In overloading the arguments and the return type must differ
In overriding, the arguments and the return type must be the same
In overloading the function name is the same, But it behaves differently depending on the arguments passed to them
In overriding the function name is same, but Derived class function can perform different operations from the base class
0 Comments