cout is a predefined object of the ______ class.

2022

cout is a predefined object of the ______ class.

Answer: B. ostreamConcept: In C++ every console read or write travels through a stream class declared by the <iostream> family of headers. The library defines two…

  1. A.

    istream

  2. B.

    ostream

  3. C.

    outstream

  4. D.

    foutstream

Attempted by 574 students.

Show answer & explanation

Correct answer: B

Concept: In C++ every console read or write travels through a stream class declared by the <iostream> family of headers. The library defines two narrow-character stream types with opposite directions: istream (the typedef for basic_istream<char>) models a stream that extracts characters from a source, and ostream (the typedef for basic_ostream<char>) models a stream that inserts characters into a destination. A predefined stream object is an instance of one of these classes, and the class it instantiates fixes both the direction of data flow and the operators the object supports.

Application: Including <iostream> automatically declares the standard stream objects. cout is the object connected to the standard output device (the console by default), so its type must be the class that models an output stream. The header synopsis in the standard declares it literally as extern ostream cout;. That is why cout << x compiles — << is the insertion operator supplied by the output-stream class — while cout >> x does not, because extraction belongs to the input-stream class.

Cross-check: the predefined stream objects and the class each one instantiates.

Object

Class

Connected to

cin

istream

standard input

cout

ostream

standard output

cerr

ostream

standard error, unbuffered

clog

ostream

standard error, buffered

Contrast: outstream and foutstream are not names the C++ standard library declares at all. Disk-file I/O uses ifstream, ofstream and fstream from <fstream>; those classes sit in the same stream hierarchy but bind a stream to a named file rather than to the console.

So the blank is filled by ostream: cout is a predefined object of the ostream class.

Explore the full course: Dsssb Tgt Computer Science Paper 2

Loading lesson…