in the Association lesson for Learn OOP in C++ the code was a bit hard to understand. i couldn’t understand how we printed the output and why did we use “operator” which i don’t know what it does and finally why is it called ostream& ? does the & affect the functionionality of the function?
In C++, we can redefine and change the way operators work using a process called operator overloading. This can be done by declaring an operator function with the keyword
operator preceding the operator.
This is precisely why we use the keyword operator over here before
<< since we want to overload the
& symbol in c++ means that the function will return by reference. By reference, I mean that rather than the value, this would be referring to a pointer/address of the function.
Therefore, we are calling
ostream& over here, which is calling the operator
<< function by its reference.
Keeping both of the things in mind, we can see that by using the operator keyword before
<<, we redefined the operator
<< to return the students who are being taught by a specific teacher and vice versa. In addition, since we are calling it by reference, we have to use
*t1 to print the ostream.
I hope it helps!