w 5 l 1 sh 1 lessonsubjectbook week 4 lesson 1class, copy-constructorh7.1-7.4; p197 – 226 week 4...

14
W 5 L 1 sh 1 Lesson Subject Book Week 4 lesson 1 Class, copy- constructor H7.1-7.4; p197 – 226 Week 4 lesson 2 Operators 1 H8.1-8.4.2; p237 – 254 Week 5 lesson 1 Operators 2 H8.4.3-8.5; p254 – 266 Week 5 lesson 2 Inheritance H9.1-9.3; p269 – 286 Week 6 lesson 1 Virtual methods H9.5-9.8; p301 – 322 (excluding 9.6) Week 6 lesson 2 Exceptions H10.1-10.2; p329 – 343

Upload: merilyn-atkins

Post on 06-Jan-2018

217 views

Category:

Documents


1 download

DESCRIPTION

Operator

TRANSCRIPT

Page 1: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

W 5 L 1 sh 1

Lesson Subject Book

Week 4 lesson 1 Class, copy-constructor H7.1-7.4; p197 – 226

Week 4 lesson 2 Operators 1 H8.1-8.4.2; p237 – 254

Week 5 lesson 1 Operators 2 H8.4.3-8.5; p254 – 266

Week 5 lesson 2 Inheritance H9.1-9.3; p269 – 286

Week 6 lesson 1 Virtual methods H9.5-9.8; p301 – 322 (excluding 9.6)

Week 6 lesson 2 Exceptions H10.1-10.2; p329 – 343

Page 2: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator <<

<< is the C++ operator for formatted printing. cout is an instance of ostream. So we need an ostream :: operator<<( Person ) But we can’t change ostream (it’s in a library) Adding operator<< to Person makes no sense (why?).

class Person {public: // Constructor. Person (const char * n, int a); private: char name[30]; int age;}; Person mw (“M.Wensink”, 48);cout << mw;

Page 3: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator <<

Luckily we can also make a ‘free floating’ operator:

ostream & operator<<( ostream & os, const Person & p){ os << p.name << “ (“ << p.age << ‘)’; return os;}

class Person {public: // Constructor. Person (const char * n, int a); private: char name[30]; int age;};

Page 4: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator <<

Take a closer look:

ostream & operator<<( ostream & os, const Person & p ){ os << p.name << “ (“ << p.age << ‘)’; return os;}

Why return ostream &?

Why not const ostream & ?

Why const? Why & ?

Person mw ( ”M.Wensink”, 48);cout << mw < newline;

Page 5: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator <<

ostream & operator<<( ostream & os, const Person & p){ os << p.name << “ (“ << p.age << ‘)’; return os;}

class Person {public: // Constructor. Person (const char * n, int a); private: char name[30]; int age;};

What is the problem with this function?

Page 6: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator <<

Solutions: make the attributes public provide get methods for all attributes delegate printing to a method (we will do that later) mark the operator<< as friendclass Person {public: Person (const char * n, int a); friend ostream & operator<< ( stream & os, const Person & p);private: char name[30]; int age;};

ostream & operator<< (ostream & os, const Person & p){ os << p.name << ” (” << p.age << ’)’; return os;}

Page 7: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Vector +

The Vector class had an operatorVector Vector::operator+( int n ) const;

Vector v, w; v = w + 7;

So we could write

It would be nice to have the reverse too:Vector v, w; v = 7 + w;

7 is an integer, not a class, and we could not change it anyway if it were a class, so we need a ‘free’ function: Vector operator+( int n, const Vector & v ){ return v + n;}

No ‘const’ here??

Page 8: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator++

Two different ++ operators:int a = 10; c = 100;a = b++;d = ++d;

Both can be specified for a class:const Vector & operator++(); // prefixVector operator++( int ); // postfix

Operator-- is the same:

Note that the return types are also different, this is a consequence of the implementation.

const Vector & operator--(); // prefixVector operator--( int ); // postfix

Page 9: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Operator[ ]

Two different [ ] operators:x = a[ 15 ]; // get a valuea[ 12 ] = 33; // store a value (change the array)

Both can be specified for a class:int operator[]( int i ) const // get element iint & operator[]( int i ) // change element i

Explain the two differences.Note that the second form is not like this:const int & operator[]( int i ) // change element i

why?

Page 10: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Extending the Vector classclass Vector {public: ... // Increment operators const Vector & operator++(); // prefix Vector operator++(int); // postfix  // Index operators int operator[](int i) const; int & operator[](int i);  // I/O friend function friend ostream & operator<<( ostream & os, const Vector & v );  // Take a slice of a vector Vector operator()( int from, int to ) const; ...};

// operator+Vector operator+ (int n, const Vector & rhs);

++vv++

[ ] and [ ]

<<

New: operator()

int + Vector

Page 11: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Implementation: ++

const Vector & Vector::operator++ (){ return (*this += 1);}

Vector Vector::operator++ (int){ Vector old (*this); *this += 1; return old;}

Add one and return the new object itself.

Save a copy Add one to the object Return the saved copy

The prefix version is always much simpler, because no copies are needed.

Page 12: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Implementation: [ ]

int Vector::operator[]( int index ) const { assert( index >= i1 && index <= last()); return pVec[ index - i1 ];} int & Vector::operator[]( int index ){ assert( index >= i1 && index <= last() ); return pVec[ index - i1 ];}

What is the difference??

Page 13: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Implementation: ( )

Vector Vector::operator()( int from, int to ) const { assert (from >= i1 && to <= last() && from <= to);  Vector tmp (from, to - from + 1); for (int i = 0; i < tmp.num; i++){ tmp.pVec[i] = pVec[from - i1 + i]; } return tmp;}

Create a temporary vector tmp, Copy a subslice from the vector to this tmp Return this tmp

Page 14: W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH7.1-7.4; p197 – 226 Week 4 lesson 2Operators 1H8.1-8.4.2; p237 – 254 Week 5 lesson

Implementation: <<, +

ostream & operator<< (ostream & os, const Vector & v ){ for( int i = 0; i < v.num; i++ ){ os << v.pVec[ i ] << ' '; } return os;}

Vector operator+( int n, const Vector & rhs ){ return rhs + n;}

Note: no Vector:: before operator<< (why?)