1. Introduction
In C++, function overloading and operator overloading are two concepts that enhance the flexibility and readability of the language. Function overloading allows multiple functions to have the same name with different parameters, while operator overloading enables custom behaviors for standard operators based on operands' types.
2. Key Points
1. Function overloading is creating multiple functions with the same name but different parameters.
2. Operator overloading involves defining a new behavior for existing operators for user-defined types.
3. Function overloading is based on the number and types of parameters, not the return type.
4. Operator overloading can be done by defining a function that represents the behavior of the operator for different operands.
3. Differences
Function Overloading | Operator Overloading |
---|---|
Involves having multiple functions with the same name but different parameters. | Involves defining new behaviors for existing operators with specific types. |
Distinguished by the number and types of parameters. | Defined for specific types, often within the class definition of those types. |
Improves code readability and function utilization. | Enhances code readability and allows objects of user-defined types to behave like fundamental types. |
4. Example
#include <iostream>
using namespace std;
// Function overloading example
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
// Operator overloading example
class Complex {
public:
int real, imag;
Complex(int r = 0, int i =0) { real = r; imag = i; }
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void display() {
cout << real << " + i" << imag << endl;
}
};
int main() {
// Function overloading
print(5);
print(5.5);
// Operator overloading
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.display();
return 0;
}
Output:
Printing int: 5 Printing float: 5.5 12 + i9
Explanation:
1. In the function overloading example, the print function is overloaded with different parameter types: one for int and another for double.
2. In the operator overloading example, the + operator is overloaded in the Complex class to add two Complex objects.
5. When to use?
- Use function overloading to create functions with the same name but different parameters for similar operations on different types or numbers of inputs.
- Use operator overloading to enable operators to work with user-defined types, making your classes integrate more seamlessly with the language’s features.
Related C++/CPP Posts:
Difference Between Struct and Class in C++
Difference Between Pointer and Reference in C++
Difference Between null and nullptr in C++
Difference Between Array and Vector in C++
Difference Between const and constexpr in C++
Difference Between List and Vector in C++
Difference Between Function Overloading and Operator Overloading in C++
Difference Between Array and List in C++
Difference Between a While Loop and a Do-While Loop in C++
Difference Between new and malloc C++
Virtual Function vs Pure Virtual Function in C++
Compile Time Polymorphism vs Runtime Polymorphism in C++
Difference Between Shallow Copy and Deep Copy in C++
Comments
Post a Comment
Leave Comment