Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Exploring Function Overloading in C++ with Examples

Basics of SQL
Basics of SQL
icon
12 Hrs. duration
icon
12 Modules
icon
2600+ Learners
logo
Start Learning

In C++, there are two types of overloading: operator overloading and function overloading. Function overloading is an important concept in C++ that allows multiple functions to have the same name despite having different logics. It enhances code readability, reusability, and maintainability, speeds up the code, and reduces memory space. Let’s do a deep dive and understand the concepts of function overloading in detail.

What is Function Overloading in C++?

The ability to declare multiple functions with the same name but different types and order of parameters is known as function overloading. It is a special feature of OOPS and is based on the idea of polymorphism. It allows a specific function name to have many behaviours dispensing upon the parameters passed. It improves the code’s readability, allows efficient code execution, and enhances the performance of the codebase.

 

Example:

// Example of function overloading # include<bits/stdc++.h> using namespace std; // Define overloaded function signatures. void productOfNumber(int p, int q); void productOfNumber(int p, int q, int r); void productOfNumber(int p, int q, int r, int s); int main() // Calling the overloaded functions with different number of arguments productOfNumber(11,20); productOfNumber(12,22,30); productOfNumber(1,12,31,4); return 0; } // Define the body of functions void productOfNumber(int p, int q) cout<< endl << "The product is : "<< p*q; void productOfNumber(int p, int q, int r) { cout<< endl << "The product is : "<< p*q*r; void productOfNumber(int p, int q, int r, int s) { cout<< endl << "The product is : "<< p*q*r*s; }

Output:

The product is : 220 The product is : 7920 The product is : 1488

What are the Different Ways of Function Overloading in C++?

In C++, there are many multiple ways we can achieve function overloading. Some of those are:

Function Overloading by Changing the Number of Arguments

As the name suggests, this overloading is achieved by changing the number of arguments passed to the function. Let’s see an example below to get a better understanding.

 

Example:

// Example of function overloading by changing the number of arguments # include<bits/stdc++.h> using namespace std; // Define overloaded function signatures. void sumOfNumber(int p, int q); void sumOfNumber(int p, int q, int r); void sumOfNumber(int p, int q, int r, int s); int main() { // Calling the overloaded functions with different number of arguments sumOfNumber(11,20); sumOfNumber(12,22,30); sumOfNumber(1,12,31,4); return 0; } // Define the body of functions void sumOfNumber(int p, int q) { cout<< endl << "The sum is : "<< p+q; } void sumOfNumber(int p, int q, int r) { cout<< endl << "The sum is : "<< p+q+r; } void sumOfNumber(int p, int q, int r, int s) { cout<< endl << "The sum is : "<< p+q+r+s;  }

Output:

The sum is : 31 The sum is : 64 The sum is : 48

In the above example, three functions with the same name are declared and defined, but the number of parameters is different in each case.

Function Overloading by Changing the Type of Arguments

As the name suggests, this overloading is achieved by changing the type of arguments passed to the function. Let’s see an example below to get a better understanding.

Example:

// Example of function overloading by changing the type of arguments # include<bits/stdc++.h> using namespace std; // Define overloaded function signatures. void showValue(int p); void showValue(char p); void showValue(float p); int main() { // Calling the overloaded functions with different number of arguments showValue(20); showValue('V'); showValue(7.14f); return 0; } Define the body of functions void showValue(int p) {      cout<< endl << "The value is : "<< p;      } void showValue(char p) {      cout<< endl << "The value is : "<< p;   } void showValue(float p) {      cout<< endl << "The value is : "<< p;      }

Output:

The value is : 20 The value is : V The value is : 7.14

In the above example, three functions with the same name are declared and defined, but the type of parameters is different in each case.

Function Overloading by Changing the Order of Arguments

As the name suggests, this overloading is achieved by changing the order of arguments passed to the function. Let’s see an example below to get a better understanding.

 

Example:

