Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.

Similar presentations


Presentation on theme: "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator."— Presentation transcript:

1 Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator Overloading, Cont.

2 Presentation Schedule – Week 6 Tuesday, Oct. 20 – Basswood, Beech, Cherry, Walnut. Thursday, Oct. 22, Lecture Time – Hickory, Maple, Oak, Birch. Thursday, Oct. 22, Tutorial Time (room TBA) – Poplar, BalsamFir, Spruce, Cedar. Friday, Oct. 23 – Hemlock, JackPine, Tamarack, WhitePine. Fall 2015CISC/CMPE320 - Prof. McLeod2

3 MyComplex Class Demo See the start of a simple class to hold complex numbers: myComplex.h and myComplex.cpp: First version uses non-member overloading and accessors. Second version uses member overloading of the + operator. Third version uses non-member friend s and no accessors. Fall 2015CISC/CMPE320 - Prof. McLeod3

4 Version 3, Observations “friend” functions are declared in the scope of the class but are implemented like non-member functions. They can see private members of instances of the class they are “friends” with. A friend function cannot access the private members of its friend object, because it is not a member of that object. You don’t need, and cannot, declare them as const. Fall 2015CISC/CMPE320 - Prof. McLeod4

5 Summary of MyComplex, So Far Mixed type expressions (using your object) need conversion constructors, that will be invoked automatically to match types before the operator is evaluated. If you are going to use your binary operators in mixed type expressions it is best to overload them as non-member functions. If you do not wish to provide accessors, then non- member functions will have to be declared as friend s. So, we know how to overload the binary arithmetic operators and <<. Fall 2015CISC/CMPE320 - Prof. McLeod5

6 Fall 2015CISC/CMPE320 - Prof. McLeod6 Member or Non-Member Overloading? You will probably need to use both kinds. For example, assignment operators must be member functions, because you need to modify the LHS. Non-member functions cannot access private things in the class. A friend function can access private members from instances.

7 Fall 2015CISC/CMPE320 - Prof. McLeod7 Overloading Arithmetic Operators Binary: + - * / and % –Easy to do. Usually a non-member function will be best if you can use accessors for the private stuff (or are a friend function). Unary: - * and & (negation, pointer de-reference, address-of operator). –Also easy. If a non-member function, you only need a single parameter. A member function does not need any.

8 Fall 2015CISC/CMPE320 - Prof. McLeod8 Overloading Boolean Operators ==,, =, != Normally a non-member function. Return a bool. Consider writing a member function called something like “compare” that returns an int. compare will take two objects and return a negative int if the first is less than the second, zero if they are equal and a positive int if the first is greater than the second. The other comparison operators can invoke compare.

9 Fall 2015CISC/CMPE320 - Prof. McLeod9 Overloading Input and Output Stream output: << This operator takes the RHS, adds it to the stream obtained as the LHS and then returns this stream. As a non-member function: ostream& operator<<(ostream& out, const MyClass myC) { out << myC.convert(); return out; } convert() changes myC to something that can normally be handled by << (an atomic type, a string or a *char ).

10 Fall 2015CISC/CMPE320 - Prof. McLeod10 Overloading Input and Output, Cont. Stream input: >> As for output except you use istream& instead of ostream&.

