Comprehensive Guide to Operator Overloading in C++
This resource provides a detailed overview of operator overloading in C++. It explains various operator types including arithmetic, comparison, logical, and increment/decrement operators. The presented examples illustrate how operator overloading is implemented for custom classes, demonstrating access to private data members and methods. Key concepts like implicit and explicit parameters, rules for overloading, and common usage patterns are discussed to reinforce understanding. This guide serves as a valuable reference for C++ programmers aiming to leverage operator overloading effectively.
Comprehensive Guide to Operator Overloading in C++
E N D
Presentation Transcript
Operator Overloading CSCE 121 J. Michael Moore Based on Slides created by Carlos Soto.
Operators Assignment: = Arithmetic: +, -, *, /, % Compound assignment: +=, -=, *=, /=, %= Increment/decrement: ++, -- Logical: !, &&, || Comparison: ==, !=, <, >, <=, >= Other: <<, >>, etc.
Operators (kind of like functions...) int a = 0, b = 5; int c = a + b; // int +(int a, int b); c++; // void ++(int& c); int d = a; // void =(constint& a, int &d);
Operators (a lot like functions...) inti = 0, j = 5; string s; i++; // matches ++ operator’s ‘arguments’ s++;// no match int k = i + j;// integer addition string s2 = s + s;// string concatenation
Operator Overloading int k = i + j; // int +(int a, int b); string s2 = s + s; // string +(string a, string b); NOT actually a function, because int is a PRIMITIVE datatype
Operator Overloading int k = i + j; // int +(int a, int b); string s2 = s + s; // string operator+(const& string a, const& string b);
Operator overloading class MyClass { intmyAttr= 7; }; // outside class scope MyClass operator+(constMyClass& a, constMyClass& b) { // ... } Does not have access to private MyClass data and methods.
Operator overloading (inside class scope) class MyClass { intmyAttr = 7; // ... public: // ... MyClass(int k) : myAttr(k) {} MyClass operator==(constMyClass& b) { // ... } }; MyClass operator+(constMyClass& a, constMyClass& b); Has access to private MyClass data and methods
Parameters (implicit and explicit) bool MyClass::operator==(constMyClass& other){ return (myAttr == other.myAttr); } // ... MyClass a = MyClass(7); MyClassB = MyClass(11); if (a == b) { // ... } Equivalent to calling: if (a.operator==(b)) {
Alternatively bool MyClass::operator==(constMyClass& other){ return (this->myAttr== other.myAttr); } element access operator for pointers to objects Recall:: ‘this’ is a pointer to the instance of the class Student that called the == operator
Return types string operator+(const string& a, const string& b); bool MyClass::operator==(constMyClass& other); MyClass& MyClass::operator=(constMyClass & other);
Returning a reference in a overloaded operator MyClass& MyClass::operator=(constMyClass& other) { this->myAttr = other.myAttr; return *this; } Essentially, dereference ‘this’ and the compiler knows how to get the address from the class.
Rules for Operator Overloading • Can only overload existing operators • can’t create your own • Cannot change the number of operands • Must take at least one non-primitive datatype operand