Function overloading is a feature in C++ where you can define multiple functions with the same name but with different parameter lists. The C++ compiler distinguishes between these functions based on the number and/or types of arguments passed to them. Function overloading is used to perform different tasks using the same function name, improving code readability and reducing the need for multiple function names.
addIntegers, addFloats).In C++, you can overload a function by defining multiple functions with the same name but different signatures (parameter types or number of parameters):
return_type function_name(parameter_list)
For overloading to work, the parameter lists of the functions must differ in at least one of the following ways:
In this example, we overload the function add() to handle both two and three integer parameters.
#include <iostream>
using namespace std;
class Calculator {
public:
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
// Function to add three numbers (overloaded)
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Calculator calc;
int sum1 = calc.add(5, 10); // Calls the add() method with two parameters
int sum2 = calc.add(5, 10, 15); // Calls the add() method with three parameters
cout << "Sum of two numbers: " << sum1 << endl; // Output: 15
cout << "Sum of three numbers: " << sum2 << endl; // Output: 30
return 0;
}
Explanation:
add function is overloaded: one version accepts two integers, and the other accepts three integers.add(5, 10) is called, the first version of add is invoked, and when add(5, 10, 15) is called, the second version is invoked.In this example, we overload the multiply() function based on different types of parameters (integers, floats, etc.).
#include <iostream>
using namespace std;
class Multiplier {
public:
// Function to multiply two integers
int multiply(int a, int b) {
return a * b;
}
// Function to multiply two floats (overloaded)
float multiply(float a, float b) {
return a * b;
}
// Function to multiply an integer and a float (overloaded)
float multiply(int a, float b) {
return a * b;
}
};
int main() {
Multiplier m;
int intResult = m.multiply(4, 5); // Calls the int version of multiply()
float floatResult1 = m.multiply(3.5f, 2.0f); // Calls the float version
float floatResult2 = m.multiply(3, 2.5f); // Calls the int-float version
cout << "Multiplication of integers: " << intResult << endl; // Output: 20
cout << "Multiplication of floats: " << floatResult1 << endl; // Output: 7.0
cout << "Multiplication of int and float: " << floatResult2 << endl; // Output: 7.5
return 0;
}
Explanation:
multiply() function is overloaded based on different parameter types (integer and float).multiply() takes a different combination of parameter types and returns the result accordingly.In this case, we overload the concatenate() function based on the order of parameters (string and integer).
#include <iostream>
#include <string>
using namespace std;
class StringManip {
public:
// Function to concatenate string and integer (string first)
string concatenate(string s, int num) {
return s + to_string(num);
}
// Function to concatenate integer and string (integer first)
string concatenate(int num, string s) {
return to_string(num) + s;
}
};
int main() {
StringManip sm;
string result1 = sm.concatenate("Number: ", 42); // String first, then integer
string result2 = sm.concatenate(42, " is the answer."); // Integer first, then string
cout << result1 << endl; // Output: Number: 42
cout << result2 << endl; // Output: 42 is the answer.
return 0;
}
Explanation:
concatenate() function is overloaded by the order of parameters. The first version concatenates a string and an integer with the string coming first, and the second version does the reverse.Return Type Does Not Help in Overloading:
// Invalid overload based on return type
int add(int a, int b); // Function 1
double add(int a, int b); // Function 2 (Invalid, because they have the same parameter list)
Compiler’s Role in Resolution:
Ambiguity in Overloading:
int and float), it may be unclear to the compiler which one to call.Default Arguments and Overloading:
void print(int a, string b = "Default"); // Overload with default argument
#include <iostream>
using namespace std;
class Printer {
public:
// Function to print integer and string
void print(int a, string b = "Default Message") {
cout << "Integer: " << a << ", String: " << b << endl;
}
};
int main() {
Printer p;
p.print(5); // Calls print() with the default string value
p.print(10, "Custom Message"); // Calls print() with a custom string
return 0;
}
Explanation:
print() function has a default argument for the string parameter. If the second argument is not provided, the default value ("Default Message") is used.addInt, addFloat), you can use the same name (add) for different types.Function overloading is a powerful feature in C++ that allows you to define multiple functions with the same name but different parameter types, numbers, or orders. It enhances the readability, flexibility, and maintainability of your code by reducing the need for multiple function names while still allowing different behaviors based on the function arguments. However, overloading should be used with care to avoid ambiguity and ensure that the function selection is clear to the compiler.
Open this section to load past papers