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


31 Write a program to enter the name and salary for a number of employees, store them all in a file, and finally search and display the salary...............
Date: Sep 22, 2018
32 Write a program, using a class to read the contents of the text file and convert all upper case letters to lower case and vice-versa and write the result in to another file.
Date: Sep 22, 2018
33 Write a program to enter the id and price of a number of products, store them all in a user defined file, and search the product when particular id is entered.
Date: Sep 22, 2018
34 Write a program using class template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
35 Write a program using function template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
36 Write a program using function template to find the sum of first and last element of an array.
Date: Sep 23, 2018
37 Write a template function to find the maximum number from the template array of size N.
Date: Sep 23, 2018
38 Write a function template to swap the numbers.
Date: Sep 23, 2018
39 Write a program using class template to swap two set of numbers.(one integer set and another floating point set)
Date: Sep 23, 2018
40 Exception handling in C++
Date: Aug 11, 2019
41 Explain the concept of encapsulation and information hiding In an object oriented paradigm with the help of an example.
Date: Sep 2, 2019
42 Write a program to find sum of first and last element of an array demonstrating use of new and delete operator.
Date: Sep 2, 2019
43 Write a program to compare two integer numbers and two single character using function overloading.
Date: Sep 2, 2019
44 Write an oop to create a class named “Time” with data objects and two member functions to add two member functions to add two times and display the resultant time.
Date: Sep 2, 2019
45 Write a program to define a class “string” using constructor initialize two string objects with given strings and write necessary functions to concatenate and display the strings.
Date: Sep 2, 2019

Tools/Apps





© Nepal Exchange Rates