Operator Overloading in C++
Operator overloading is a feature in C++ that allows you to define how operators (like +, -, *, ==, etc.) behave when they are used with user-defined data types (classes). This allows you to use operators with objects in a natural and intuitive way, similar to how they work with built-in data types.
+ and - on custom data types, making your code more intuitive and easier to understand.The syntax for overloading an operator involves defining a special function inside the class that uses the keyword operator followed by the symbol of the operator being overloaded.
returnType operator symbol (parameter list) {
// implementation
}
+ Operator for a Complex Number ClassLet’s take the example of a class Complex where we want to overload the + operator to add two complex numbers.
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
// Constructor to initialize complex numbers
Complex(int r, int i) : real(r), imag(i) {}
// Overloading the + operator to add two complex numbers
Complex operator + (const Complex& obj) {
Complex temp(0, 0);
temp.real = real + obj.real; // Add real parts
temp.imag = imag + obj.imag; // Add imaginary parts
return temp; // Return the sum of complex numbers
}
// Function to display the complex number
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex num1(3, 4), num2(1, 2);
Complex sum = num1 + num2; // Using overloaded + operator
sum.display(); // Output: 4 + 6i
return 0;
}
Complex class with two private data members: real and imag (real and imaginary parts of the complex number).+ operator is overloaded using the syntax operator + to add the real and imaginary parts of two complex numbers.main function demonstrates the usage of the overloaded + operator, and the result is displayed using the display() function.Arithmetic Operators: +, -, *, /, %, etc.
Complex operator - (const Complex& obj); // Overload subtraction
Comparison Operators: ==, !=, <, >, <=, >=
bool operator == (const Complex& obj); // Overload equality check
Assignment Operator: =
Complex& operator = (const Complex& obj);
Increment and Decrement Operators: ++, --
Complex operator ++ (); // Pre-increment
Complex operator ++ (int); // Post-increment
Stream Insertion and Extraction Operators: <<, >>
friend ostream& operator << (ostream& out, const Complex& obj);
friend istream& operator >> (istream& in, Complex& obj);
When you overload the assignment operator =, it's important to handle memory management properly, especially if the class involves dynamic memory allocation (using new or delete).
class Complex {
private:
int* real;
int* imag;
public:
// Constructor
Complex(int r, int i) {
real = new int(r);
imag = new int(i);
}
// Copy constructor
Complex(const Complex& obj) {
real = new int(*obj.real);
imag = new int(*obj.imag);
}
// Assignment operator overloading
Complex& operator = (const Complex& obj) {
if (this != &obj) {
*real = *obj.real;
*imag = *obj.imag;
}
return *this;
}
// Destructor
~Complex() {
delete real;
delete imag;
}
};
In this example, we overload the assignment operator to ensure that when one Complex object is assigned to another, it properly copies the data without causing memory issues (like shallow copying).
Overloading the = Operator:
= operator to manage memory properly (deep copying).Overloading [] (Array Subscript):
int& operator [] (int index) {
// return the element at the given index
}
Friend Function for Overloading:
friend function of the class.+, -, etc., with custom classes.=.Open this section to load past papers