answersLogoWhite

0


Best Answer

The first argument of the insertion and extraction operators is always a non-const reference to the stream being operated upon. The second argument is an object of the type being inserted or extracted. For insertions, always use a constant reference to the type. For extractions, use a non-const reference. The return value is always a reference to the first argument. For any given type, T, the following functions may be defined:

std::ostream& operator<< (std::ostream& os, const T& data)

{

// ...

return os;

}

std::istream& operator>> (std::istream& is, T& data)

{

// ...

return is;

}

The implementation of the output stream insertion operator must convert type T to a character sequence.

The implementation of the input stream extraction operator must convert a character sequence to a type T.

Both operators must be declared in the global namespace, never as members of any class. However, they may be declared friends of the class declared in the second argument if (and only if) they require private access to the class representation.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the first parameters of stream insertion and stream extraction for operator function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is ellipsis operator in c?

It somehow tells the compiler that the function will accept an unknown number of parameters.


How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (&lt;&lt;) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value. You must overload the insertion operator from outside of the class, like so: ostream&amp; operator&lt;&lt;(ostream&amp; lhs, const MyObject&amp; rhs) { lhs &lt;&lt; rhs.get_data(); return( lhs ); }


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


What is a user defined manipulator in c plus plus?

A user-defined manipulator is a function which can be passed as an argument to the stream insertion or extraction operator overloads. For example, the output stream insertion operator has the following overload: std::ostream&amp; operator&lt;&lt; (std::ostream&amp; st, std::ostream&amp; (*func) (std::ostream&amp;)); The second argument is the function pointer, with the following signature: std::ostream&amp; (*func) (std::ostream&amp;) Any function that matches this signature can be used as a manipulator. For instance, the following user-defined manipulator does exactly the same job as the std::endl manipulator: std::ostream&amp; my_manipulator (std::ostream&amp; os) { return os &lt;&lt; '\n' &lt;&lt; std::flush; } Example usage: std::cout &lt;&lt; "Hello world!" &lt;&lt; my_manipulator; You can, of course, provide your own implementations to perform any type of manipulation. For example, suppose you want a manipulator that inserts an elipses into an output stream: std::ostream&amp; elipses (std::ostream&amp; os) { return os &lt;&lt; "..."; } Example usage: std::cout &lt;&lt; "Hello" &lt;&lt; elipses &lt;&lt; "world!" &lt;&lt; my_manipulator; Output: Hello...world!

Related questions

What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


What is ellipsis operator in c?

It somehow tells the compiler that the function will accept an unknown number of parameters.


What is stream operator in c plus plus?

There are two stream operators: &lt;&lt; (insert or put) and &gt;&gt; (extract or get). Output streams implement the insertion operator, input streams implement the extraction operator and input/output streams implement both operators.


What the operator used when invoking a function?

parenthesis - () are used along with the function name to invoke a function.Eg:Consider a function named trial. Then the invoking statement would be:trial();If the function has arguments, then these arguments are passed inside the parameters.


How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


What is the function of plus sign?

The "plus sign" (+) is an operator that, by default, takes the left and right operands as parameters, and returns the sum of both operands as the return value.


What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (&lt;&lt;) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value. You must overload the insertion operator from outside of the class, like so: ostream&amp; operator&lt;&lt;(ostream&amp; lhs, const MyObject&amp; rhs) { lhs &lt;&lt; rhs.get_data(); return( lhs ); }


Distinguish between operator overloading and function overloading?

in C++ there is no real difference as operators are overloaded by implementing them as functions. However, while we differentiate between function overloads by the function signature (the number and type of parameters), operator overloads are distinguished only by the parameter types. The parameters are interpreted as operands, and the number of operands will depend upon whether the operator is unary, binary or ternary. That is, for any given operator, the number of operands will be the same for each overload you implement. The only exceptions are the unary increment (++) and decrement (--) operators as they each have postfix and prefix variants. In order to differentiate their signatures, an unreferenced or dummy parameter must be passed to the postfix variants.


What is operator function?

An operator function implements a particular operator symbol. The database server provides special SQL-invoked functions, called operator functions, that implement operators. An operator function processes one to three arguments and returns a value. When an SQL statement contains an operator, the database server automatically invokes the associated operator function. The association between an operator and an operator function is called operator binding. You can overload an operator function to provide the operator for a UDT. The SQL user can then use the operator with the UDT as well as with the built-in data types. When an SQL statement contains an operator, the database server automatically invokes the associated operator function.


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


What is THIS operator in c?

The this operator is not a c operator. It is a c++ keyword. It is equivalent to an r-value pointer to the current instance of an object. It is useful when resolving between object members and method parameters.


In SQL what is the function of the union operator?

In SQL, the function of the union operator is to combine the result of two or more select-statements. The union operator is a very useful tool when coding SQL.