Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between fstream, ofstream, ostream, iostream

I'm taking a quiz on an intro level class in C++ and I'm trying to understand a question. After searching the internet and not getting an answer, so here I am.

Which of the following function declarations will accept either cout or a file stream 
object as its argument?

A. void output( fstream &outFile);  
B. void output( ofstream &outFile); 
C. void output( ostream &outFile);  
D. void output( iostream &outFile);

The answer is C.

I know the difference between: fstream, ofstream, ostream, iostream.

What I don't understand is why none of the other options are able to accept the cout or file stream object as an argument.

Is the answer as simple as ostream objects contain data (char,etc) that are able to be passed as arguments?

Any information would be greatly appreciated.

like image 976
Humanid 1652487954543 Avatar asked Oct 11 '17 14:10

Humanid 1652487954543


People also ask

What is the difference between fstream and ofstream?

ofstream is output file stream which allows you to write contents to a file. fstream allows both reading from and writing to files by default.

Is fstream inherited from iostream?

Dealing with files is similar to dealing with standard input and standard output; classes ifstream, ofstream, and fstream are derived from classes istream, ostream, and iostream, respectively.

What does fstream stand for?

The fstream term stands for File Stream. Stream refers to a sequence of characters moving from the disk to the C++ program or from the C+ program to the disk. Moving characters from a file in disk to the program is inputting. Moving characters from the program to a file in the disk is outputting.

Does iostream include istream?

The iostream: This class is responsible for handling both input and output stream as both istream class and ostream class is inherited into it. It provides function of both istream class and ostream class for handling chars, strings and objects such as get, getline, read, ignore, putback, put, write etc..


1 Answers

The answer is C. The question is about the inheritance hierarchy. std::cout is an instance of std::ostream. All other functions accept subclasses of std::ostream and can therefore not handle std::cout. std::fstream could be passed to all of them but the question was about both.

like image 170
MichaelE1000 Avatar answered Oct 03 '22 07:10

MichaelE1000