answersLogoWhite

0


Best Answer

Consider the following line:

cout<<obj;

where obj is the object of Demo class.

In this case we are overloading "<<" operator. But overloading the binary

operator using member function, the left hand operand should be the object of relevant class.

Here in this case left hand side operand is not the object of Demo class. It is object of ostream class.

Hence we cant overload ostream operators using member function. But we can overload these type of operators using friend functions.

Thanks,

Prof. D. H. Ingole

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why ostream operators not overloaded using member functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are special operators in c plus plus?

The only "special" operators in C++ are those that cannot be overloaded. That is; the dot member operator (.), pointer to member operator (.*), ternary conditional operator (:?), scope resolution operator (::), sizeof() and typeof().


What is the operator that cannot be overloaded?

There are 5 operators which cannot be overloaded. They are: * .* - class member access operator * :: - scope resolution operator * . - dot operator * ?:: - conditional operator * Sizeof() - operator Note:- This is possible only in C++.


Which c plus plus operators cannot be overloaded?

1. Member-of operator (.) 2. Pointer-to-member-of operator (.*) 3. Ternary condition operator (?:) 4. Scope resolution operator (::) 5. sizeof operator 6. typeid operator


Can static member function be overloaded?

Yes. Any function can be overloaded. However you cannot override a static member function. Only instance members can be overridden.


Which operator not overloaded in c plus plus?

The if statementex.if (index < 5)printf("Index is less than 5\n");elseprintf("index is greater or equal to 5\n");(You can also replace the "if" with a "?" and the "else" with a "?" -- no, that would be syntax error)


How is working of a member function different from a friend function and a non-member function?

With respect to a given class, all functions can be split into four categories: 1. Member functions. 2. Static member functions. 3. Friend functions. 4. Non-member functions. All class member functions have the following three properties with respect to the class in which they are declared a member: 1. Private access to the class representation. 2. Scoped to the class. 3. Invoked through an instance of the class (has a 'this' pointer). Static member functions have the first two properties only. Friend functions have the first property only. Non-member functions have none of these properties.


Differentiate between overloading unary operator and overloading binary operator?

Unary operators declared as member function take NO arguments; if declared as global function, then take only one argument.Binary operators declared as member functions take one argument; if declared as global function, then would take two arguments.Note: The first argument for member function overloaded is always the class type; the class in which the operator is declared.


What are the pointer to member operators?

It is the right arrow: p->fld means (*p).fld


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


Can you use friend functions to overload operators?

Yes you can. Although you will often hear it said that friend functions undermine encapsulation, this is errant nonsense. Here's a typical example: #include &lt;iostream&gt; using namespace std; class Date { private: int mo, da, yr; public: Date( int m, int d, int y ): mo(m), da(d), yr(y) {} friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Date&amp; dt); }; ostream&amp; operator&lt;&lt;(ostream&amp; os, const Date&amp; dt) { os &lt;&lt; dt.mo &lt;&lt; '/' &lt;&lt; dt.da &lt;&lt; '/' &lt;&lt; dt.yr; return os; } int main() { Date dt( 5, 6, 92 ); cout &lt;&lt; dt; return( 0 ); } While it is possible to eliminate the friend function declaration simply by providing public accessors to Date::mo, Date::da and Date::yr, there is no advantage in doing so. You can still provide those accessors, of course, but since you'd need to return those members by value in order to maintain encapsulation, the operator overload would be less efficient as a result. In this case the difference is minimal but, with more complex members, the inefficiency can quickly add up. Remember that friend functions merely extend the class interface. While it is true that you should not grant friend access to code that you have no control over (and thus undermine any concept of encapsulation), operator overloads such as the insertion operator overload shown above is under your complete control. Ultimately, if it were possible to declare the function as an actual member of the class then you would rightly do so. But that's precisely what you do when you declare friendship. And in this case, that friendship is perfectly legitimate because the alternative, non-friend function, would be less efficient.


What are functions of a community?

to protect member of the community


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.