135k views
4 votes
You want to be able to display an instance of class Foo, like this:

Foo f;
cout << f;
What would be the correct declaration for a function to do that?

a) ostream& operator<<(ostream& os, const Foo& f)
b) void operator<<(ostream& os, const Foo& f)
c) istream& operator>>(istream& is, const Foo& f)
d) int operator<<(int a, const Foo& f)

User Halilcakar
by
8.5k points

1 Answer

2 votes

Final answer:

The correct declaration for a function to display an instance of class Foo is a) ostream& operator<<(ostream& os, const Foo& f).

Step-by-step explanation:

The correct declaration for a function to display an instance of class Foo using the << operator is a) ostream& operator<<(ostream& os, const Foo& f). The << operator is used for output stream and it should return a reference to the output stream object to enable chaining of << operators. The const reference to the Foo object allows for accessing the data members of the object without making modifications.

User Rohan Taneja
by
8.0k points