Ask Question
31 August, 11:48

In a class, why do you include the function that overloads the stream insertion operator, <<, as a friend function? (5, 6)

+1
Answers (1)
  1. 31 August, 13:21
    0
    To provide << operator access to non-public members of the class.

    Explanation:

    Whenever we overload << operator, an object to ostream class has to be passed as this is the class which has properties which allows printing of output to console. Moreover, this operator is used outside class all the time so if not made a global member it will produce conflicts so thats why it has to be made a friend function to allow efficient access to private members of the class. Its declaration inside class is as follows:

    friend ostream& operator<< (std::ostream& os, const T& obj);
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In a class, why do you include the function that overloads the stream insertion operator, ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers