10.1

What is a stream?

Answer

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.

10.2

Describe briefly the features of I/O system supported by C++.

Answer

See table 10.1, page 293 of Balagurusamy.

10.3

How do the I/O facilities in C++ differ from that in C?

Answer

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.
10.4

Why are the words such as cin and cout not considered as keywords?

Answer

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.

10.5

How is cout able to display various types of data without any special instructions?

Answer

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.

10.6

Why is it necessary to include the file iostream in all our programs?

Answer

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.

10.7

Discuss the various forms of get() function supported by the input stream. How are they used?

Answer

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.

10.8

How do the following two statements differ in operation?
cin >> c;
cin.get(c);

Answer

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.

10.9

Both cin and getline() function can be used for reading a string. Comment.

Answer

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.

10.10

Discuss the implications of size parameter in the following statement:
cout.write(line, size);

Answer

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.

10.11

What does the following statement do?
cout.write(s1,m).write(s2,n);

Answer

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

10.12

What role does the iomanip file play?

Answer

The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats.

10.13

What is the role of file() function? When do we use this function?

Answer

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 (‘*’);

10.14

Discuss the syntax of set() function.

Answer

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);

10.5

What is the basic difference between manipulators and ioa member functions in implementation? Give examples.

Answer

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.

10.6

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.
Answer
  • (a) TRUE
  • (b) FALSE
  • (c) FALSE
  • (d) TRUE
  • (e) TRUE
  • (f) FALSE
  • (g) TRUE

 


Next Previous