Ask Question
7 August, 08:33

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

+2
Answers (1)
  1. 7 August, 12:03
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers