Tuesday, 21 April 2026

FUNCTION OVERLOADING in C++

 

FUNCTION OVERLOADING in C++
Function overloading is a logical method of calling several functions with different arguments and data types that perform basically identical things by the same name. The main advantages of using function overloading are:
  • eliminating the use of different function names for the same operation.
  • helps to understand, debug and grasp easily.
  • easy maintainability of the code.
  • better understanding of the relation between the program and the outside world.
Function overloading is an easy concept in C++ and generally it is used within the class concept for member functions and constructors. The compiler classifies the overloaded function by its name and the number and type of arguments in the function declaration. The function declaration and definition is essential for each function with the same function name but with different arguments and data types.
    For example, consider the following swap( ) function which is used to swap the different data types such as int, oat and char. In order to carry out the swapping of these data types, it is required to write three distinct functions with different names, i.e., one function for swapping integer values, one for floating point numbers and another one for character data types. The function declaration, definition and calling of each function is also different. Each function swap( ) must have a different definition such that the compiler can choose the correct function.
Some steps are:
1. A function for swapping two integers is,
void swap_int (int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}

2.A function for swapping two integers is,
void swap_int (int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}

3.A function for swapping two character data types is,
void swap_char (char &a,char &b)
{
char temp;
temp = a;
a = b;
b = temp;
}

Example 1.
A program to perform the swapping of two data items of integer, floating point numbers and character types without function overloading.

