Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 1Operators 2H8.4.3-8.5; p254 – 266 Week 5 lesson 2InheritanceH9.1-9.3; p269 – 286 Week 6 lesson 1Virtual methodsH9.5-9.8; p301 – 322 (excluding 9.6) Week 6 lesson 2ExceptionsH10.1-10.2; p329 – 343

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

3 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; };

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

5 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?

6 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 friend class 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; }

7 Vector + The Vector class had an operator Vector 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??

8 Operator++ Two different ++ operators: int a = 10; c = 100; a = b++; d = ++d; Both can be specified for a class: const Vector & operator++(); // prefix Vector 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--(); // prefix Vector operator--( int ); // postfix

9 Operator[ ] Two different [ ] operators: x = a[ 15 ]; // get a value a[ 12 ] = 33; // store a value (change the array) Both can be specified for a class: int operator[]( int i ) const // get element i int & 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?

10 Extending the Vector class class 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); ++v v++ [ ] and [ ] << New: operator() int + Vector

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

12 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??

13 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

14 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?)


Download ppt "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."

Similar presentations


Ads by Google