Stream I - Stream I/O INTRODUCTION TO STREAM I/O PART 1...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Stream I/O INTRODUCTION TO STREAM I/O PART 1 - OVERLOADING << In this issue we will begin discussing the stream I/O package that comes with C++. The first four sections of this issue are related and present several aspects of stream I/O along with some related topics. If you've used C++ at all, you've probably seen a simple example of how to do output: cout << "Hello, world" << "\n"; instead of: printf("Hello, world\n"); cout is an output stream, kind of like stdout in C. The C example could be written as: fprintf(stdout, "Hello, world\n"); which makes this correspondence a bit clearer. Once you get beyond simple input/output usage, what is the stream I/O package good for? One quite useful thing it can do is to allow the programmer to take control of I/O for particular C++ types such as classes. This end is achieved by the use of operator overloading. Suppose that we have a Date class: class Date { int month; int day; int year; public: Date(char*); Date(int, int, int); }; with an internal representation of a Date using three integers for month, day, and year, and a couple of constructors to create a Date object. How would we output the value of a Date object? One way would be to devise a member function: void out(); implemented as: void Date::out() { printf("%d/%d/%d", month, day, year); } This function would operate on an object instance of a Date and would access the month/day/year members and display them. This approach will certainly work and may be suitable in some kinds of applications. But this scheme doesn't integrate very well with stream I/O. For example, I cannot say: Date d(9, 25, 1956); cout << "Today's date is " << d;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
but must say: printf("Today's date is "); d.out(); For this purpose it is necessary to overload the << operator. We can add a friend function to Date: friend ostream& operator<<(ostream& os, const Date& d); with definition: ostream& operator<<(ostream& os, const Date& d) { return os << d.month << "/" << d.day << "/" << d.year; } With this definition, it is possible to say: Date d(9, 25, 1956); cout << "Today's date is " << d << "\n"; Several aspects of this example need explanation. An overloaded operator in C++ is an operator like "+" or "<<" that is given a special meaning for certain kinds of arguments, and turns into a function. Wherever the operator is used with these arguments, a function is called. So, for example, an output statement: cout << "xxx"; is actually: cout.operator<<("xxx"); which is a valid function call in C++ if you're using stream I/O. cout is an instance of class ostream (at least conceptually; the actual hierarchy is a bit complicated). When we wrote the actual statement to output a formatted Date: return os << d.month << "/" << d.day << "/" << d.year; we returned the ostream reference so as to allow << usage to be chained. Because << operators group left to right, a sequence like: cout << "x" << "y"; actually means: cout.operator<<("x").operator<<("y"); Finally, the reason that we declared operator<<(ostream&, const Date&) as a friend and not a member is that a member function that is a binary operator has an implicit convention on argument usage, namely, that for some operator @: x @ y means: [email protected](y); that is, the left operand of the operator must be an instance of the class of which the overloaded operator is a member.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern