In my last article, I showed you how to overload the input and output operators so that you can perform input and output with your user-defined types using the standard methods. In this article, I will cover how to overload three more operators: assignment, increment, and decrement.

The this Pointer

Before I can discuss how to overload the assignment operator, I need to discuss the this pointer. The this pointer is a built-in pointer that always points to the current object that has “focus” when code is executing.

Let’s say you have two objects that represents Points: p1 and p2 and you want to retrieve the x and y coordinates:

cout << p1.getX() << ", " << p1.getY();

Internally, the C++ compiler creates a compiler called this that is pointing to the p1 object. If I then write:

cout << p2.getX() << ", " << p2.getY();

the this pointer is now pointing to p2.

We will need the this pointer for overloading the operators I’m discussing in this article so you will get to see the this pointer in action soon.

#object-oriented #learning-to-code #cpp #machine-learning

Learning C++: Overloading the Assignment, Increment, and Decrement Operators
1.10 GEEK