DOC PREVIEW
UD CISC 181 - Lecture 25

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 25December 1, 200928.1 Introduction• Use operators with objects (operator overloading)– Clearer than function calls for certain classes– Operator sensitive to context• Examples– << 2003 Prentice Hall, Inc. All rights reserved.• Stream insertion, bitwise left-shift– +• Performs arithmetic on multiple types (integers, floats, etc.)• Will discuss when to use operator overloading38.2 Fundamentals of Operator Overloading• Types– Built in (int, char) or user-defined– Can use existing operators with user-defined types• Cannot create new operators• Overloading operators 2003 Prentice Hall, Inc. All rights reserved.– Create a function for the class– Name function operator followed by symbol• Operator+ for the addition operator +48.2 Fundamentals of Operator Overloading• Using operators on a class object– It must be overloaded for that class• Exceptions:• Assignment operator, =– Memberwise assignment between objects•Address operator& 2003 Prentice Hall, Inc. All rights reserved.•Address operator, &– Returns address of object• Both can be overloaded• Overloading provides concise notation– object2 = object1.add(object2);– object2 = object2 + object1;58.3 Restrictions on Operator Overloading• Cannot change– How operators act on built-in data types• I.e., cannot change integer addition– Precedence of operator (order of evaluation)• Use parentheses to force order-of-operationsA i ti it (l fttiht ihttlft) 2003 Prentice Hall, Inc. All rights reserved.–Associativity (left-to-right or right-to-left)– Number of operands• & is unitary, only acts on one operand• Cannot create new operators• Operators must be overloaded explicitly– Overloading + does not overload +=68.3 Restrictions on Operator OverloadingOperators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[]  2003 Prentice Hall, Inc. All rights reserved.Operators that cannot be overloaded . .* :: ?: sizeof278.4 Operator Functions As Class Members Vs. As Friend Functions• Operator functions– Member functions•Use this keyword to implicitly get argument• Gets left operand for binary operators (like +)• Leftmost object must be of same class as operatorNon member functions 2003 Prentice Hall, Inc. All rights reserved.–Non member functions• Need parameters for both operands• Can have object of different class than operator• Must be a friend to access private or protected data– Called when• Left operand of binary operator of same class• Single operand of unitary operator of same class88.4 Operator Functions As Class Members Vs. As Friend Functions• Overloaded << operator– Left operand of type ostream &• Such as cout object in cout << classObject– Similarly, overloaded >> needs istream &– Thus, both must be non-member functions 2003 Prentice Hall, Inc. All rights reserved.98.4 Operator Functions As Class Members Vs. As Friend Functions• Commutative operators–May want + to be commutative • So both “a + b” and “b + a” work– Suppose we have two different classes– Overloaded operator can only be member function when its class is on left 2003 Prentice Hall, Inc. All rights reserved.class is on left• HugeIntClass + Long int• Can be member function– When other way, need a non-member overload function• Long int + HugeIntClass108.5 Overloading Stream-Insertion and Stream-Extraction Operators• << and >>– Already overloaded to process each built-in type– Can also process a user-defined class• Example program– Class PhoneNumber 2003 Prentice Hall, Inc. All rights reserved.• Holds a telephone number– Print out formatted number automatically• (123) 456-7890Outline11fig08_03.cpp(1 of 3)1 // Fig. 8.3: fig08_03.cpp2 // Overloading the stream-insertion and 3 // stream-extraction operators.4 #include <iostream>5 6 using std::cout;7 using std::cin;8 using std::endl;9 using std::ostream;10 using std::istream;11 12 #include <iomanip>13 14 using std::setw;15 Notice function prototypes for overloaded operators >> and << 2003 Prentice Hall, Inc.All rights reserved.16 // PhoneNumber class definition17 class PhoneNumber {18 friend ostream &operator<<( ostream&, const PhoneNumber & );19 friend istream &operator>>( istream&, PhoneNumber & ); 20 21 private:22 char areaCode[ 4 ]; // 3-digit area code and null23 char exchange[ 4 ]; // 3-digit exchange and null24 char line[ 5 ]; // 4-digit line and null25 26 }; // end class PhoneNumberThey must be non-member friendfunctions, since the object of class Phonenumber appears on the right of the operator.cin << objectcout >> objectOutline12fig08_03.cpp(2 of 3)27 28 // overloaded stream-insertion operator; cannot be 29 // a member function if we would like to invoke it with 30 // cout << somePhoneNumber; 31 ostream &operator<<( ostream &output, const PhoneNumber &num )32 { 33 output << "(" << num.areaCode << ") " 34 << num.exchange << "-" << num.line; 35 36 return output; // enables cout << a << b << c; 37 38 } // end function operator<< 39 40 // overloaded stream-extraction operator; cannot be 41 // a member function if we would like to invoke it with The expression:cout << phone;is interpreted as the function call:operator<<(cout, phone);output is an alias for cout.This allows objects to be cascaded.cout << phone1 << phone2;ignore()skips specified 2003 Prentice Hall, Inc.All rights reserved.42 // cin >> somePhoneNumber; 43 istream &operator>>( istream &input, PhoneNumber &num ) 44 { 45 input.ignore(); // skip ( 46 input >> setw( 4 ) >> num.areaCode; // input area code 47 input.ignore( 2 ); // skip ) and space48 input >> setw( 4 ) >> num.exchange; // input exchange 49 input.ignore(); // skip dash (-) 50 input >> setw( 5 ) >> num.line; // input line 51 52 return input; // enables cin >> a >> b >> c; cout << phone1 << phone2;first calls operator<<(cout, phone1), and returns cout. Next, cout << phone2 executes.ignore()skips specified number of characters from input (1 by


View Full Document
Download Lecture 25
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 25 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 25 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?