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.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CMSC 2021 Stream I/O Operators and Friend Functions.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Generic Positional Containers and Double-Ended Queues.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Pointers OVERVIEW.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
1 Chapter 8 Operator Overloading. 2 User-defined Meaning for “built-in” Operators zNatural, suggestive usage yComplex number addition, subtraction, etc.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
W 4 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Design Patterns Solving problems with already known solutions Unit - 13.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 Generic Positional Containers and Double-Ended Queues.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Introduction to Programming
Templates.
The dirty secrets of objects
Overloading the << operator
Operator Overloading; String and Array Objects
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Lesson Subject Book Week 4 lesson 1 Class, copy-constructor
COP 3330 Object-oriented Programming in C++
C++ Templates L03 - Iterator 10 – Iterator.
Operator Overloading; String and Array Objects
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Overloading the << operator
C++ Templates L04 - Iterator 10 – Iterator.
Presentation transcript:

W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson 1Operators 2H ; p254 – 266 Week 5 lesson 2InheritanceH ; p269 – 286 Week 6 lesson 1Virtual methodsH ; p301 – 322 (excluding 9.6) Week 6 lesson 2ExceptionsH ; p329 – 343

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;

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

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;

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?

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

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

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

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?

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

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.

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

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

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