11 Aside – Converting Between Strings and Numbers Applies to C++98: Use the stringstream stream ( #include ) See StringNumberConversion.cpp These functions also provide a “pre-look” at how to code with templates. (Like “generics” in Java). Gets around the problem of having to write separate functions for each numeric type. Avoids the use of C functions like atof() and atoi() in. Fall 2015CISC/CMPE320 - Prof. McLeod11

12 Converting Between Strings and Numbers, Cont. Should be much nicer now in C++11 The library has built-in functions for conversions in both directions. See the library docs. The current distro. (4.8.1) of MinGW still does not support the string conversion functions. But you can download a patch: http://tehsausage.com/mingw-to-string And follow the instructions on this site. This: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 says that the bug is fixed for 64 bit versions of MinGW only. Fall 2015CISC/CMPE320 - Prof. McLeod12

13 Fall 2015CISC/CMPE320 - Prof. McLeod13 Overloading Increment and Decrement Operators ++ and -- Pre-increment, ++x, increments by one then returns the value. Post-increment, x++, returns the value then increments by one. Member functions: MyClass& operator++(); // pre-increment MyClass operator++(int unused); // post-increment In the pre-increment form, use return *this; to return a reference to the current object.

14 Fall 2015CISC/CMPE320 - Prof. McLeod14 Overloading Assignment Operators The = operator is automatically generated for you, so you don’t have to overload it. By default the assignment operator carries out member-wise assignment. As long as you don’t have to carry out any dynamic memory management tasks with the heap, the default = should be fine. However you will still have to overload the combined assignment operators such as +=, -=, *=, /=, etc. (Invoke the binary operators…)

15 MyComplex Demo, Again See “version 4”: overloads ++ and +=. Which is faster? Pre-increment or post- increment? Fall 2015CISC/CMPE320 - Prof. McLeod15

16 Fall 2015CISC/CMPE320 - Prof. McLeod16 Overloading Conversion Operators You don’t need to write these for assignment 2. Consider that your Fraction class has top for the numerator and bottom for the denominator. To overload a cast to type double : Fraction::operator double() const { return static_cast (top) / bottom; } If test is a Fraction object, this allows stuff like: double aNum = static_cast (test);

17 Fall 2015CISC/CMPE320 - Prof. McLeod17 Overloading Conversion Operators, Cont. Similarly for int. Note the lack of return type. But if you have a conversion operator and an applicable conversion constructor this leads to an ambiguity: Which operator should be used in a mixed type expression? See MyComplexAmbiguous

18 Fall 2015CISC/CMPE320 - Prof. McLeod18 Overloading Conversion Operators, Cont. With just the constructor you can do things like: int aVal = 10; Fraction test = static_cast (aVal); Same as: Fraction test = Fraction(aVal); But you can’t do: aVal = static_cast (test);

19 Fall 2015CISC/CMPE320 - Prof. McLeod19 Overloading Conversion Operators, Cont. So, what’s to do? If you ditch the one number constructor, then you give yourself a lot more work when you need to overload operators for mixed expressions. Might be easiest to keep the constructor and compromise. Write member functions called intVal() and doubleVal() instead? Or declare the constructor explicit ?

20 Fall 2015CISC/CMPE320 - Prof. McLeod20 The explicit Keyword If you want a constructor to just be a constructor and not for use as a rule for implicit type conversions, declare it with explicit. For example: class Fraction { // etc. explicit Fraction(int); // etc. };

21 The explicit Keyword, Cont. If you use explicit : From the MyComplexAmbiguous demo, what is the result of a mixed type expression with a number on one side and a MyComplex instance on the other? Fall 2015CISC/CMPE320 - Prof. McLeod21

22 Fall 2015CISC/CMPE320 - Prof. McLeod22 Overloading the Subscript Operator: [ ] Appropriate for: –A data structure requiring access using an index. –A data structure that wants to make sure index values are legal. See the SafeArray class. –Note the two versions of operator[] used. –Note the use of a friend non-member function. –Note the use of a destructor (getting ahead of myself…)

23 Fall 2015CISC/CMPE320 - Prof. McLeod23 Overloading the Function Call Operator: ( ) Allows an object to behave as if it is a function. Called a function object. The only operator where the number of arguments is not fixed or limited to one or two. See the RandomInt class. Used in the STL.

24 Fall 2015CISC/CMPE320 - Prof. McLeod24 Aside - inline Functions Used in the RandomInt class. Used when the function body is short: –≤ 3 assignment statements. –one conditional. –one return. An inline function is not placed on the activation record stack, but is expanded in place and so is executed faster. Must be in declaration file. The downside is that a copy is made every time the function is invoked – so it should be short.


Download ppt "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator."

Similar presentations


Ads by Google