0% found this document useful (0 votes)
15 views5 pages

Function Overloading by Dr.afzalBadshah

Function overloading in C++ allows multiple functions with the same name to coexist in the same scope, differentiated by their parameter lists. This feature enhances code readability, reusability, and flexibility, enabling similar operations on different types or numbers of inputs. The document includes a Calculator class example demonstrating various overloaded add functions for integers, strings, and doubles.

Uploaded by

atiqawan.me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Function Overloading by Dr.afzalBadshah

Function overloading in C++ allows multiple functions with the same name to coexist in the same scope, differentiated by their parameter lists. This feature enhances code readability, reusability, and flexibility, enabling similar operations on different types or numbers of inputs. The document includes a Calculator class example demonstrating various overloaded add functions for integers, strings, and doubles.

Uploaded by

atiqawan.me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

​Azal Badshah, PhD

Function overloading is a powerful feature in C++ that allows


multiple functions with the same name to exist in the same scope,
provided their parameter lists are different. This feature is a part of
polymorphism in Object-Oriented Programming (OOP) and allows
developers to implement functions that perform similar tasks but
operate on different types or numbers of inputs.

What is Function Overloading?


Function overloading occurs when:

Functions have the same name but different:


Number of parameters.
Types of parameters.
Order of parameters.
The compiler differentiates between these functions based on their
signature (name + parameter list).
Advantages of Function Overloading:
Code Readability:
Simplifies naming conventions since the same name can be reused
for similar operations.
Code Reusability:
Avoids duplication of similar functions with slightly different
names.
Flexibility:
Supports varying data types or numbers of parameters for the
same functionality.
Example Code: Function Overloading in C++
Below is an example demonstrating function overloading using a
Calculator class.

Class Code:
#include <iostream>
using namespace std;

class Calculator {
public:
// Function to add two integers
int add(int a, int b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Function to concatenate two strings


string add(string a, string b) {
return a + b;
}

// Function to add two double values


double add(double a, double b) {
return a + b;
}
};
Explanation of the Class Code:
Function: int add(int a, int b)
Purpose: Adds two integers and returns their sum.
Parameters: Two integer inputs a and b.
Function: int add(int a, int b, int c)
Purpose: Adds three integers and returns their sum.
Parameters: Three integer inputs a, b, and c.
Function: string add(string a, string b)
Purpose: Concatenates two strings and returns the result.
Parameters: Two string inputs a and b.
Function: double add(double a, double b)
Purpose: Adds two double values and returns their sum.
Parameters: Two double inputs a and b.
Main Function Code:
int main() {
Calculator cal; // Create an object of the Calculator class

// Call overloaded functions


cout << "The sum of two integers is: " << cal.add(5, 10) << endl;
cout << "The sum of three integers is: " << cal.add(5, 10, 15) <<
endl;
cout << "The concatenation of two strings is: " <<
cal.add("HELLO", " WORLD!") << endl;
cout << "The sum of two double numbers is: " << cal.add(5.55,
6.75) << endl;

return 0;
}
Explanation of the Main Function:
Object Creation:
Calculator cal: Creates an instance of the Calculator class.
Calling Overloaded Functions:
cal.add(5, 10)
Invokes the first add function for integers with two parameters.
Outputs: The sum of two integers is: 15.
cal.add(5, 10, 15)
Invokes the second add function for integers with three
parameters.
Outputs: The sum of three integers is: 30.
cal.add("HELLO", " WORLD!")
Invokes the third add function for string concatenation.
Outputs: The concatenation of two strings is: HELLO WORLD!.
cal.add(5.55, 6.75)
Invokes the fourth add function for double values.
Outputs: The sum of two double numbers is: 12.3.
Output of the Program:
The sum of two integers is: 15
The sum of three integers is: 30
The concatenation of two strings is: HELLO WORLD!
The sum of two double numbers is: 12.3
Key Points About Function Overloading:
Differentiation by Parameters:
Functions must differ by the number, type, or order of their
parameters.
Changing only the return type is not sufficient for function
overloading.
Compile-Time Polymorphism:
Function overloading is an example of compile-time
polymorphism, meaning the decision of which function to call is
made during compilation.
Real-Life Applications:
Allows the same operation (e.g., add) to work with different data
types or scenarios, such as adding numbers or concatenating
strings.
Conclusion:
Function overloading in C++ is a key feature of polymorphism that
enhances code reusability and readability. By allowing functions
with the same name to handle different types and numbers of
parameters, developers can write cleaner and more organized
code. In the provided example, the overloaded add functions
demonstrate how this feature can be used for multiple purposes,
from arithmetic operations to string concatenation.

You might also like