Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.

Similar presentations


Presentation on theme: "CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel."— Presentation transcript:

1 CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel

2 Fall 2010CSCI 383 Lecture 13 M. 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();

3 Fall 2010CSCI 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

4 Fall 2010CSCI 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)

5 Fall 2010CSCI 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 + time2 is a message sent to the Timer instance time1

6 Fall 2010CSCI 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

7 Fall 2010CSCI 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!()) …

8 Fall 2010CSCI 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., “[]”)handout #2 Here is an example that uses it IntArray c1(5); for(int i=0; i<5; i++) { c1[i] = i; cout << c1[i] << endl; }

9 Fall 2010CSCI 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., “()”)handout #2 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

10 Fall 2010CSCI 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

11 Fall 2010CSCI 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!();

12 Fall 2010CSCI 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

13 Fall 2010CSCI 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; }

14 Fall 2010CSCI 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!


Download ppt "CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel."

Similar presentations


Ads by Google