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,

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Operator Overloading Fundamentals
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 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.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Chapter 8 Operator Overloading, Friends, and References.
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.
 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.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS Object Oriented Programming Using C++
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
1 Today’s Objectives  Announcements Homework #5 is due today. Since this date is so close to the end of the semester, no late assignments will be accepted.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Today’s Objectives 5-Jul-2006 Announcements
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
Operator Overloading.
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Today’s Objectives 28-Jun-2006 Announcements
Presentation transcript:

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, 5-Jul Link to a grade estimator is in the Announcements of our class page Last day to drop a 9-week class without grade penalty is 5-Jul. To drop, contact UHCL directly  Progress review  Using string and stringstream classes  Operator Overloading (Ch. 11) Fundamentals of operator overloading Overloading binary and unary operators Overloading stream insertion and stream extraction operators Overloading ++ and -- 3-Jul-2006

2 Progress Review

3 So far, we have learned…  Object-Oriented Programming Basics of C++ Functions and argument passing Arrays Pointers and dynamic memory allocation C++ classes and objects Some C++ standard libraries and their namespace The STL vector class Programs that use objects to solve problems Debugging techniques Encapsulation Information hiding  Object-Oriented Analysis and Design A simple software process Pseudocode algorithms UML class diagrams and use case diagrams Progress Review

4 Next…  The string class and the stringstream class  Operator overloading  Inheritance  Polymorphism  Templates  Stream I/O  Exception handling  File processing  Linked lists  The g++ compiler Progress Review

5 Operator Overloading Chapter 11

6 Operators are Like Functions  With objects, an operator, like “ + ”, is really just a function with a different syntax  With a binary operator, the arguments are placed on either side of the operator string air = "air", plane = "plane"; string combined = air + plane;  The compiler generates the following function call string combined = operator+( air, plane ); Operator Overloading (Deitel, 572–621; Savitch) lhsrhs lhsrhs operator

7 Operators Can Be Overloaded  Just like any function, operators can be overloaded  Operators are already overloaded so that they “do the right thing” with all built-in data types, like int and double ; ;  Also with classes in the standard library, like string string result; result = result + "test" ;  But operators are not automatically overloaded for classes that we create Operator Overloading (Deitel, 572–621)

8 Operators Can Be Overloaded  We have to overload operators for the classes that we create  The implementation of an overloaded operator is usually a member function of the class of the object used on the lhs  Example of a binary operator used with objects of a class we made Book book1, book2; //Instantiate two book objects Book book3 = book1 + book2;  The operator + must be implemented as a public member function class Book { public: Book& operator+( const Book& rhs ); //remainder of the code left out... }; Operator Overloading (Deitel, 572–621) lhs rhs operator lhs rhs

9 Operators that Can Be Overloaded  Fig. 11.1, page 574  One operator that never has to be overloaded operator& –The “address of” operator –Can be used with any object to get its address  One operator that has to be overloaded only when a data member is created with “new” operator= –Assignment operator –Can be used with any objects for default memberwise assignment –Must be overloaded when default memberwise assignment doesn’t work correctly Operator Overloading (Deitel, 572–621)

10 Restrictions  Precedence of operators cannot be changed  Arity of operators cannot be changed (“arity” is the number of operands)  New operators cannot be created  Operators for built-in data types cannot be overloaded  Some operators cannot be overloaded Fig. 11.2, page 574 Operator Overloading (Deitel, 572–621)

11 Sample Class in Book.h #ifndef BOOK_H #define BOOK_H #include using std::string; class Book{ public: Book( string t="",int cpy=0 ): title(t), copies(cpy){} void setTitle( string t ){ title = t; } string getTitle(){ return title; } void setCopies ( int cpy ){ copies = cpy; } int getCopies(){ return copies; } string toString(){ return "Book: " + title; } private: string title; int copies; }; #endif Operator Overloading (Deitel, 572–621)

12 Binary Operators  Operands are placed on both sides of the operators  Example: the equality operator Usage Book book1("C++ How to Program",1); Book book2("Java How to Program",1); if( book1 == book3 ) cout << "Equal" << endl; Implementation class Book{ public: bool operator==( const Book& rhs ){ return this->title==rhs.title; } Operator Overloading (Deitel, 572–621) lhsrhs operator lhs

13 Unary Operators  Only one operand  Example: the not operator Usage Book book3; if( !book3 ) cout << "empty" << endl; Implementation class Book{ public: bool operator!(){ return ( title=="" && copies==0 ); } Operator Overloading (Deitel, 572–621) lhs The conditions of an “empty” object

14 Stream Insertion Operator  The stream insertion operator is a binary operator  Usage cout << "Hello";  The lhs is a C++ object of the ostream class which is in the iostream library  The ostream class knows how to output any of the built- in C++ data types and any standard library data types  We have to overload << for our own classes, but we can’t put the overloaded operator into the lhs class Operator Overloading (Deitel, 572–621) lhsrhs operator

15 Stream Insertion Operator  We implement << as a global function and then declare it as a friend of our own class  Example: Usage cout << book1; Implementation class Book{ friend ostream& operator<<( ostream& out, const Book& rhs ); //remainder of the Book class here }; Global function definition: ostream& operator<<( ostream& out, const Book& rhs ){ out << rhs.title; return out; } Operator Overloading (Deitel, 572–621) lhsrhs A friend has access to the private data members

16 Stream Extraction Operator  Implement as a friend function  Example: Usage cin >> book3; //Already declared like this: Book book3; Implementation class Book{ friend istream& operator>>( istream& in, Book& rhs ); //remainder of the Book class here }; Global function definition: istream& operator>>( istream& in, Book& rhs ){ in >> rhs.title;//captures a single word, no whitespace in >> rhs.copies; return in; } Operator Overloading (Deitel, 572–621)

17 Overloading ++  Special syntax to distinguish pre- from post-increment  Example: Usage book1++; cout << "Copies = " << book1.getCopies() << endl; Implementation as member functions of the Book class Book& operator++(){ //Pre-increment ++copies; return *this; } Book operator++(int){ //Post-increment Book temp = *this; ++copies; return temp; } Operator Overloading (Deitel, 572–621)

18 Library Demo (Continued)

19 Library Demo  Progress review Implemented two classes in the first version –Book class –BookList class –Library class  TODO Add to the Book class –operator>> –operator<< –operator== Use these three operators of the Book class –Use >> in main to get the input for a new book –Use << in the printList member function of BookList –Add a find member function to BookList and use ==

20 References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, Lippman, Stanley B., and Josee Lajoie, C++ Primer. Boston: Addison- Wesley, Savitch, W., Problem Solving with C++, Fifth Edition. Boston: Addison- Wesley, 2005.