DOC PREVIEW
UD CISC 181 - Introduction to Computer Science

This preview shows page 1-2-3-4-5-6 out of 17 pages.

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

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 20098.1 Introduction8.2 Fundamentals of Operator OverloadingSlide 48.3 Restrictions on Operator OverloadingSlide 68.4 Operator Functions As Class Members Vs. As Friend FunctionsSlide 8Slide 98.5 Overloading Stream-Insertion and Stream-Extraction Operatorsfig08_03.cpp (1 of 3)fig08_03.cpp (2 of 3)fig08_03.cpp (3 of 3) fig08_03.cpp output (1 of 1)8.6 Overloading Unary OperatorsSlide 158.7 Overloading Binary OperatorsSlide 171CISC181 Introduction to Computer ScienceDr. McCoyLecture 25December 1, 2009 2003 Prentice Hall, Inc. All rights reserved.28.1 Introduction•Use operators with objects (operator overloading)–Clearer than function calls for certain classes–Operator sensitive to context•Examples–<<•Stream insertion, bitwise left-shift–+•Performs arithmetic on multiple types (integers, floats, etc.)•Will discuss when to use operator overloading 2003 Prentice Hall, Inc. All rights reserved.38.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–Create a function for the class–Name function operator followed by symbol•Operator+ for the addition operator + 2003 Prentice Hall, Inc. All rights reserved.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, &–Returns address of object•Both can be overloaded•Overloading provides concise notation–object2 = object1.add(object2);–object2 = object2 + object1; 2003 Prentice Hall, Inc. All rights reserved.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-operations–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 += 2003 Prentice Hall, Inc. All rights reserved.68.3 Restrictions on Operator OverloadingOperators that cannot be overloaded . .* :: ?: sizeof Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] 2003 Prentice Hall, Inc. All rights reserved.78.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 operator–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 class 2003 Prentice Hall, Inc. All rights reserved.88.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•HugeIntClass + Long int•Can be member function–When other way, need a non-member overload function•Long int + HugeIntClass 2003 Prentice Hall, Inc. All rights reserved.108.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•Holds a telephone number–Print out formatted number automatically•(123) 456-7890 2003 Prentice Hall, Inc.All rights reserved.Outline11fig08_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 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 PhoneNumberNotice function prototypes for overloaded operators >> and <<They must be non-member friend functions, since the object of class Phonenumber appears on the right of the operator.cin << objectcout >> object 2003 Prentice Hall, Inc.All rights reserved.Outline12fig08_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 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


View Full Document
Download Introduction to Computer Science
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 Introduction to Computer Science 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 Introduction to Computer Science 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?