In this tutorial, we'll explore operator overloading in C++.
Operator overloading allows you to redefine the way operators work with your classes. By the end of this tutorial, you will be able to overload various operators in C++, allowing you to create intuitive interfaces for your classes.
Prerequisites:
- Basic knowledge of C++
- Understanding of Classes and Objects in C++
Operator overloading is a type of polymorphism in which an operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc.
All operators can be overloaded except :: (Scope Resolution Operator), .(Member Access Operator), .*(Member dereference Operator) and ?: (Ternary or Conditional Operator).
Syntax for Operator Overloading:
return_type class_name::operator op(argument_list)
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
In this code:
- We're defining a class named Complex
.
- We're overloading the +
operator to add two Complex
objects.
- When c1 + c2
is executed, it calls the overloaded +
operator, which returns a new Complex
object.
- The expected output is 12 + i9
.
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
friend ostream & operator << (ostream &out, const Complex &c) {
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
};
int main() {
Complex c1(10, 5);
cout << c1;
}
In this code:
- We're overloading the <<
operator to print a Complex
object.
- Since <<
is a binary operator, which requires two arguments, we make it a friend function to access private members.
- When cout << c1
is executed, the overloaded <<
operator is called.
- The expected output is 10 + i5
.
In this tutorial, you've learned how to overload operators in C++. You can overload most of the operators available in C++ to work with your classes in a way that makes sense for your program.
Next steps for learning include:
- Overloading other operators like -
, *
, ==
, !=
, >
, <
, etc.
- Understanding the situations where operator overloading is beneficial.
Additional resources:
- C++ Operator Overloading Guidelines
- Cplusplus.com Operator Overloading
-
operator for the Complex
class.*
operator for the Complex
class.==
operator for the Complex
class.Solutions:
-
operator:Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
*
operator:Complex operator * (Complex const &obj) {
Complex res;
res.real = real*obj.real - imag*obj.imag;
res.imag = real*obj.imag + imag*obj.real;
return res;
}
==
operator:bool operator == (Complex const &obj) {
return (real == obj.real && imag == obj.imag);
}
These exercises should help you solidify your understanding of operator overloading in C++. Practice by creating your own classes and overloading operators to perform the operations you want.