Function Overloading says, when two or more functions have the same name but different parameters.
Below are two types of difference in parameters that C++ considers for function overloading :
Now, let's see the first way of operator Overloading.
int add(int firstNumber, int secondNumber, int thirdNumber)
int add(int firstNumber, int secondNumber)
int add(int firstNumber)
#include <iostream> using namespace std; //Add function starts here. int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; } //Overloaded Add function starts here. int add(int firstNumber, int secondNumber, int thirdNumber) { int result; result = firstNumber + secondNumber + thirdNumber; return result; } int main() { int firstNum = 5; int secondNum = 8; int thirdNum = 2; int addedResult1; int addedResult2; addedResult1 = add(firstNum,secondNum); cout << "The added value of two numbers is : " << addedResult1 << endl; addedResult2 = add(firstNum,secondNum,thirdNum); cout << "The added value of three numbers is : " << addedResult2 << endl; return 0; }
addedResult1 = add(firstNum,secondNum);
addedResult2 = add(firstNum,secondNum,thirdNum);
float add(float firstNumber, float secondNumber)
int add(int firstNumber, int secondNumber)
#include <iostream> using namespace std; //Add function starts here. int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; } //Overloaded Add function starts here. float add(float firstNumber, float secondNumber) { float result; result = firstNumber + secondNumber; return result; } int main() { int firstNum = 5; int secondNum = 8; float firstNumFloat = 2.4; float secondNumFloat = 9.5; int addedResult1; float addedResult2; addedResult1 = add(firstNum,secondNum); cout << "The added value of two numbers is : " << addedResult1 << endl; addedResult2 = add(firstNumFloat,secondNumFloat); cout << "The added value of decimal numbers : " << addedResult2 << endl; return 0; }
int add(int firstNumber, int secondNumber)
float add(float firstNumber, float secondNumber)
addedResult1 = add(firstNum,secondNum);
int add(int firstNumber, int secondNumber)
addedResult2 = add(firstNumFloat,secondNumFloat);
float add(float firstNumber, float secondNumber)