Function overloading

Aug 31, 2018

As we prefer naming the function name that is related to its functionality, there can be more than one functions that can have same name. In C Programming naming the function name with same name for more than one functions is not supported; C++ on the other hand supports such mechanism.

Function overloading is the programming concept that allows the programmers to define more than one functions with the same name and in the same scope; but functions has unique signatures, that means the mechanism to differentiate the functions. Let's see the simple example to return the square of the number using function overloading for int, float and double type.

int square(int num);

float square(float num);

double square(double num);

In above example, the function name are same, and what makes the functions to differentiate from others is the type of arguments, during function call the function that is called is detected by the compiler at compile time based on the type of arguments passed. i.e. if function is called as square(3); the square() function related to int is called.

The function overloading can be achieved by the following ways, or there are following types of function overloadig.

  1. Type of Arguments
  2. Number of Arguments
  3. Type and Number of Arguments

 

1)Type of Arguments

    This type of overloading is best matched for the same operation or functions to be performed for the different types of data. Function is overloaded based on the type of arguments. Let's consider the example for the squaring the number for different types of data.

#include<iostream>
using namespace std;

// function declarations or headers
int square(int num);
float square(float num);
double square(double num);

int main(){
	int x = 10;
	float y = 11.22;
	double z = 22.33;
	cout<<"Square of "<<x<<" = "<<square(x)<<endl;
	cout<<"Square of "<<y<<" = "<<square(y)<<endl;
	cout<<"Square of "<<z<<" = "<<square(z)<<endl;
	return 0;
}
// function definations
int square(int num){
	return num * num;
}
float square(float num){
	return num * num;
}
double square(double num){
	return num * num;
}

Sample Run:

Square of 10 = 100
Square of 11.22 = 125.888
Square of 22.33 = 498.629

 

2) Number of Arguments

In this type of overloading, compiler differentiates the function based on the number of arguments. Let's take an example to return the sum of numbers.

#include<iostream>
using namespace std;

// function declarations or headers
int sum(int n1, int n2);
int sum(int n1, int n2, int n3);
int sum(int n1, int n2, int n3, int n4);

int main(){
	int x = 10, y = 15, z = 20, a = 25;
	cout<<"Sum of "<<x<<" and "<<y<<" is "<<sum(x,y)<<endl;
	cout<<"Sum of "<<x<<" and "<<y<<" and "<<z<<" is "<<sum(x,y,z)<<endl;
	cout<<"Sum of "<<x<<" and "<<y<<" and "<<z<<" and "<<a<<" is "<<sum(x,y,z,a)<<endl;
	return 0;
}
// function definations
int sum(int n1, int n2){
	return n1 + n2;
}
int sum(int n1, int n2, int n3){
	return n1 + n2 + n3;
}
int sum(int n1, int n2, int n3, int n4){
	return n1 + n2 + n3 + n4;
}

Sample Run:

Sum of 10 and 15 is 25
Sum of 10 and 15 and 20 is 45
Sum of 10 and 15 and 20 and 25 is 70

 

3)Type and Number of arguments

The function is also overloaded by mixing above both types. That means compiler differentiates the function based on type and number of arguments passed. Let's see the following example.

#include<iostream>
using namespace std;

// function prototype
void display(int  num);
void display(int num, char ch);
void display(float num, int num1, char ch);

int main(){
	display(3);
	display(3, 'A');
	display(3.3, 5, 'K');
	return 0;
}
// function definations
void display(int num){
	cout<<"This function has integer "<<num<<endl;
}
void display(int num, char ch){
	cout<<"This function has integer "<<num<<" and character "<<ch<<endl;
}
void display(float num, int num1, char ch){
	cout<<"This function has float "<<num<<" integer "<<num1<<" and character "<<ch<<endl;
}

Sample Run:

This function has integer 3
This function has integer 3 and character A
This function has float 3.3 integer 5 and character K

 

 

Related


16 Default arguments in C++
Date: Sep 2, 2018
17 Friend Function, Friend class
Date: Sep 4, 2018
18 What are friend functions ? Is it possible for a function to be friend of two classes ? If yes then how is it implemented in C++. Explain with suitable example. ---PU-2016 (New Course)
Date: Sep 10, 2018
19 Write an OOP to add two complex numbers using passing objects as arguments. ---PU_2015
Date: Sep 10, 2018
20 Write a Program to add two complex numbers using friend function. ----PU_2015
Date: Sep 10, 2018
21 What are constructors and destructors ? Write a Object Oriented Program to display Fibonacci series (Use constructor for initialization) ----PU-2014
Date: Sep 15, 2018
22 Write a OOP to create a class named "Time" with data members 'hour' and 'minute' a constructor to initialize............. ---PU-2011
Date: Sep 15, 2018
23 What is "this" pointer ? Illustrate with example.
Date: Sep 15, 2018
24 Write a program to create class “Counter” having integer data member, overload the post increment, post decrement, pre increment and pre decrement for the class “Counter”.
Date: Sep 15, 2018
25 Write a Program to overload the unary minus operator using friend function.
Date: Sep 15, 2018
26 Write a OOP to overload "==" operator to determine the equality of two fractional numbers (fractional numbers must be in terms of numerator and denominator)
Date: Sep 15, 2018
27 Write a program to create classes “Dollar” and “Rupee”. Write conversion routine to convert dollar to rupee (1 dollar = 108 rupees).
Date: Sep 15, 2018
28 Write a Program to convert angle in degree to angle in radian. where, angle in radian = angle in degree * PI/180. Use Conversion routine in source class.
Date: Sep 15, 2018
29 Write a program which asks file name from keyboard, opens a file with that name for output, reads the line from keyboard character by character and writes the line onto the file.
Date: Sep 22, 2018
30 WAP to to read the text from the file named "hello.txt" to the file name called "test.txt".
Date: Sep 22, 2018

Tools/Apps





© Nepal Exchange Rates