200 likes | 278 Views
3-Jul-2006. 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
E N D
3-Jul-2006 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 --
Progress Review 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 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
Operator Overloading Chapter 11
Operator Overloading (Deitel, 572–621; Savitch) 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 ); lhs rhs operator lhs rhs
Operator Overloading (Deitel, 572–621) 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 42 + 58; 98.6 + 0.3; • 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) 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... }; lhs rhs operator lhs rhs
Operator Overloading (Deitel, 572–621) Operators that Can Be Overloaded • Fig. 11.1, page 574 • One operator that neverhas 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) 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) Sample Class in Book.h #ifndef BOOK_H #define BOOK_H #include <string> 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) Binary Operators • Operands are placed on both sides of the operators • Example: the equality operator • UsageBook 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; } lhs rhs operator lhs
Operator Overloading (Deitel, 572–621) 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 ); } lhs The conditions of an “empty” object
Operator Overloading (Deitel, 572–621) 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 lhs rhs operator
Operator Overloading (Deitel, 572–621) 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; } lhs rhs A friend has access to the private data members
Operator Overloading (Deitel, 572–621) 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) 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; }
Library Demo (Continued)
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 ==
References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, 2003. Lippman, Stanley B., and Josee Lajoie, C++ Primer. Boston: Addison-Wesley, 1998. Savitch, W., Problem Solving with C++, Fifth Edition. Boston: Addison-Wesley, 2005.