Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6...

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

Transcript of Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6...

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 1

CISC/CMPE320

• RAD due Friday in your Wiki.• Presentations week 6 – next week. Schedule on

next slide.

• Today:– Operator Overloading

Page 2: 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 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 2015 CISC/CMPE320 - Prof. McLeod 2

Page 3: 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 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 2015 CISC/CMPE320 - Prof. McLeod 3

NTD!!

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 4

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.

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 5

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.

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 6

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

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 7

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

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

Operator Overloading in string Class

• See cplusplus.com for list of overloaded operators in this class:

= [ ] += + == != < <= > >= >> <<

Fall 2015 CISC/CMPE320 - Prof. McLeod 8

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 9

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

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 10

Overloadable Operators in C++

+ - * / % ^ &

| ~ ! = < > +=

-= *= /= %= ^= &= |=

<< >> >>= <<= == != <=

>= && || ++ -- ->* ,

-> [] () new new[] delete delete[]

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 11

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

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 12

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.

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

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 friends and no accessors.

Fall 2015 CISC/CMPE320 - Prof. McLeod 13

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

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 2015 CISC/CMPE320 - Prof. McLeod 14

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

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 2015 CISC/CMPE320 - Prof. McLeod 15

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

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 2015 CISC/CMPE320 - Prof. McLeod 16

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

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

• So, we know how to overload the binary arithmetic operators and <<.

Fall 2015 CISC/CMPE320 - Prof. McLeod 17

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 18

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.

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 19

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.

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 20

friend Functions, Summary

• (Classes can also be declared friends.)• 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 };)

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 21

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.

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 22

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

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

Fall 2015 CISC/CMPE320 - Prof. McLeod 23

Overloading Input and Output, Cont.

• Stream input: >>• As for output except you use istream& instead

of ostream&.

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

Aside – Converting Between Strings and Numbers

• Applies to C++98:• Use the stringstream stream

(#include <sstream>)

• 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 <cstdlib>.

Fall 2015 CISC/CMPE320 - Prof. McLeod 24

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

Converting Between Strings and Numbers, Cont.

• Should be much nicer now in C++11• The <string> library has built-in functions for conversions

in both directions.• See the <string> 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 2015 CISC/CMPE320 - Prof. McLeod 25