1 Today’s Objectives Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10...
-
Upload
kerry-todd -
Category
Documents
-
view
216 -
download
0
Transcript of 1 Today’s Objectives Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10...
![Page 1: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/1.jpg)
1
Today’s ObjectivesToday’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-20063-Jul-2006
![Page 2: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/2.jpg)
2
Progress ReviewProgress Review
![Page 3: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/3.jpg)
3
So far, we have learned…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 ReviewProgress Review
![Page 4: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/4.jpg)
4
Next…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 ReviewProgress Review
![Page 5: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/5.jpg)
5
Operator OverloadingOperator Overloading
Chapter 11
![Page 6: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/6.jpg)
6
Operators are Like FunctionsOperators 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 operatorstring air = "air", plane = "plane";string combined = air + plane;
The compiler generates the following function callstring combined = operator+( air, plane );
Operator Overloading (Deitel, 572–621; Savitch) Operator Overloading (Deitel, 572–621; Savitch)
lhs rhs
lhs rhs
operator
![Page 7: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/7.jpg)
7
Operators Can Be OverloadedOperators 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) Operator Overloading (Deitel, 572–621)
![Page 8: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/8.jpg)
8
Operators Can Be OverloadedOperators 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 madeBook book1, book2; //Instantiate two book objectsBook book3 = book1 + book2;
The operator + must be implemented as a public member functionclass Book{public: Book& operator+( const Book& rhs );//remainder of the code left out...};
Operator Overloading (Deitel, 572–621) Operator Overloading (Deitel, 572–621)
lhs
rhsoperatorlhs
rhs
![Page 9: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/9.jpg)
9
Operators that Can Be OverloadedOperators 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) Operator Overloading (Deitel, 572–621)
![Page 10: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/10.jpg)
10
RestrictionsRestrictions
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) Operator Overloading (Deitel, 572–621)
![Page 11: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/11.jpg)
11
Sample Class in Book.hSample 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) Operator Overloading (Deitel, 572–621)
![Page 12: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/12.jpg)
12
Binary OperatorsBinary 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;
• Implementationclass Book{
public: bool operator==( const Book& rhs ){
return this->title==rhs.title; }
Operator Overloading (Deitel, 572–621) Operator Overloading (Deitel, 572–621)
lhs rhsoperator
lhs
![Page 13: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/13.jpg)
13
Unary OperatorsUnary Operators
Only one operand Example: the not operator
• UsageBook book3;if( !book3 ) cout << "empty" << endl;
• Implementationclass Book{public:
bool operator!(){return ( title=="" && copies==0 );
}
Operator Overloading (Deitel, 572–621) Operator Overloading (Deitel, 572–621)
lhs
The conditions of an “empty” object
![Page 14: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/14.jpg)
14
Stream Insertion OperatorStream 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) Operator Overloading (Deitel, 572–621)
lhs rhsoperator
![Page 15: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/15.jpg)
15
Stream Insertion OperatorStream 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) Operator Overloading (Deitel, 572–621)
lhs rhs
A friend has access to the private data members
![Page 16: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/16.jpg)
16
Stream Extraction OperatorStream Extraction Operator
Implement as a friend function Example:
• Usagecin >> 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 whitespacein >> rhs.copies;return in;
}
Operator Overloading (Deitel, 572–621) Operator Overloading (Deitel, 572–621)
![Page 17: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/17.jpg)
17
Overloading ++Overloading ++
Special syntax to distinguish pre- from post-increment Example:
• Usagebook1++;cout << "Copies = " << book1.getCopies() << endl;
• Implementation as member functions of the Book classBook& operator++(){ //Pre-increment
++copies;return *this;
}
Book operator++(int){ //Post-incrementBook temp = *this;++copies;return temp;
}
Operator Overloading (Deitel, 572–621) Operator Overloading (Deitel, 572–621)
![Page 18: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/18.jpg)
18
Library DemoLibrary Demo
(Continued)
![Page 19: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/19.jpg)
19
Library DemoLibrary 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 ==
![Page 20: 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,](https://reader036.fdocuments.in/reader036/viewer/2022082713/5697bfee1a28abf838cb93c8/html5/thumbnails/20.jpg)
20
ReferencesReferences
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.