Function overloading is a concept in which one can use many functions having same function name but can pass different number of parameters or different types of parameters.
Overloading
Functions
Definition :
Function overloading is a concept in which one can use many functions having same
function name but can pass different number of parameters or different
types of parameters.
Following
are the rules for function overloading ‒
1.
The overloaded functions may differ by number of parameters.
2.
The overloaded functions may differ by data types.
3.
The same function name is used for various instances of function call.
For
example, if there is a function sum which performs addition operation then one
can use overloading functions like this ‒
int
sum(int a,int b);
int
sum(int a,int b,int c);
int
sum(int a,int b,int c,int d);
Similarly
the function overloading can be achieved like this ‒
int
sum(int a,int b);
float
sum(float a,float b);
double
sum(double a,double b);
char
sum(char a,char b);
That
means we can handle different number of parameters or different types of
parameters using the same function name.
Following
program takes different number of parameters and performs the task of addition.
Note that name of these functions is same and that is sum.
#include <iostream>
using namespace std;
class test {
private:
int a,b,c,d;
public:
int sum(int,int);
int
sum(int,int,int);
int sum(int,int,int,int);
};
int test::sum(int a,int b)
{
return (a+b);
}
int test::sum(int a,int b,int c)
{
return (a+b+c);
}
int test::sum(int a,int b,int c,int d)
{
return (a+b+c+d);
}
int main()
{
test obj;
int a, b, c, d,choice;
int result=0;
cout<<"\n\t\t Main Menu";
cout<<"\n\t 1.Addition of two numbers";
cout<<"\n\t 2.Addition of three numbers";
cout<<"\n\t 3.Addition of four numbers"
<endl;
cout<<"\n Enter Your choice: ";
cin >> choice;
switch(choice)
{
case 1: cout
<<"\nEnter 2 numbers: ";
cin
>> a >> b;
result =
obj.sum(a,b); // Overloading Functions
break;
case 2: cout
<<"\nEnter 3 numbers: ";
cin
>> a >> b >> c;
result =
obj.sum(a,b,c); // Overloading Functions
break;
case 3:
cout<<"\nEnter 4 numbers: ";
cin
>> a >>b>>c>> d;
result =
obj.sum(a,b,c,d); // Overloading Functions
break;
default:
cout<<"\nWrong Choice";
break;
}
cout<<"\n\nresult: << result << endl;
return 0;
}
Output
Main Menu
1.Addition of two
numbers
2.Addition of three
numbers
3.Addition of four
numbers
Enter Your choice: 2
Enter 3 numbers: 10 20
30
result: 60
•
Now following program will take the parameters of different data types. This makes
the function to overload.
C++ Program
#include <iostream>
using namespace std;
class test {
public:
int sum(int,int);
float sum(float,float);
double
sum(double,double);
};
int test::sum(int a,int b)
{
return (a+b);
}
float test::sum(float a,float b)
{
return (a+b);
}
double test::sum(double a,double b)
{
return (a+b);
}
int main()
{
test obj;
int choice;
int a,b;
float x,y;
double m,n;
double result=0;
cout<<"\n\t\t Main Menu";
cout<<"\n\t 1.Addition of two integer numbers";
cout<<"\n\t 2.Addition of float numbers ";
cout<<"\n\t 3.Addition of double
numbers"<<endl;
cout<<"\n Enter Your choice: ";
cin >> choice;
switch(choice)
{
case 1: cout
<<"\nEnter 2 numbers: ";
cin >> a
>> b;
result =
obj.sum(a,b);
break;
case 2: cout
<<"\nEnter 2 numbers: ";
cin >> x
>> y;
result =
obj.sum(x,y);
break;
case 3: cout
<<"\nEnter 2 numbers:
cin >> m
>> n;
result = obj.sum(m,n);
break;
default: cout
<<"\nWrong Choice";
break;
}
cout<<"\n\nresult: " << result <<
endl;
retum 0;
}
Output
Main Menu
1.Addition of two
integer numbers
2.Addition of float
numbers
3.Addition of double
numbers
Enter Your choice: 2
Enter 2 numbers: 1.13 5.6
result: 6.73
Example: 1
Write a C++ program that can take either two
integers or two floating point. numbers and outputs the smallest number using
class, function overloading.
Solution:
#include <iostream>
using namespace std;
class Test
{
public:
int
smallest(int,int);
float
smallest(float,float);
};
int Test::smallest(int
a,int b)
{
if(a<b)
return a;
else
return b;
}
float Test::smallest (float x,float y)
{
if(x<y)
return x;
else
return y;
}
int main()
{
Test obj;
class choice;
int a,b;
float x,y;
cout<<"\n Menu"<<endl;
cout<<"\n 1. Integers \n 2.Float "<<endl;
cout<<"\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n
Enter two numbers: "<<endl;
cin>>a;
cin>>b;
cout<<"Smallest
Number is :"<<obj.smallest(a,b);
break;
case 2:
cout<<"\n Enter two numbers:
"<<endl;
cin>>x;
cin>>y;
cout<<"Smallest
Number is: "<<obj.smallest(x,y);
break;
}
return 0;
}
Output
Menu
1. Integers
2. Float
Enter your choice: 1
Enter two numbers:
11
7
Smallest Number is: 7
Example:2
Design calculator using function overloading.
Solution:
#include<iostream>
#include<cstdlib>
using namespace std;
class Calculator
{
public:
int addition(int&,int);
float addition(float&, float);
int subtraction(int&,int);
float subtraction(float&, float);
int Multiplication(int&,int);
float Multiplication(float&, float);
int division(int&,int);
float division(float&, float);
};
void getdata(float& result, float temp);
float result,temp;
char sign;
int Calculator::addition(int& result,int temp)
{
result=result+temp;
return result;
}
float Calculator::addition(float& result, float temp)
{
result=result+temp;
return result;
}
int Calculator::subtraction(int& result,int temp)
{
result=result‒temp;
return result;
}
float Calculator::subtraction (float& result, float temp)
{
result=result‒temp;
return result;
}
int Calculator::Multiplication(int& result,int temp)
{
result=result*temp;
return result;
}
float Calculator::Multiplication(float& result, float temp)
{
result=result*temp;
return result;
}
int Calculator::division(int& result,int temp)
{
result=result/temp;
return result;
}
float Calculator::division (float& result, float temp)
{
result=result/temp;
return result;
}
void getdata(float& result, float temp)
{
Calculator obj;
char oper="\0';
cout<<"\n\t\t
Calculator Program";
cout<<"\n
Enter some expression";
cout<<"\n
First of all enter some number then enter an operator and then some
number.";
cout<<"\n
For obtaining the result type = and then press Enter";
cout<<"\n
For Quitting the program type q or Q";
cin >> result;
while(1)
{
cin >>oper;
if((oper=='q')||(oper=='Q'))
break;
if (oper= = '=')
{
result;
cout << result << endl;
}
else
{
cin
>>temp://reading second operand for continuing the expression
}
switch(oper)
{
case '+':
result=obj.addition(result,temp);
result=result;
break;
case ':
result=obj.subtraction(result,temp);
result=result;
chat break;
case '*':
result=obj.Multiplication(result,temp);
result=result;
break;
case '/':
result=obj.division(result,temp);
result=result;
break;
}
}
}
void main()
{
float val, first_num;
clrscr();
getdata(val,
first_num);//function for entering expression
}
Output
Calculator Program
Enter some expression
First of all enter
some number then enter an operator and then some number.
For obtaining the
result type = and then press Enter
For Quitting the
program type q or Q
2+3=
5
*7=
35
q
Example:
3
Illustrate
the concept of function overloading to find the maximum of two numbers.
Solution :
class test
{
public:
int max(int,int);
double
max(double,double);
};
int test::max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
double test::max(double a,double b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
test obj;
cout<<"\n
The maximum number is: "<<obj.max(10,20);
cout<<"\n The maximum number is:
"<<obj.max(11.11,2.22);
}
Example: 4
Write a
C++ program to find area of the square, rectangle, circle using function
overloading.
Solution :
#include<iostream>
using namespace std;
void area(int r)
{
double a = 3.14*r*r;
cout <<
"\n The area of circle is: " << a;
}
void area(int 1, int b)//overloaded function
{
int a;
a = 1*b;
cout <<
"\n The area of rectangle is: " << a;
}
void area(double val)//overloaded function
{
double a = val*val;
cout << "\n The area of square
is: << a;
}
void main()
{
int choice;
int radius, length, breadth;
double side;
char ans = 'y';
cout << "\n Program for Demonstrating function
overloading";
do
{
cout <<
"\n 1. Area of Square \n 2. Area of Rectangle \n 3. Area of Circle";
cout <<
"\n Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:cout
<< "\n Enter side: ":
cin
>> side;
area(side);
break;
case 2:cout
<< "\n Enter length: ";
cin
>> length;
cout
<< "\n Enter breadth: ";
cin
>> breadth;
area(length,
breadth);
break;
case 3:cout
<< "\n Enter radius:
cin
>> radius;
area(radius);
break;
}
cout << "\n Do you want to continue?(y/n) ";
cin >> ans;
} while (ans= = 'y');
}
Output
Program for
Demonstrating function overloading
1. Area of Square
2. Area of Rectangle
3. Area of Circle
Enter your choice: 1
Enter side: 11.11
The area of square is:
123.432
Do you want to
continue?(y/n) y
1. Area of Square
2. Area of Rectangle
3. Area of Circle
Enter your choice: 2
Enter length: 10
Enter breadth: 20
The area of rectangle
is: 200
Do you want to continue?(y/n)
y
1. Area of Square
2. Area of Rectangle
3. Area of Circle
Enter your choice: 3
Enter radius: 5
The area of circle is:
78.5
Do you want to
continue? (y/n) n
Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Overloading Functions
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation