CSI 201 - Introduction to Computer Science
Chapter 5 I/O Streams as an Introduction to Objects and Classes Brian R. King Instructor
2/26/2006 CSI 201 -- Chapter 05 2
Introduction
- Up to now, we've known a variable to be a named reference to a
place in memory that stores one type of data value.
- double interest_rate = .059;
- Types such as double, char, bool, int, etc. are simple data types.
They are types built into the C++ language.
- An object is a special kind of variable.
- An object has:
- data (sometimes referred to attributes, members, or member
fields, or fields)
- member functions – special functions that are designed to work with
the data contained in the object.
- Types for objects are complex data types, called classes. They are
made available through include directives and other means we'll discover in Chapter 6.
- Chapter 6 will go into objects and classes in much more detail.
- The goal of this chapter is to simply introduce you to the
concepts of objects and classes using the I/O Stream library.
2/26/2006 CSI 201 -- Chapter 05 3
Streams - Review
A stream is a flow of data to and from your program. C++ represents streams in your program as objects. Input is delivered to your program via an input stream object
The object is connected to the resource you want to read from.
- cin is an input stream object connected to the keyboard.
Type (or class) – istream We say that cin is an instance of the class istream.
Output is sent to an output device via an output stream object
The object is connected to the resource you want to send data to.
- cout is an output stream object connected to the screen.
Type (or class) – ostream We say that cout is an instance of the class ostream. 2/26/2006 CSI 201 -- Chapter 05 4
File I/O
Why use files?
Store data permanently Repeated input data
- No need to repeatedly type your data in
Allows for large data set processing
How do we use files in our C++ programs?
C++ programs read and write data residing in files through file
stream objects.
File stream objects behave much like cin and cout. It will be our job to connect the file stream object to an actual file
managed by the OS's file system.
Terminology:
When a program takes data from a file, we say the program is
reading from the file.
When a program sends data to a file, we say the program is
writing to the file.