What is a stream?
The I/O system in C++ is designed to work with a wide variety of devices including terminals, disks and tape drives. Although each device is very different, the I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as stream.
Describe briefly the features of I/O system supported by C++.
See table 10.1, page 293 of Balagurusamy.
How do the I/O facilities in C++ differ from that in C?
We can design our own manipulators for certain special purposes in C++ but not in C.
Example :
Ostream & unit (ostream & output) { output << “TK”; return output; } The statement cout << 100 << unit; will print 100 Tk.
Why are the words such as cin and cout not considered as keywords?
cin and cout are related to iostream object, that’s why we must include iostream header file in our program to use cin and cout.
Hence cin and cout are considered as iostream objects, not keywords.
How is cout able to display various types of data without any special instructions?
The biggest single advantage to the stream methods is: they are type-safe. If you change a variable’s type, the sub sequent stream operations using that variable will either automatically accommodate the change, or will indicate an incompatibility at compile time. Thus cout is able to display various types of data without any special instructions.
Why is it necessary to include the file iostream in all our programs?
Because any program has input or output statements and without iostream header file we can not use input or output statements, that’s why it is necessary to include the file iostream in all our programs.
Discuss the various forms of get() function supported by the input stream. How are they used?
There are two types of get ( ) functions. We can use both get(char*) and get(void) prototype to fetch a character including the blank space.
How do the following two statements differ in operation?
cin >> c;
cin.get(c);
cin>>c will read a character but it will skip the white space and new line character.
cin.get (c); will read a character including white space and newline character.
Both cin and getline() function can be used for reading a string. Comment.
cin reads a string and stops when a space or newline is encountered.
getline ( ) function reads a whole line of text that ends with a newline character.
Discuss the implications of size parameter in the following statement:
cout.write(line, size);
cout.write (line, size)
The first argument line represents the name of the string to be displayed and the secon d argument size indicates the number of characters to display.
What does the following statement do?
cout.write(s1,m).write(s2,n);
The statement
cout.write(s1,m).write (s2,n);
is equivalent to the following two statements;
cout.write(s1,m);
cout.write(s2,n);
So, cout.write (s1,m).write (s2,n); will print two string s1 and s2
What role does the iomanip file play?
The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats.
What is the role of file() function? When do we use this function?
fill ( ) is known as filling and padding function. The unused positions of the field are filled with white spaces, by default. We can use the fill ( ) function to fill the unused positions by any desired character.
General form :
cout.fill (ch);
Example :
cout.fill (‘*’);
Discuss the syntax of set() function.
syntax of set function:
cout.setf (arg1, arg2);
here arg1 is one of the formatting flags
and arg2 is bit field.
Example:
cout.setf (ios::left, ios :: adjust field);
What is the basic difference between manipulators and ioa member functions in implementation? Give examples.
We can design our manipulator using manipulator function, which can not be done by ios member functions
Example :
ostream & show (ostream & output) { output.setf (ios :: showpoint); output.setf(ios :: showpos); output << setw(10); return output; }
This function defines a manipulator called show that turns on the flags showpoint and showpos declared in the class ios and sets the field width to 10.
State whether the following statements are TRUE or FALSE.
- (a) A C++ stream is a file.
- (b) C++ never truncates data.
- (c) The main advantage of width() junction is that we can use one width specification for more than one items.
- (d) The get(void) function provides a single-character input that does not skip over the white spaces.
- (e) The header file iomanip can be used in place of iostream.
- (f) We cannot use both the C 110 functions and C++ 110 functions in the same program.
- (g) A programmer can define a manipulator that could represent a set of format functions.
- (a) TRUE
- (b) FALSE
- (c) FALSE
- (d) TRUE
- (e) TRUE
- (f) FALSE
- (g) TRUE
Next Previous