// Example of function overloading by changing the order of arguments # include<bits/stdc++.h> using namespace std; // Define overloaded function signatures. void showValue(int p, char q, float r); void showValue(char p, int q, float r); void showValue(float p, char q, int r); int main() {     // Calling the overloaded functions with different number of arguments     showValue(20, 'S', 1.2f);     showValue('V', 35, 2.21f);     showValue(7.14f, 'E', 76);      return 0; } // Define the body of functions void showValue(int p, char q, float r) cout<< endl << "The values are : "<< p << ", " <<q <<" ," <<r<<endl; void showValue(char p, int q, float r) { cout<< endl << "The values are :: "<< p << ", " <<q <<" ," <<r<<endl; } void showValue(float p, char q, int r) { cout<< endl << "TheThe values are :: "<< p << ", " <<q <<" ," <<r<<endl; }

Output:

The values are : 20, S ,1.2 The values are :: V, 35 ,2.21 The values are :: 7.14, E ,76

In the above example, three functions with the same name are declared and defined, but the order of parameters is different in each case.

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Rules of Function Overloading in C++

While performing function overloading, it is crucial to follow some rules or steps, as mentioned below:

 

  • The overloaded functions need to have the same name.
  • The overloaded functions can have different lists of parameters. The parameters can differ in order, type, and number.  
  • The overloaded functions can have the same or different return types.
  • The function signatures of overloaded functions must be different. 
  • The overloaded function must be distinguishable by the compiler, preventing ambiguous or conflicting function overloading.  
  • The function with default arguments can be overloaded. 

Uses of Function Overloading in C++

Function overloading enhances code readability, reusability, and maintainability. Below are some of its uses:

 

  • It improves the code’s readability, and developers can easily understand the function’s purpose based on its name, regardless of the data types or arguments used. 
  • It provides uniform consistency for similar tasks. 
  • It enables developers to write modular and reusable code. Once the core functionality is defined, the same function can be reused across different segments of the codebase. 
  • It promotes code consistency by allowing developers to perform the same logical operation on different data types without needing a distinct function name every time.
  • It can be used in combination with default arguments and provides flexibility to callers who may not want to pass all arguments. 
  • It ensures type safety. The compiler makes sure the correct overload is selected based on the argument type.
  • Function overloading is a form of static-polymorphism, as the decision of which function to call is taken at compile-time. 
  • It results in efficient code execution and enhances the performance of the codebase.

Advantages of Function Overloading in C++

Below are some benefits of function overloading:

 

  • It is efficient and saves memory space. 
  • It increases the readability and consistency of the program.
  • It offers different behaviours using the same method name.
  • It increases the execution speed of the program.
  • It enhances code reusability.
  • It makes code maintenance and debugging easy.
  • It guarantees codebase scalability and backward compatibility.
  • It helps applications load class methods based on the parameter type.

Disadvantages of Function Overloading in C++

Below are some drawbacks of function overloading:

 

  • When multiple functions with the same name are defined, it becomes challenging for the compiler to identify the right function to call, which can lead to a compilation error or unusual behaviour of the program.
  • It increases the code complexity and makes the code difficult to debug. 
  • The maintenance of code is quite difficult in such cases.

Conclusion

This concludes our discussion on function overloading in C++. We discussed different ways to achieve function overloading, some rules to follow, and their uses, with their benefits and drawbacks, each with proper explanation and examples.

FAQs
In C++, there are two main types of overloading: operator overloading and function overloading.
There must be at least two functions required to obtain function overloading in C++
Yes. Function overloading is a form of static polymorphism, as the decision of which function to call is taken at compile-time.
No. A function can only have a single specified return type, which is defined in its signature.
  • It increases the readability and consistency of the program.
  • It increases the execution speed of the program.
  • It enhances code reusability.
  • It makes code maintenance and debugging easy.
  • It guarantees codebase scalability and backward compatibility.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

24 October, 7:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved