Ask Question

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

+3
Answers (1)
  1. 29 February, 03:31
    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?
Find an answer to your question 👍 “In a class, why do you include the function that overloads the stream insertion operator, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers