Function Overloading with an Example in C++ – to find the sum
In this tutorial, we will Learn:
What is Function Overloading in C++?
Write a C++ program to define three overloaded functions to find the sum of two integers, sum of two floating-point numbers, and the sum of three integers.
Solution:
Video Tutorial:
Function overloading means, two or more functions have the same names but different argument lists.
The arguments may differ in the type of arguments or number of arguments, or both.
However, the return types of overloaded methods can be the same or different is called function overloading.
Function overloading conceptshelps us to use the same function names multiple time in the same program.
Following program demonstrates the overloaded function to find the sum of two integers, sum of two floating-point numbers, and the sum of three integers.
#include<iostream> using namespace std; int sum(int x, int y) { return x+y; } double sum(double x, double y) { return x+y; } int sum (int x, int y, int z) { return x+y+z; } int main() { cout <<"The Sum of two integers: "<<sum(10, 20)<<endl; cout <<"The Sum of two floats: "<<sum(10.5, 20.7)<<endl; cout <<"The Sum of three integers: "<<sum(10, 20, 30)<<endl; }
OUTPUT:
The Sum of two integers: 30
The Sum of two floats: 31.2
The Sum of three integers: 60
Summary:
In this article, we understood Function Overloading in C++ to find the sum and wrote an overloaded function to find the sum of two integers, the sum of two floating-point numbers, and the sum of three integers.
If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.