• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » Function Overloading
Next →
← Prev

Function Overloading

By Dinesh Thakur

In some cases when a similar action is to be performed on different types of data, different functions having different names are to be defined for all types of data. This makes the program very complex as the programmer must keep a track of the names of all the functions defined in the program. To prevent such situations, C++ allows the functions to be overloaded.

Overloading affirms the role of a single entity for multiple tasks. Function overloading is a type of polymorphism that allows multiple functions to share the same name with different parameters. The compiler identifies the function either on the basis of the number of parameters, the data type of the parameters or the order of the data type of the parameters passed to the function. Moreover, functions with different return type but with similar function signature are not considered as overloaded functions.

To understand the concept of function overloading, consider these function declarations.

void func (int) ;
void func (int, int) ;

In these statements, the two functions named, func () are different as the number of arguments passed are different. Now, consider these statements.

void func (int) ;
void func (char) ;

In these statements, the two functions named, func () are different as the data type of arguments passed is different. Now, consider these statements.

void func (int , float) ;
void func (float, int) ;

In these statements, two functions named, func () are different as the order of the data type of the arguments passed is different.

The compiler follows these steps to perform a comparison between the actual and the formal arguments to find the best match (the most appropriate overloaded function).

• Exact match: If the number and type of the arguments exactly match the number and type of parameters of anyone of the overloaded functions then that function is called.

• Match through type promotions: If an exact match is not found, the compiler tries to promote the type of the argument to the type of the parameter of an overloaded function. For example, if the argument is of type char, the compiler promotes it to type int or to the equivalent type unsigned into

• Match through standard conversions: If a match is also not found after performing type promotions, the compiler tries to convert the type of the argument to the type of the parameter through standard conversion rules. For example, if the argument is of type int, the compiler can convert it to type float, double or long double.

• Match through user-defined conversions: If a match is also not found after performing standard type conversions, the compiler tries to convert the type of the argument to the type of the parameter through the user-defined conversions.

• Match through ellipsis: If a match is not found after performing the previous steps, the compiler tries to find an ellipsis in the definition (or declaration) of any of the overloaded functions.

To understand the concept of function overloading, consider this example.

Example : A program to demonstrate the concept of function overloading

#include<iostream>
#include<math>
using namespace std;
int area (int); //function prototype 1
float area (float, float); //function prototype 2
float area (float, float, float); //function prototype 3
int main () {
  int side;
  float length, width, a, b, c;
  cout<<"Enter side of a square: ";
  cin>>side;
  cout<<"Area of a square is : "<<area (side)<<endl;
  cout<<"Enter length and width of a rectangle: ";
  cin>>length>>width;
  cout<<"Area of a rectangle is :"<<area(length,width)<<endl;
  cout<<"Enter three sides of a triangle: ";
  cin>>a>>b>>c;
  cout<<"Area of a triangle is : "<<area(a, b, c)<<endl;
  return 0;
}
int area(int s) {
 return (s*s);
}
float area(float len, f]oat wid) {
  return (len*wid);
}
float area(float a, float b, float c) {
  float s,area;
  s=(a+b+c)/2;
  area=sqrt(s*(s-a)*(s-b)*(s-c)) ;
  return area;
}

The output of the program is

Enter side of a square : 12

Area of a square is : 144

Enter length and width,of a rectangle: 34.5 67.7

Area of a rectangle is : 2335.65

Enter three sides of a triangle: 12.5 20.5 25.5

Area of a triangle is : 126.791

In this example, three functions are declared with the same name area (),however, accepting different arguments. The compiler examines the number, type of arguments or order of data type in the function call and calls the appropriate function.

In some situations, when only the number of arguments is different then instead of function overloading, default arguments can be used. This is because default arguments combine different operations into one function and hence, reduce the overhead of writing multiple functions. To understand this concept, consider this example.

Example : A program to demonstrate the use of default arguments as an alternative to function overloading

#include<iostream>
using namespace std;
void disp(char= '*',int=4);
int main () {
  disp ();
  disp (‘^’) ;
  disp ('@' ,5) ;
  return 0;
}
void disp(char ch, int n) {
  for(int i=0; i<=n; i++) {
   for(int j=0; j<I; j++) {
     cout<<ch;
     cout<<" ";
   }
     cout<<endl;
}}

The output of the program is

*

* *

* * *

* * * *

^

^^

^^^

^^^^

@

@@

@@@

@@@@

@@@@@

In this example, three calls with different number of arguments are made to the same function void disp (char=’*’,int=4) . In this way, default arguments are used to reduce the number of functions and hence, serve as an alternative to function overloading.

You’ll also like:

  1. What is Function Overloading and Operator Overloading
  2. Function Overloading and Method Overloading in Java
  3. Write a C++ program to Operator Overloading Using a Friend Function.
  4. What is Overloading. Type of Overloading
  5. Write A C++ Program To Add, Subtract And Multiply Two Numbers By Using The Function Within Function Concept (Nesting Of Function).
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW