220 likes | 232 Views
C++. Lecture 6 Tuesday, 19 July 2005. Chapter 8 Operator Overloading. Define operations on new data types with built-in operators Examples. Fundamentals of Operator Overloading. Operator overloading amounts to define a function to be associated with an operator
E N D
C++ Lecture 6 Tuesday, 19 July 2005
Chapter 8 Operator Overloading • Define operations on new data types with built-in operators • Examples
Fundamentals of Operator Overloading • Operator overloading amounts to define a function to be associated with an operator • Most built-in operators can be overloaded • The operators should be well-choosing • the precedence or "arity" of an overloaded operator can not change
Overloading Functions • The functions can be friend function or member function of a class, or even normal function • Use member function only if the left-most operand is a class object
Example, Overload << and >> • Define >> and << such that the follow statements work: PhoneNumber phone; cin >> phone; // phone is an object cout << "The phone number is" << phone << endl;
Output Overloading << friend ostream &operator<<(ostream &output, const PhoneNumber &num) { ouput << "(" << num.areaCode << ") " << num.exchange << "-" << num.line; return ouput; } C.F. Fig.8.3
<< is binary operator Thus a << b evokes a friend function of b f(a,b) I.e., operator<<(a,b) Or evokes a member funtion of a a.f(b) I.e. a.operator<<(b)
Form of Operator-Overloading Function • To overload op as in A op B we define a function of the form operator op(B) (member function) Or operator op(A, B) (non-member) where op can be +,-,/, %, =, !=, [ ], +=, etc
Overloading Unary Operators (member function) • Class function with no argument class String { public: bool operator!() const; … } • Used as !s • this will translate into s.operator()
Overloading Unary Operators (friend function) class String { friend bool operator!(const String &); … } • !s will be translated into operator!(s)
Overloading Binary Operators • Overloading += • y += z translate to y.operator+=(z) • where class String { public: const String &operator+=(const String &); … };
Overloading Binary Operators • Overloading += • y += z translate to operator+=(y,z) • where class String { public: friend const String &operator+=(String &, const String &); … };
Case Study, an Array Class • Do range checking with [ ] • use << and >> for I/O • use ==, != to compare arrays • use = to assign one array to another C.F. Fig. 8.4-6
Copy Constructor • The method used when we say • A(B) (where A and B are objects of the same class), or when we pass argument (by value) to function calls. • Array(const Array &);
Copy Constructor Array::Array(const Array &init): size(init.size) { ptr = new int[size]; assert(ptr != 0); ++arrayCount; for(int i = 0; i < size; i++) ptr[i] = init.ptr[i]; }
Destructor, release memory Array::~Array() { delete [ ] ptr; --arrayCount; }
Overload Assignment Operator const Array &Array::operator=(const Array &right) { if(&right != this) { if(size != right.size) { delete [ ] ptr; size = right.size; ptr = new int [size]; } } }
Overload [ ] int &Array::operator[ ] (int subscript) { assert(0 <= subscript && subscript < size); return ptr[subscript]; }
Case Study: A Date Class class Date { friend ostream &operator<<(… public: … Date &operator++(); // ++d Date operator++(int); // d++ const Date &operator+=(int); ….
Overloading ++ and -- • Signature to distinguish pre- (++d) and post- (d++) increment • Pre-increment: ++d1 is translated into d1.operator++() • whose prototype is d1.operator++( )
Overloading ++ and -- • Signature to distinguish pre- (++d) and post- (d++) increment • Post-increment: d1++ is translated into d1.operator++(0) • whose prototype is d1.operator++(int)
The Date Class • Increment date by ++, or increment by an arbitrary value by +=. Use << for output. • C.f. Fig.8.10-12