Question:- overloding inc++.
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.
The parameters should follow any one or more than one of the following conditions for Function overloading:
Parameters should have a different type
add(int a, int b)
add(double a, double b)
Below is the implementation of the above discussion:
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{ add(10, 2);
add(5.3, 6.2);
return 0;
}
Output
sum = 12
sum = 11.5
Parameters should have a different number
add(int a, int b)
add(int a, int b, int c)
Below is the implementation of the above discussion:
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(int a, int b, int c)
{
cout << endl << "sum = " << (a + b + c);
}
// Driver code
int main()
{
add(10, 2);
add(5, 6, 4);
return 0;
}
Output
sum = 12
sum = 15
Comments
Post a Comment