I'm working on a homework project for a virtual rolodex that has called for a main class, a rolodex class, and a card class. To output the contents of all of the "cards" to the console, the assignment says that main() should call a show(...) function in the rolodex class, passing it an ostream and show(...) then iterates over the cards, calling each of their showCard() functions. The actual showing is done by the card objects' showCard() function, showing on the provided ostream.
What I don't understand is why an ostream would/should be passed anywhere. Seems like the assignment is calling for something like this:
main() {
Rolodex myRolodex;
ostream myStream;
myRolodex.show(myStream);
}
void Rolodex::show(ostream& theStream) {
//for each card 'i' in the Rolodex...
myCard[i].show(theStream);
}
void Card::show(ostream& theStream) {
theStream << "output some stuff" << endl;
}
instead of something like this:
main() {
Rolodex myRolodex;
myRolodex.show(); //no ostream passed
}
void Rolodex::show() {
//for each card 'i' in the Rolodex...
myCard[i].show();//no ostream passed
}
void Card::show() {
cout << "output some stuff" << endl;
}
Am I either misunderstanding the use of ostream as a parameter or missing some other obvious reason to pass an ostream down the stream like that?
main
needs to go away, and the third line needs to bemyRolodex.show(std::cout);
. – Rachealrachelstd::cout
is anostream
object. The idea of passing astd::ostream
is to make it so that the function doesn't care where it's sending the output.std::cout
is just a special instance of astd::ostream
. If you make the function itself use that one instance ofstd::ostream
you've defeated the point of the parameter. – Rachealrachel