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

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 Presentation Schedule – Week 6, Cont. Plan on 10 minutes. Few questions? You must fill out peer evaluations in Moodle on the team’s Wiki and on their presentation – but not for your own team! Attendance sheets will be circulated – if you are not there, you cannot evaluate the presentation! Fall 2015CISC/CMPE320 - Prof. McLeod3

4 Fall 2015CISC/CMPE320 - Prof. McLeod4 Team Presentations, Cont. Talking to upper management. Describe your project, its scope, the inherent challenge. You have lots of material from your RAD. Describe how you intend to complete your project: –Team role assignments –Time line (what you will do when) What you have done so far. Prototypes? Minimum one person to present (!), but everyone on the team should have a chance to review and comment on or edit the presentation.

5 Fall 2015CISC/CMPE320 - Prof. McLeod5 Operator Overloading – Java You should remember that the binary + operator is overloaded in Java: 2 + 3 is 5 “two” + “three” is “twothree” 2 + “three” is “2three” “two” + 3 is “two3” So the result of the operator depends on the type of the operands. If there is a String literal on one side then the other side is converted to a String and a concatenation takes place.

6 Fall 2015CISC/CMPE320 - Prof. McLeod6 + and string Objects in C++ string two("two"); string three("three"); string sum1 = two + three; // sum1 is "twothree" string sum2 = 2 + three; // doesn’t work Note that a string literal is actually a char* object and the + operator does not work with them if they are on both sides of the operator.

7 Fall 2015CISC/CMPE320 - Prof. McLeod7 + and string Objects in C++, Cont. However, something like: string two("two"); string sum1 = two + "five"; does work. The char* literal is converted to a string object first. So, the operation that takes place depends on the types of the operands on either side of the operator.

8 Operator Overloading in string Class See cplusplus.com for list of overloaded operators in this class: = [ ] += + == != >= >> << Fall 2015CISC/CMPE320 - Prof. McLeod8

9 Fall 2015CISC/CMPE320 - Prof. McLeod9 Operator Overloading This flexibility is a result of operator overloading in the string class – a task carried out by the class designer to make his class easier to use. In Java operator overloading is almost non- existent. For example, if a, b and c are BigIntegers, to carry out the operation: x = a + b * c; you would have to write x = a.add(b.multiply(c));

10 Fall 2015CISC/CMPE320 - Prof. McLeod10 Overloadable Operators in C++ +-*/%^& |~!=<>+= -=*=/=%=^=&=|= <<>>>>=<<===!=<= >=&&||++--->*, ->[]()newnew[] deletedelete[]

11 Fall 2015CISC/CMPE320 - Prof. McLeod11 Operator Functions You can overload an operator as a member or a non- member function. Overloading function header syntax: return_type operatoroperator_symbol(parameters) For example, non-member overloading the + operator for an object called MyClass : MyClass operator+(const MyClass& lhs, const MyClass& rhs);

12 Fall 2015CISC/CMPE320 - Prof. McLeod12 Operator Functions, Cont. Member overloading functions: For example, overloading the + operator for an object called MyClass : MyClass operator+(const MyClass& rhs) const; The object owning the function is the LHS operand in this case.

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

14 Version 1, Observations Overloaded binary operator should return a copy of the value, not a reference! (How does operator<< get away with returning a reference?) Non-member overloadings must have accessors to get at attributes. Used the single parameter constructor as conversion constructor to allow mixed type expressions with int or double on either side. Nice! What would you have to do if this did not work this way? Fall 2015CISC/CMPE320 - Prof. McLeod14

15 Version 2, Observations Member overloading does not need accessors. Current object is LHS of binary operation. Parameter is RHS. Mixed type expressions cannot be evaluated if LHS is not a MyComplex object. Fall 2015CISC/CMPE320 - Prof. McLeod15

16 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 the class. Fall 2015CISC/CMPE320 - Prof. McLeod16

17 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. McLeod17

18 Fall 2015CISC/CMPE320 - Prof. McLeod18 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, unless they are a friend function.

19 Fall 2015CISC/CMPE320 - Prof. McLeod19 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.

20 Fall 2015CISC/CMPE320 - Prof. McLeod20 friend Functions, Summary (Classes can also be declared friend s.) A function that is declared as a friend function can be implemented as a non-member function but can still access and change private data fields. The function prototype must be inside the class definition (before the }; )

21 Fall 2015CISC/CMPE320 - Prof. McLeod21 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.

22 Fall 2015CISC/CMPE320 - Prof. McLeod22 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 << (a fundamental type, a string or a *char ).

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

24 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. McLeod24

25 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. McLeod25


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