1 / 14

Mastering Operator Overloading in C++

Learn the essentials of operator overloading in C++, including characteristics, methods, and examples to enhance your programming skills

tinad
Download Presentation

Mastering Operator Overloading in 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. CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel

  2. Orthodox Canonical Class Form • Most classes should be defined with (at least) these four important member functions • Default constructor ClassName(); • Copy constructor ClassName(const ClassName &copy); • Assignment operator (overloaded operator) const ClassName &operator=(const ClassName &copy); • Destructor ClassName::~ClassName(); CSCI 383 Lecture 13 M. van Bommel

  3. Operator Overloading • Most C++ operators can be overloaded • Overloaded operators are implemented using the C++ keyword operator • There are some characteristics of the operators that cannot be changed when overloading them • New operator names cannot be introduced • Built-in operators cannot be overriden • Operator precedence cannot be changed • Operator associativity cannot be changed • The number of operator parameters cannot be changed CSCI 383 Lecture 13 M. van Bommel

  4. Operator Overloading • Handout #2 • Notice that the Timer class contains an overloaded addition operator, which is a binary operator • timer1 + timer2 • In the addition operator of Timer, the receiver of the message is the left (first) argument of “+” (timer1), and the argument is the second argument of “+” (timer2) CSCI 383 Lecture 13 M. van Bommel

  5. Global vs. Method Operators • Instead, one could defined the previous overloaded operator as a global function rather than as a method Timer operator+(const Timer& t1, const Timer& t2) { … } • Notice the difference between the global and the method definition: the first argument is missing in the method version…or is it? • this serves as the first (left) argument of the operator • this is a built-in, self-referential identifier; it contains the address of the receiving object • In such a case, an operator call such as time1 + time2is a message sent to the Timer instance time1 CSCI 383 Lecture 13 M. van Bommel

  6. Operator Overloading • Also notice the return value is a copy of a timer return result; • Must ensure that copy constructor is included (if necessary) • Copy is used for result of “+”, as in c = a + b • The Timer class also contains an overloaded assignment operator, which is a binary operator • a = b • In the assignment operator, the receiver of the message is the left (first) argument of “=”, and the argument is the second argument of “=” • This time the return value is a reference to a timer object • used in string of assignments c = b = a CSCI 383 Lecture 13 M. van Bommel

  7. Global vs. Method Operators • Overloaded operators can be called in either operator form or functional form • time0 = time1 + time2; // operator form • time0 = time1.operator+(time2) // functional form • The same rules hold for unary operators. For example, consider the “not” operators (i.e., “!”) • int operator!(const Timer& t); // as global function • int Timer::operator!() const; // as method • Equivalent calls to overloaded operator ! • if (!time1) … • if (time1.operator!()) … CSCI 383 Lecture 13 M. van Bommel

  8. The Subscripting Operator • The second example of handout #2 illustrates an implementation of an overloaded subscripting operator (i.e., “[]”) • Here is an example that uses it IntArray c1(5); for(int i=0; i<5; i++) { c1[i] = i; cout << c1[i] << endl; } CSCI 383 Lecture 13 M. van Bommel

  9. The Function Call Operator • The second example of handout #2 also illustrates an implementation of an overloaded function call operator (i.e., “()”) • Here is an example that uses it IntArray c1(5); // … for(int i=0; i<5; i++) cout << c1() << endl; • The function call operator can be defined with zero or more parameters in its parameter list CSCI 383 Lecture 13 M. van Bommel

  10. Conversion Operators • A conversion operator converts an instance of a class to an instance of a built-in class (type) Timer::operator int() { return hours*3600 + minutes*60 + int(seconds); } • Conversion operators have no return type specified explicitly since the name of the operator specifies the return type. They also always have a void parameter list CSCI 383 Lecture 13 M. van Bommel

  11. Overloaded Operator Q&A Q: What if one wants to use objects as conditions? For example: if (time1) … if (!time1) … A: Define these operators as the methods: operator int(); int operator!(); CSCI 383 Lecture 13 M. van Bommel

  12. Overloaded Operator Q&A Q: What if one wants to overload both prefix and infix ++? For example, ++time time++ A: In the C++ standard, the two are distinguished as illustrated in the following examples CSCI 383 Lecture 13 M. van Bommel

  13. Prefix Overloaded Method Timer Timer::operator++() { if (++seconds >= 60.0) { minutes++; seconds -= 60.0; } if (minutes >= 60) { hours++; minutes -= 60; } return *this; } CSCI 383 Lecture 13 M. van Bommel

  14. Postfix Overloaded Method Timer Timer::operator++(int i) { Timer temp = *this; if (++seconds >= 60.0) { minutes++; seconds -= 60.0; } if (minutes >= 60) { hours++; minutes -= 60; } return temp; } WARNING! Since no actual value is sent to the int parameter, do not use the parameter in the method’s code! CSCI 383 Lecture 13 M. van Bommel

More Related