Download presentation
Presentation is loading. Please wait.
Published byWilfred Lyons Modified over 9 years ago
1
CMSC 2021 Stream I/O Operators and Friend Functions
2
CMSC 2022 Stream I/O Background contains declarations for classes –istream –ostream cin is an istream object cout is an ostream object
3
CMSC 2023 Stream I/O Background (continued) C++ implements stream I/O by overloading the >> and > and << are really bit shift operators.) Extraction operator (>>) use: istream_object >> object 1 >> object 2 >> … object n ; cin >> x >> y; Insertion operator (<<) use: ostream_object << object 1 << object 2 << … object n ; cout << “Number = “ << num << endl; >> and << return objects of type istream_object and ostream_object, respectively.
4
CMSC 2024 Overloading Stream I/O Operators How can we read data into or write out the values of data types other than integer, floating point, or character? –Fraction –Vector –Set Options: –Class methods, such as Fraction Fraction::readFraction(); Fraction Fraction::displayFraction(); –Write functions to further overload >> and <<
5
CMSC 2025 Overloading Stream I/O Operators (continued) Syntax for overloading >> istream& operator >> (istream& is_name, class_name& object_name); –is_name is passed by reference because its state will be changed –object_name is passed by reference because data will be read into it Syntax for overloading << ostream& operator <<(ostream& os_name, const class_name& object_name); –os_name is passed by reference because its state will be changed –object_name is a const because it will not be changed
6
CMSC 2026 Overloading Stream I/O Operators (continued) Fraction class example class Fraction { public: // constructors and other methods void setNumerator(int n); void setDenominator(int d); int getNumerator(); int getDenominator(); private: int numerator, denominator; }; istream& operator >>(istream& input, Fraction& f); ostream& operator <<(ostream& output, const Fraction& f);
7
CMSC 2027 Overloading Stream I/O Operators (continued) istream& operator >>(istream& input, Fraction& f) { char c; // to hold the ‘/’ separator int n, d; input >> n >> c >> d; if (d == 0) { cerr << “Zero denominator invalid” << endl; exit(1); } f.setNumerator(n); f.setDenominator(d); return input; }
8
CMSC 2028 Overloading Stream I/O Operators (continued) ostream& operator <<(ostream& output, const Fraction& f) { output << f.getNumerator() << ‘/’ << f.getDenominator(); return output; }
9
CMSC 2029 Friend Functions A function can be defined to be a “friend” of a class: friend function-name(arguments); // inside of class definition A friend function has direct access to the class’ private members, both data and methods Making a function a friend can be convenient -- the class does not need “get” methods for the function to access its data Using a friend function violates the rules of encapsulation. Use friends only when absolutely necessary!
10
CMSC 20210 Friend Functions (continued) Fraction class using friends class Fraction { public: // constructors and other methods friend istream& operator >>(istream& input, Fraction& f); friend ostream& opeator <<(ostream& input, const Fraction& f); // set and get methods private: int numerator, denominator; }; istream& operator >>(istream& input, Fraction& f); ostream& operator <<(ostream& output, const Fraction& f);
11
CMSC 20211 Friend Functions (continued) istream& operator >>(istream& input, Fraction& f) { char c; // to hold the ‘/’ separator input >> f.numerator >> c >> f.denominator; // direct access if (f.denominator == 0) { cerr << “Zero denominator invalid” << endl; exit(1); } return input; }
12
CMSC 20212 Friend Functions (continued) ostream& operator <<(ostream& output, const Fraction& f) { output << f.numerator << ‘/’ << f.denominator; // direct access return output; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.