Home | Projects | Notes > C++ Programming > Operator Overloading

Operator Overloading

 

Overview

C++ allows the programmer to overload most operators to work with user-defined classes. This is intended to make code more readable and writable by allowing the use of familiar operators in a familiar manner, but with our own classes and objects.

 

What is Operator Overloading?

 

What Operators can be Overloaded?

 

Basic Rules of Operator Overloading

 

Examples of Operator Overloading

Operator Overloading used on C++ Built-In Types:

Operator Overloading used on User-Defined Types:

 

Copy Assignment Operator Overloading

Move Assignment Operator (=) Overloading

 

Overloading Operators as Member Functions

 

Overloading Operators as Global Functions

Notice that now the very last line of code is legal with the global (non-member) overloading function.

 

Overloading Stream Insertion and Extraction Operators (<<, >>)

 

Complete Mystring Class

==, -, + oerators are overloaded as both member and non-member functions. As this can cause some ambiguity issues, it would be safe to test (or comment out) one group at a time.

Mystring class declaration:

Mystring class implementation:

 

 

References

Mitropoulos, F. (2022). Beginning C++ Programming - From Beginner to Beyond [Video file]. Retrieved from https://www.udemy.com/course/beginning-c-plus-plus-programming/