// functions without overloading
//swapping two items
#include <iostream>
#include <string>
using namespace std;
int main()
{
void swap_int (int &ix , int &iy);
void swap_ oat ( oat &fx, oat &fy);
void swap_char (char &cx,char &cy); // function declaration
void swap_string (string &str1, string &str2);
void menu();
int ix,iy;
oat fx,fy;
char cx,cy,ch;
string str1,str2;
menu();
while (( ch = cin.get()) != ‘q’) {
switch(ch) {
case ‘i’:
// swapping on integers
cout << “ enter any two integers \n”;
cin >> ix >> iy ;
cout << “ swapping of integers \n”;
cout << “ ix = ” << ix << “ iy = ” << iy << endl;
swap_int(ix,iy);
cout << “ after swapping \n”;
cout << “ ix = ” << ix << “ iy = ” << iy << endl;
break;
case ‘f’:
// oating point numbers
cout << “ enter any two oating point numbers\n”;
cin >> fx >> fy;
cout << “ swapping of oating point numbers \n”;
cout << “ fx = ” << fx << “ fy = ” << fy << endl;
swap_ oat(fx,fy);
cout << “ after swapping \n”;
cout << “ fx = ” << fx << “ fy = ” << fy << endl;
break;
case ‘c’:
//swapping characters
cout << “ enter any two characters\n”;
cin >> cx >> cy;
cout << “ swapping of characters \n”;
cout << “ cx = ” << cx << “ cy = ” << cy << endl;
swap_char(cx,cy);
cout << “ after swapping \n”;
cout << “ cx = ” << cx << “ cy = ” << cy << endl;
break;
case ‘s’:
//swapping strings
cout << “ enter any two strings\n”;
cin >> str1 >> str2;
cout << “ swapping of characters \n”;
cout << “ str1 = ” << str1 << “ str2 = ” << str2;
cout << endl;
swap_string(str1,str2);
cout << “ after swapping \n”;
cout << “ str1 = ” << str1 << “ str2 = ” << str2;
cout << endl;
break;
case ‘m’:
menu();
break;
}
} //end of while statement
return 0;
}
void menu()
{
cout << “Swapping two data without function overloading\n”;
cout << “ i -> integer swapping \n”;
cout << “ f -> real number swapping \n”;
cout << “ c -> character swapping \n”;
cout << “ s -> string swapping \n”;
cout << “ m -> menu \n”;
cout << “ q -> quit \n”;
}
void swap_int (int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swap_ oat ( oat &a, oat &b)
{
oat temp;
temp = a;
a = b;
b = temp;
}
void swap_char (char &a,char &b)
{
char temp;
temp = a;
a = b;
b = temp;
}
void swap_string (string &a,string &b)
{
string temp;
temp = a;
a = b;
b = temp;
}
Output of the above program
Swapping two data without function overloading
i -> integer swapping
f -> real number swapping
c -> character swapping
s -> string swapping
m -> menu
q -> quit
i
enter any two integers
10 20
swapping of integers
ix = 10 iy = 20
after swapping
ix = 20 iy = 10
f
enter any two oating point numbers
1.1 -2.2
swapping of oating point numbers
fx = 1.1 fy = -2.2
after swapping
fx = -2.2 fy = 1.1
c
enter any two characters
a b
swapping of characters
cx = a cy = b
after swapping
cx = b cy = a
s
enter any two strings
Windows Linux
swapping of characters
str1 = Windows str2 = Linux
after swapping
str1 = Linux str2 = Windows
q

Function Overloading with Various Data Types
The function overloading allows to use the same function name for the various data types. The function declaration, defi nition and calling of these functions are done with the same function name but with different data arguments. By function overloading, one can achieve greater fl exibility in program. The following program segment illustrates how the function declaration is done and called for swapping two data items for the various arguments using the concept of function overloading.

#include <iostream>
using namespace std;
int main()
{
void swap (int &ix , int &iy);
void swap ( oat &fx, oat &fy);
void swap (char &cx,char &cy); // functions are declared
-------
-------
swap(ix,iy); // functions are called with same name
swap(fx,fy);
swap(cx,xy);
return 0;
}

Example 1.
A program to demonstrate how function overloading is carried out for swapping of two variables of the various data types, namely integers, floating point numbers and character types.

// functions overloading
//swapping two items
#include <iostream>
#include <string>
using namespace std;
int main()
{
void swap (int &ix , int &iy);
void swap ( oat &fx, oat &fy);
void swap (char &cx,char &cy); // function declaration
void swap (string &str1, string &str2);
void menu();
int ix,iy;
oat fx,fy;
char cx,cy,ch;
string str1,str2;
menu();
while (( ch = cin.get()) != ‘q’) {
switch(ch) {
case ‘i’:
// swapping on integers
cout << “ enter any two integers \n”;
cin >> ix >> iy ;
cout << “ swapping of integers \n”;
cout << “ ix = ” << ix << “ iy = ” << iy << endl;
swap(ix,iy);
cout << “ after swapping \n”;
cout << “ ix = ” << ix << “ iy = ” << iy << endl;
break;
case ‘f’:
// oating point numbers
cout << “ enter any two oating point numbers\n”;
cin >> fx >> fy;
cout << “ swapping of oating point numbers \n”;
cout << “ fx = ” << fx << “ fy = ” << fy << endl;
swap(fx,fy);
cout << “ after swapping \n”;
cout << “ fx = ” << fx << “ fy = ” << fy << endl;
break;
case ‘c’:
//swapping characters
cout << “ enter any two characters\n”;
cin >> cx >> cy;
cout << “ swapping of characters \n”;
cout << “ cx = ” << cx << “ cy = ” << cy << endl;
swap(cx,cy);
cout << “ after swapping \n”;
cout << “ cx = ” << cx << “ cy = ” << cy << endl;
break;
case ‘s’:
//swapping strings
cin >> str1 >> str2;
cout << “ swapping of characters \n”;
cout << “ str1 = ” << str1 << “ str2 = ” << str2;
cout << endl;
swap(str1,str2);
cout << “ after swapping \n”;
cout << “ str1 = ” << str1 << “ str2 = ” << str2;
cout << endl;
break;
case ‘m’:
menu();
break;
}
} //end of while statement
return 0;
}
void menu()
{
cout << “Swapping two data using function overloading\n”;
cout << “ i -> integer swapping \n”;
cout << “ f -> real number swapping \n”;
cout << “ c -> character swapping \n”;
cout << “ s -> string swapping \n”;
cout << “ m -> menu \n”;
cout << “ q -> quit \n”;
cout << “ code, please ?\n”;
}
void swap (int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swap ( oat &a, oat &b)
{
oat temp;
temp = a;
a = b;
b = temp;
}
void swap (char &a,char &b)
{
char temp;
temp = a;
a = b;
b = temp;
}
void swap (string &a,string &b)
{
string temp;
temp = a;
a = b;
b = temp;
}
Output of the above program
Swapping two data using function overloading
i -> integer swapping
f -> real number swapping
c -> character swapping
s -> string swapping
m -> menu
q -> quit
code, please?
i
enter any two integers
100 200
swapping of integers
ix = 100 iy = 200
after swapping
ix = 200 iy = 100
f
enter any two oating point numbers
3.3 -5.6
swapping of oating point numbers
fx = 3.3 fy = -5.6
after swapping
fx = -5.6 fy = 3.3
c
enter any two characters
x y
swapping of characters
cx = x cy = y
after swapping
cx = y cy = x
s
enter any two strings
C++ Java
swapping of characters
str1 = C++ str2 = Java
after swapping
str1 = Java str2 = C++
q

Example 2.
A program to demonstrate how to use the function overloading for displaying different parameters of the member functions.

#include <iostream>
#include <string>
using namespace std;
class abc {
private:
int a,b;
oat fa,fb;
string str;
public:
void display();
void display (int a);
void display (int a,int b);
void display ( oat fa, oat fb);
void display (string msg);
void menu();
};
void abc :: display()
{
cout <<“Calling function without arguments\n”;
}
void abc :: display(int a)
{
cout <<“Calling function with one int argument \n”;
cout << “a = ” << a;
}
void abc :: display(int a, int b)
{
cout <<“Calling function with two int arguments \n”;
cout << “a = ” << a << “ b = ” << b << endl;
}
void abc :: display( oat fa, oat fb)
{
cout <<“Calling function with oating arguments \n”;
cout << “fa = ” << fa << “ fb = ” << fb << endl;
}
void abc :: display(string msg)
{
cout <<“Calling function with string argument\n”;
cout << “Message = ” << msg << endl;
}
void abc :: menu()
{
cout <<“Function overloading\n”;
cout <<“ 1 -> calling display()\n”;
cout <<“ 2 -> calling display(int a) \n”;
cout <<“ 3 -> calling display(int a, int b) \n”;
cout <<“ 4 -> calling display( oat fa, oat fb) \n”;
cout <<“ 5 -> calling display (string msg) \n”;
cout <<“ m -> menu() \n”;
cout <<“ q -> quit \n”;
}
int main()
{
abc obj;
void menu();
int n1,n2;
oat x,y;
string str;
char ch;
obj.menu();
while (( ch = cin.get()) != ‘q’) {
switch (ch) {
case ‘1’:
obj.display();
break;
case ‘2’:
cout <<“enter an integer \n”;
cin >> n1;
obj.display(n1);
break;
case ‘3’:
cout <<“enter any two integers \n”;
cin >> n1 >> n2;
obj.display(n1,n2);
break;
case ‘4’:
cout <<“enter any two real numbers \n”;
cin >> x >> y;
obj.display(x,y);
break;
case ‘5’:
cout <<“enter a string \n”;
cin >> str;
obj.display(str);
break;
case ‘m’:
obj.menu();
break;
}
}
return 0;
}
Output of the above program
Function overloading
1 -> calling display ()
2 -> calling display (int a)
3 -> calling display (int a, int b)
4 -> calling display ( oat fa, oat fb)
5 -> calling display (string msg)
m -> menu()
q -> quit
1
Calling function without arguments
2
enter an integer
10
Calling function with one int argument
a = 10
3
enter any two integers
100 200
Calling function with two int arguments
a = 100 b = 200
4
enter any two real numbers
1.1 2.2
Calling function with floating arguments
fa = 1.1 fb = 2.2
5
enter a string
Hello, C++
Calling function with string argument
Message = Hello, C++
q

Example 3.
A program to find the square of a given number belonging to the three data types, namely integers, floating point and double precision numbers without overloading of functions.

// function without overloading
#include <iostream>
using namespace std;
class abc {
public:
int square_int (int);
oat square_ oat ( oat);
double square_double (double);
void menu();
};
int abc :: square_int (int a)
{
return(a*a);
}
oat abc :: square_ oat ( oat a)
{
return(a*a);
}
double abc :: square_double (double a)
{
return(a*a);
}
void abc :: menu()
{
cout << “Finding the square of a given number\n”;
cout << “ i -> integers\n”;
cout << “ f -> real numbers \n”;
cout << “ d -> double precision numbers\n”;
cout << “ m -> menu \n”;
cout << “ q -> quit \n”;
cout << “ code, please ?\n”;
}
int main()
{
abc obj;
int x,xsq;
oat y,ysq;
double z,zsq;
char ch;
obj.menu();
while (( ch = cin.get()) != ‘q’) {
switch (ch) {
case ‘i’:
cout << “ enter an integer ” << endl;
cin >> x;
xsq = obj.square_int (x);
cout << “ x = ” << x;
cout << “ and its square = ” << xsq << endl;
break;
case ‘f’:
cout << “ enter a oating point number\n”;
cin >> y;
ysq = obj.square_ oat (y);
cout << “ y = ” << y ;
cout << “ and its square = ” << ysq <<endl;
break;
case ‘d’:
cout << “ enter a double ” << endl;
cin >> z;
zsq = obj.square_double (z);
cout << “ z = ” << z;
cout << “ and its square = ” << zsq << endl;
break;
case ‘m’:
obj.menu();
break;
}
}
return 0;
}
Output of the above program
Finding the square of a given number
i -> integers
f -> real numbers
d -> double precision numbers
m -> menu
q -> quit
code, please?
i
enter an integer
12
x = 12 and its square = 144
f
enter a oating point number
3.4
y = 3.4 and its square = 11.56
d
enter a double
100.34
z = 100.34 and its square = 10068.1
q

Example 4.
A program to fi nd the square of a given number belonging to the three data types, namely integers, floating point and double precision numbers using overloading of functions.

// function overloading
#include <iostream>
using namespace std;
class abc {
public:
int square (int);
oat square ( oat);
double square (double);
void menu();
};
int abc :: square (int a)
{
return(a*a);
}
oat abc :: square ( oat a)
{
return(a*a);
}
double abc :: square (double a)
{
return(a*a);
}
void abc :: menu()
{
cout << “Finding the square of a given number\n”;
cout << “ i -> integers\n”;
cout << “ f -> real numbers \n”;
cout << “ d -> double precision numbers\n”;
cout << “ m -> menu \n”;
cout << “ q -> quit \n”;
cout << “ code, please ?\n”;
}
int main()
{
abc obj;
int x,xsq;
oat y,ysq;
double z,zsq;
char ch;
obj.menu();
while (( ch = cin.get()) != ‘q’) {
switch (ch) {
case ‘i’:
cout << “ enter an integer ” << endl;
cin >> x;
xsq = obj.square (x);
cout << “ x = ” << x;
cout << “ and its square = ” << xsq << endl;
break;
case ‘f’:
cout << “ enter a oating point number\n”;
cin >> y;
ysq = obj.square (y);
cout << “ y = ” << y ;
cout << “ and its square = ” << ysq <<endl;
break;
case ‘d’:
cout << “ enter a double ” << endl;
cin >> z;
zsq = obj.square (z);
cout << “ z = ” << z;
cout << “ and its square = ” << zsq << endl;
break;
case ‘m’:
obj.menu();
break;
}
}
return 0;
}
Output of the above program
Finding the square of a given number
i -> integers
f -> real numbers
d -> double precision numbers
m -> menu
q -> quit
code, please?
i
enter an integer
2
x = 2 and its square = 4
f
enter a oating point number
4.4
y = 4.4 and its square = 19.36
d
enter a double
6.6
z = 6.6 and its square = 43.56
q

Function Overloading with Arguments
we have seen how a function could be overloaded for the various data types. The functions can be overloaded not only for the different data types but also for a number of arguments in the function call. The following program segment shows how a function can be declared, defi ned and called for different arguments for overloading of functions.

#include <iostream>
using namespace std;
int main()
{
int square (int );
int square (int, int,int);
-------
-------
asq = square (a); // function call with a single argument
bsq = square (x,y,z); //function call with various arguments
-------
-------
return 0;
}

1. A function definition to fi nd the square of a single argument is,
int square (int a)
{
return(a*a);
}

2. A function definition to fi nd the square of three arguments is,
int square (int a,int b, int c)
{
int temp;
temp = a+b+c; // sum of three numbers
return(temp*temp);
}

Example 1.
A program to fi nd the square of a given number with different arguments using function overloading.

// overloading of functions
// nding the square of the numbers
// number of arguments are also a factor
#include <iostream>
using namespace std;
int square (int );
int square (int, int,int);
int main()
{
int a,x,y,z,asq,bsq;
cout << “ enter an integer ” << endl;
cin >> a;
cout << “ enter any three numbers \n”;
cin >> x >> y >> z;
asq = square (a);
cout << “ a = ” << a << “ and its square = ” <<asq << endl;
bsq = square (x,y,z);
cout << “ x = ” << x;
cout << “ ,y = ” << y;
cout << “ ,z = ” << z ;
cout << “ and its sum of square = ” << bsq <<endl;
return 0;
}
int square (int a)
{
return(a*a);
}
int square (int a,int b , int c)
{
int temp;
temp = a+b+c; // sum of three numbers
return(temp*temp);
}
Output of the above program
enter an integer
2
enter any three numbers
1 2 3
a = 2 and its square = 4
x = 1, y = 2, z = 3 and its sum of square = 36

Example 2.
A program to fi nd the sum of the elements of a two-dimensional array of integers and fl oating point numbers without function overloading.

#include <iostream>
using namespace std;
int main()
{
int sum_funct1 (int a[3][3],int n);
oat sum_funct2 ( oat b[3][3], int n);
int n = 3, sum1;
oat sum2;
static int a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
static oat b[3][3] = {
{1.1,2.2,3.3},
{4.4,5.5,6.6},
{7.7,8.8,9.9}
};
sum1 = sum_funct1(a,n);
cout << “ sum of integers = ” << sum1;
cout << endl;
sum2 = sum_funct2(b,n);
cout << “ sum of oating point numbers = ” << sum2;
cout << endl;
return 0;
}
int sum_funct1 ( int a[3][3], int n)
{
int temp = 0;
for ( int i =0; i<= n-1; ++i) {
for (int j = 0; j<=n-1; ++j)
temp = temp+a[i][j];
}
return(temp);
}
oat sum_funct2 ( oat b[3][3], int n)
{
oat temp = 0.0;
for ( int i = 0; i<= n-1; ++i) {
for (int j = 0 ; j<=n-1; ++j)
temp = temp+b[i][j];
}
return(temp);
}
Output of the above program
sum of integers = 45
sum of oating point numbers = 49.5

Example 3.
A program to fi nd the sum of the elements of a two-dimensional array of integers and fl oating point numbers with function overloading.

//function overloading
#include <iostream>
using namespace std;
int sum (int a[3][3],int n);
double sum (double b[3][3], int n);
int main()
{
int n = 3, sum1;
double sum2;
static int a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
static double b[3][3] = {
{1.1,2.2,3.3},
{4.4,5.5,6.6},
{7.7,8.8,9.9}
};
sum1 = sum(a,n);
cout << “ sum of integers = ” << sum1;
sum2 = sum(b,n);
cout << “ sum of oating point numbers = ” << sum2;
cout << endl;
return 0;
}
int sum (int a[3][3], int n)
{
int temp = 0;
for ( int i = 0; i <= n-1; ++i) {
for (int j = 0; j<=n-1; ++j)
temp = temp+a[i][j];
}
return(temp);
}
double sum (double b[3][3], int n)
{
double temp = 0.0;
for (int i = 0; i <= n-1; ++i) {
for (int j = 0 ; j<=n-1; ++j)
temp = temp+b[i][j];
}
return(temp);
}
Output of the above program
sum of integers = 45
sum of floating point numbers = 49.5

Scoping Rules for Function Overloading
By definition, overloading is a process of defining the same function name to carry out similar types of activities with various data items or with different arguments. The overloading mechanism is acceptable only within the same scope of the function declaration. Sometimes, one can declare the same function name for different scopes of the classes or with global and local declaration, but it does not come under the technique of function overloading.

Example 1.
#include <iostream>
using namespace std;
class rst {
private :
int x;
public :
void getdata();
void display();
};
class second {
private :
int y;
public :
void getdata();
void display();
};
void rst :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void rst :: display()
{
cout << “ Entered number is x = ” << x;
cout << endl;
}
void second :: getdata()
{
cout << “ enter a value for y \n”;
cin >> y;
}
void second :: display()
{
cout << “ Entered number is y = ” << y;
cout << endl;
}
int main()
{
rst obj1;
second obj2;
obj1.getdata();
Obj2.getdata();
obj1.display();
obj2.display();
return 0;
}
Output of the above program
enter a value for x
10
enter a value for y
20
Entered number is x = 10
Entered number is y = 20

Example 2.
A program to demonstrate how to define the same function name within a global declaration and a class. Even though the same name is used to define the functions, in the different scopes, no function overloading takes place in the program.

#include <iostream>
using namespace std;
void display ()
{
cout << “ global display \n”;
cout << endl;
}
class sample {
private :
int x;
public :
void getdata();
void display();
};
void sample :: getdata()
{
cout << “ enter a value for x \n”;
cin >> x;
}
void sample :: display()
{
cout << “ Entered number is :” << x;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
cout << “ calling a member function display()\n”;
obj.display();
cout << “ calling a non-member function display()\n”;
::display();
return 0;
}
Output of the above program
enter a value for x
10
calling a member function display()
Entered number is: 10
calling a non-member function display()
global display

0 comments

Post a Comment