91.7k views
4 votes
Why must the >> and << operators be declared as friends?

a) To provide operator overloads for input and output streams.
b) To restrict access to these operators within the class.
c) To make the class more encapsulated.
d) To improve operator performance.

User F Andrei
by
9.0k points

1 Answer

3 votes

Final answer:

The >> and << operators in C++ can be declared as friends to provide operator overloads for input and output streams, while also improving operator performance. However, the main reason for declaring them as friends is to make the class more encapsulated.

Step-by-step explanation:

The >> and << operators in C++ can be declared as friends to provide operator overloads for input and output streams. By declaring the operators as friends, they can directly access private members of a class, allowing them to manipulate input and output streams more efficiently. While this does improve operator performance, the main reason for declaring the operators as friends is to facilitate input and output operations, making the class more encapsulated.

User Tislam
by
8.4k points