Week 3 - 1 Week 3 2 Chapter 5 Operator Overloading Outline...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Week 3 2 Chapter 5: Operator Overloading Outline:  Motivation  Unary operator overloading  Binary operator overloading  Overloading the output << operator  Restrictions 3 Motivation  Operator overloading allows objects to become operands of existing operators (those provided by the syntax).  The semantics of the operation is defined by the programmer.  Operator overloading makes classes appear like primitive types.  Java does not support operator overloading. 4 Unary Operator Overloading  Let OP be a unary operator (++, --, !, *, &, …) and obj be an object.  C++ considers the following expressions equivalent OP obj ; obj . operator OP ();  That is, unary operator OP is simply a 0-parameter member function operator OP that can be invoked with a unary operation syntax.  Thus when operator OP is implemented to work on obj , the function operator OP is overloaded 5 Unary Operator Overloading Example (1) $ cat uoo.cpp #include <iostream> using namespace std; class P { int x, y; // private: by default public: P(int X=0, int Y=0): x(X), y(Y) {} void operator*() { // overload unary operator * x = ++x; y = --y; } void operator&() { // overload unary operator & cout << x << ’\t’ << y << endl; } } ; 6 Unary Operator Overloading Example (2) int main() { P p00; // x and y are set to 0 by default p00. operator* (); // increment x and decrement y p00. operator& (); // print x and y P q00; * q00; // Use unary operator * & q00; // Use unary operator & return 0; } $ g++ uoo.cpp $ a.out 1 -1 1 -1 7 Overloading the Prefix and Postfix ++ / -- Operators  To distinguish the prefix member operator function from that of the postfix, the latter pretends to take an int parameter.  To be consistent with the expected semantics, the prefix operator function should return the new value but the postfix operator function should return the original value. 8 Overloading ++ / -- Operators Example (1) $ cat pp.cpp #include <iostream> using namespace std; class P { int x, y; public: P(int X=0, int Y=0): x(X), y(Y) {} P(const P & p) : x(p.x), y(p.y) {} P operator++() { x++; y++; return *this; } // prefix P operator--() { x--; y--; return *this; } P operator++( int ) { P orig = *this; x++; y++; return orig;} //postfix P operator--( int ) { P orig = *this; x--; y--; return orig; } void show() { cout << x << ’ ’ << y << ’\t’; } } ; 9 Overloading ++ / -- Operators Example (2) int main() { P p00, q; q = ++p00; q.show(); p00.show(); cout << endl; q = p00--; q.show(); p00.show(); cout << endl; q = --p00; q.show(); p00.show(); cout << endl; q = p00++; q.show(); p00.show(); cout << endl; return 0; } $ g++ pp.cpp $ a.out 1 1 1 1 1 1 0 0-1 -1-1 -1-1 -1 0 0 10 Binary Operator Overloading  Let OP be a binary operator and obj1 , obj2 be objects (not necessarily of the same class)....
View Full Document

Page1 / 53

Week 3 - 1 Week 3 2 Chapter 5 Operator Overloading Outline...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online