For the standard output stream object cout (std::cout), which operator is overloaded to support formatted output? Select the operator commonly chained in printing expressions.

Difficulty: Easy

Correct Answer: <<

Explanation:


Introduction:
C++ iostreams rely on operator overloading to present a clean, chainable syntax for input and output. This question asks which operator is overloaded for the output stream std::cout to send data to the console.


Given Data / Assumptions:

  • We are using iostreams (std::cout).
  • Formatted output is achieved via operator chaining.
  • Insertion/extraction operators are part of the iostream design.


Concept / Approach:
The insertion operator << is overloaded for std::ostream (base of std::cout). Overloads exist for built-in types and can be provided for user-defined types by writing free functions like std::ostream& operator<<(std::ostream&, const T&). Input streams use the extraction operator >>.


Step-by-Step Solution:
1) Identify output direction: “insert into stream” uses <<.2) Note that std::cout is a std::ostream object.3) The expression std::cout << "Hello" << 42; chains multiple insertions.4) The overloaded functions return std::ostream& to enable chaining.


Verification / Alternative check:
Header defines the relevant operator<< overloads; reading the signature shows the return type is std::ostream& for chaining.


Why Other Options Are Wrong:
>>: extraction for input streams (std::cin).+: unrelated to stream output semantics.=: assignment operator; not used for printing.


Common Pitfalls:
Forgetting to make custom operator<< overloads non-members with access to internals via friends if needed.


Final Answer:
<<

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion