Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Which is the object of the overloading operator cout?


Asked by Bo Buckley on Dec 09, 2021 FAQ



The ‘cout’ is actually an object of type ostream. Similarly, in overloading operator >>, the left operand is the cin object and the right operand is the class object cin which is an object of type istream. The general syntax for overloading I/O operator is as follows: It indicates the return type of overloaded I/O operator.
Just so,
But in overloading operator <<, the left operand is the ‘cout’ object and the right operand is the class object. The ‘cout’ is actually an object of type ostream. Similarly, in overloading operator >>, the left operand is the cin object and the right operand is the class object cin which is an object of type istream.
Next, For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc.
Indeed,
Why these operators must be overloaded as global? In operator overloading, if an operator is overloaded as a member, then it must be a member of the object on the left side of the operator. For example, consider the statement “ob1 + ob2” (let ob1 and ob2 be objects of two different classes).
Consequently,
When we overload the binary operator for user-defined types by using the code: The operator function is called using the obj1 object and obj2 is passed as an argument to the function. using & makes our code efficient by referencing the complex2 object instead of making a duplicate object inside the operator function.