In this guide, you will discover the regulations concerning operator overloading in the C++ programming language. Numerous guidelines govern the process of operator overloading in C++. The primary regulations are outlined below:
1. Syntax
Defining operator overloading involves creating a function with the operator keyword followed by the specific operator that is being overloaded.
For example:
ReturnType operator+(const MyClass& obj)
{
// Overloaded + operator implementation
}
2. Logical and Consistent Implementation
Overloaded operators must be programmed in a logical and consistent manner to align with the anticipated functionality of the related built-in operators.
Refrain from overburdening operators in a manner that could result in ambiguity or unpredicted outcomes.
3. Number of Operands
The majority of unary and binary operators are capable of being overloaded.
Unary operators require only a single operand, such as +, -, ++, or --.
Binary operators require two operands, such as addition (+), subtraction (-), multiplication (*), and division (/).
4. Precedence and Associativity
The priority and grouping direction of the applicable predefined operators are inherited by overloaded operators.
Overloading an operator does not have the ability to change the precedence and associativity of the operator.
5. Types of Operand and Return
An operand used in an overloaded operator must belong to a user-defined type at a minimum.
The return type typically should point to the class or the class type in the case of assignment operators.
6. Overloading Restrictions
Some operators are not allowed to be overloaded, or their functionality cannot be altered significantly. For instance, the (member access), .* (member pointer access), :: (scope resolution), ?: (ternary conditional), sizeof, and typeid operators fall into this category.
7. Overloading as a Member Function
When overloaded as a member function, the calling object is implicitly considered as the left operand.
At least one operand of the user-defined type is required for binary operators to function.
Example:
MyClass operator+(const MyClass& obj) const
{
// Overloaded + Operator as a member function
}
8. Overloading as a Non-Member Function
Both parameters are provided as arguments when overloading a function as a non-member.
Example:
MyClass operator+(const MyClass& obj1, const MyClass& obj2)
{
// Overloaded + Operator as a non-member function
}
9. Friend Function Overloading
Operator overloading can alternatively be managed through a friend function when it necessitates access to private class members.
Example:
friend MyClass operator+(const MyClass& obj1, const MyClass& obj2)
{
// Overloaded + Operator as a friend function
}
10. Common Operators to Overload
Some commonly overloaded operators include +, -, , /, %, ==, !=, <, >, <=, >=, +=, -=, =, /=, ++, --, =, <<, and >>.
11. Overloading << for Output
Defining the << operator overload enables you to specify the output format for objects of your class when using std::cout.
Example:
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
// Overloaded << for output
}
Example:
#include <iostream>
class Complex
{
private:
double real;
double imag;
public:
// Constructor
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
//Overloading + Operator as a member function
Complex operator+(const Complex& other) const
{
Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}
// Overloading << operator for output
friend std::ostream& operator<<(std::ostream& os, const Complex& obj)
{
os << obj.real << " + " << obj.imag << "i";
return os;
}
};
int main()
{
// Creating complex numbers
Complex c1(2.5, 3.5);
Complex c2(1.5, 2.5);
// Using the overloaded + operator
Complex sum = c1 + c2;
// Displaying the result
std::cout << "Complex Number 1: " << c1 << std::endl;
std::cout << "Complex Number 2: " << c2 << std::endl;
std::cout << "Sum: " << sum << std::endl;
return 0;
}
Output:
Complex Number 1: 2.5 + 3.5i
Complex Number 2: 1.5 + 2.5i
Sum: 4 + 6i
Explanation:
- Definition of Class (Complex)
- In this example, the 'Complex' class represents complex numbers having real and imaginary components.
- It has a public constructor and two private data members, 'real' and 'imag' for initialisation.
- Overloaded Operator + (Member Function)
- It is overloaded because the '+' Operator is a member function of the Complex class.
- The parameter for the function is a reference to another Complex object (other).
- The relevant real and imaginary parts are added to generate a new Complex object (result).
- Overloaded Operator << (Friend Function)
- The << Operator is overused as a friend function to alter the output format.
- It requires two references: a Complex object (obj) and a std::ostream& reference (os).
- The real and imaginary components are printed to the output stream in the designated format.
- Main Function
- Two Complex objects ( c1 and c2 ) are created with initial values.
- The '+' Operator adds c1 and c2; the result is stored in the sum variable.
- The << Operator displays the complex numbers and their sum.
- Output
- When executed, the program outputs the complex numbers and their total in the predetermined format.