Operator Overloading: CS-2303 System Programming Concepts
Operator Overloading: CS-2303 System Programming Concepts
Operator Overloading
Simple Example
class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} }
General Format
returnType operator*(parameters);
any type keyword operator symbol
Operator Overloading
C++ Philosophy
All operators have context
Even the simple built-in operators of basic types E.g., '+', '-', '*', '/' for numerical types Compiler generators different code depending upon type of operands
Operator Overloading
Outline
Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading Stream Insertion and Stream Extraction Operators
Operator Overloading
Function name is keyword operator followed by the symbol for the operation being overloaded
E.g. operator+, operator=
CS-2303, C-Term 2010 Operator Overloading 10
Operator Overloading
12
Questions?
Operator Overloading
14
Operator Overloading
16
Stream Insertion and Extraction Operators as Global Functions Overload << operator used where
Left operand of type ostream &
Such as cout object in cout << classObject
&
Reason:
These operators are associated with class of right operand
CS-2303, C-Term 2010 Operator Overloading 17
Commutative operators
May need + to be commutative
So both a + b and b + a work as expected.
Already overloaded to process each built-in type (pointers and strings). Can also process a user-defined class.
Overload using global, friend functions
Example program
Class PhoneNumber
Holds a telephone number
17 private:
Notice function prototypes for overloaded operators >> and << (must be global, friend functions)
Operator Overloading
20
Operator Overloading
21
Operator Overloading
22
Invoke overloaded >> and << operators to input and output a PhoneNumber object
27 } // end main
Operator Overloading
23
Questions?
Operator Overloading
24
Unary Operators
Can overload as
Non-static member function with no arguments. As a global function with one argument.
Argument must be class object or reference to class object.
Why non-static?
static functions only access static data
Implemented as:
class String { public: bool operator!() const; };
CS-2303, C-Term 2010 Operator Overloading 26
Operator Overloading
27
By shorthand rule
y += z becomes y.operator+=( z )
CS-2303, C-Term 2010 Operator Overloading 28
By short-hand rule
y += z becomes operator+=(y, z)
CS-2303, C-Term 2010 Operator Overloading 29
Overloading Operators
On the previous slide, y and z are assumed to be String-class objects or references to Stringclass objects. There are two ways to pass arguments to the global function:
With an argument that is an object (this requires a copy of the object) or with an argument that is a reference to an object (this means the side effects of the function called to implement the overloaded operator can side-effect this object that is called-by-reference!)
CS-2303, C-Term 2010 Operator Overloading 30
Questions?
Operator Overloading
31
Next Time
Array Class
Deitel & Deitel
Operator Overloading
32