1 / 14

Chapter 11-2 Operator Overloading

Chapter 11-2 Operator Overloading. C++, How to Program Deitel & Deitel. Streams. C++ I/O occurs in streams – sequences of bytes Input Bytes flow from a device to main memory Output Bytes flow from main memory to a device I/O transfers typically take longer than processing the data.

adonica
Download Presentation

Chapter 11-2 Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11-2Operator Overloading C++, How to Program Deitel & Deitel CS2000 Yanjun Li

  2. Streams • C++ I/O occurs in streams – sequences of bytes • Input • Bytes flow from a device to main memory • Output • Bytes flow from main memory to a device • I/O transfers typically take longer than processing the data CS2000 Yanjun Li

  3. Standard Streams • C++ standard stream libraries • Enables I/O operations with Unicode characters • Unicode character set • Represents most of the world’s commercially viable languages, mathematical symbols and more • www.unicode.org CS2000 Yanjun Li

  4. <iostream> header file • Declares basic services required for all stream-I/O operations • Defines input/output classes • istream • ostream • Defines standard stream objects • cin : the standard input stream object • cout: the standard output stream object CS2000 Yanjun Li

  5. Stream operators overloading • Stream insertion operator • Left-shift operator (<<) is overloaded for stream output • Left operand of type ostream& • Such as cout object in cout << classObject • Stream extraction operator • Right-shift operator(>>) is overloaded for stream input • Left operand of istream & • Such ascinobject incin >> classObject • Already overloaded to process each built-in type • Thus, both must be global functions CS2000 Yanjun Li

  6. friend function • Use global function with two arguments • One argument must be class object or reference • Often use friend function for performance reason. • friend function of a class is defined outside that class’s scope (external function), yet has the right to access the non-public and public members of the class. • We declare a prototype of this external function within the class with keyword friend. CS2000 Yanjun Li

  7. operator>> • friend function (global function) //Complex.h friend istream & operator>>( istream &, Complex & ); //(real,img) //Complex.cpp istream & operator>>(istream & input, Complex & cNumber) { double real, imaginary; input.ignore(256, '('); //ignor ( input >> real; //read real part input.ignore(256,','); //white spaces up to next "," are ignored. input >> imaginary; //read imaginary part input.ignore(256,')'); //ignore white spaces after it. if (input.good()) { cNumber.real = real; cNumber.imaginary = imaginary; } return input; // enable cin >> c1 >> c2 >> c3 } CS2000 Yanjun Li

  8. Operator<< • friend function (global function) //Complex.h friend ostream &operator<<(ostream &, const Complex &); //(real,imaginary) //Complex.cpp ostream& operator<<(ostream & output, const Complex & cNumber) { output << "(" << cNumber.real <<", “ << cNumber.imaginary << ")"; return output; //enable cout << c1 << c2 << c3 } CS2000 Yanjun Li

  9. Restrictions on Operator Overloading • Cannot change • Precedence of operator (order of evaluation) • Use parentheses to force order of operators • Associativity (left-to-right or right-to-left) • Number of operands • e.g., & is unary, can only act on one operand • How operators act on built-in data types (i.e., cannot change integer addition) CS2000 Yanjun Li

  10. Restrictions on Operator Overloading • Cannot create new operators • Operators must be overloaded explicitly • Overloading + and = does not overload += • Operator ?: cannot be overloaded CS2000 Yanjun Li

  11. Fig. 11.1| Operators that can be overloaded. CS2000 Yanjun Li

  12. Fig. 11.2| Operators that cannot be overloaded. CS2000 Yanjun Li

  13. Restrictions on Operator Overloading • At least one argument of an operator function must be an object or reference of a user-defined type. This prevents programmers from changing how operators work on fundamental types. CS2000 Yanjun Li

  14. Reference • Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. • Reproduced by permission of Pearson Education, Inc. CS2000 Yanjun Li

More Related