140 likes | 273 Views
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.
E N D
Chapter 11-2Operator Overloading C++, How to Program Deitel & Deitel CS2000 Yanjun Li
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
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
<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
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
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
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
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
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
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
Fig. 11.1| Operators that can be overloaded. CS2000 Yanjun Li
Fig. 11.2| Operators that cannot be overloaded. CS2000 Yanjun Li
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
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