1 / 22

C++

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

hollyp
Download Presentation

C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Lecture 6 Tuesday, 19 July 2005

  2. Chapter 8 Operator Overloading • Define operations on new data types with built-in operators • Examples

  3. 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

  4. 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

  5. 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;

  6. Output Overloading << friend ostream &operator<<(ostream &output, const PhoneNumber &num) { ouput << "(" << num.areaCode << ") " << num.exchange << "-" << num.line; return ouput; } C.F. Fig.8.3

  7. << 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)

  8. 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

  9. 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()

  10. Overloading Unary Operators (friend function) class String { friend bool operator!(const String &); … } • !s will be translated into operator!(s)

  11. Overloading Binary Operators • Overloading += • y += z translate to y.operator+=(z) • where class String { public: const String &operator+=(const String &); … };

  12. Overloading Binary Operators • Overloading += • y += z translate to operator+=(y,z) • where class String { public: friend const String &operator+=(String &, const String &); … };

  13. 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

  14. 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 &);

  15. 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]; }

  16. Destructor, release memory Array::~Array() { delete [ ] ptr; --arrayCount; }

  17. 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]; } } }

  18. Overload [ ] int &Array::operator[ ] (int subscript) { assert(0 <= subscript && subscript < size); return ptr[subscript]; }

  19. Case Study: A Date Class class Date { friend ostream &operator<<(… public: … Date &operator++(); // ++d Date operator++(int); // d++ const Date &operator+=(int); ….

  20. Overloading ++ and -- • Signature to distinguish pre- (++d) and post- (d++) increment • Pre-increment: ++d1 is translated into d1.operator++() • whose prototype is d1.operator++( )

  21. 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)

  22. The Date Class • Increment date by ++, or increment by an arbitrary value by +=. Use << for output. • C.f. Fig.8.10-12

More Related