The return type of the operator function for << operator is ________.
Aclass for which we overload operator
Breference of ostream class (ostream&)
Creference of istream class (istream&)
Dvoid
operator<< returns ostream& so that insertions can be chained.
2.
★ Final term 2017 (paper 1)
The stream insertion and stream extraction operators are already overloaded for ________.
AUser-defined data types
BBuilt-in data types
CUser-defined and built-in data types
DConstants
The library overloads << and >> for built-in types; user-defined types need their own overloads.
3.
★ Final term 2017 (paper 4)
The stream insertion and extraction operators ____________.
Amust be overloaded as member function
Bmust be overloaded as non-member function
Ccan be overloaded either as member or non-member
Dalways overloaded in class functions
The left operand is a stream object, not the user's class, so << and >> for a user class are overloaded as non-member (usually friend) functions.
4.
★ Final term solved MCQs (Moaaz & Asad)
The first parameter of operator function for << operator,
AMust be passed by value
BMust be passed by reference
CCan be passed by value or reference
DMust be object of class
The ostream argument cannot be copied, so it is taken by reference.
5.
★ Final term solved MCQs (Moaaz & Asad)
The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.
ATrue
BFalse
The stream comes first and the class object second, e.g. ostream& operator<<(ostream&, const T&).
6.
★ Final term solved MCQs (Moaaz & Asad)
The first parameter of overloaded stream insertion operator is _________ where second parameter is _______
Ainput stream, object of class
Bobject of class, output stream
Coutput stream, object of class
Dobject of class, input stream
operator<< takes the ostream first and the class object second.
7.
★ Final term solved MCQs (Moaaz & Asad)
The operator function of << and >> operators are always the member function of a class.
ATrue
BFalse
Stream operators are normally non-member (often friend) functions since the stream is the left operand.
No MCQs match this filter.Is filter mein koi MCQ nahi.
1.
★ Final term 2017 (paper 1)Lecture 372 marks
Write the operator overloading declaration for a stream extraction (>>) operator for an integer.
Show answerJawab dekhein
Generic form for a class (declared as friend inside the class):
friend istream& operator>>(istream& input, ClassName& obj);
For a class holding an integer, e.g. class Number { int n; ... };
friend istream& operator>>(istream& in, Number& num); // body: in >> num.n; return in;
2.
★ Final term 2017 (paper 4)Lecture 372 marks
Write the C++ code for the declaration of overloaded stream extraction(>>) and stream insertion(<<) operator for the object d of type Date.
Show answerJawab dekhein
class Date
{
// data members ...
public:
friend ostream &operator<<(ostream &os, const Date &d);
friend istream &operator>>(istream &is, Date &d);
};
3.
★ Final term 2017 (paper 4)Lecture 373 marks
Can we pass the first parameter of a stream extraction or stream insertion operator by value? If not then why?
Show answerJawab dekhein
No. The first parameter is the stream object (ostream/istream) and it must be passed by reference. Stream objects cannot be copied (their copy constructor is not accessible), the operator must work on and change the state of the actual stream (for example cout or cin), and it returns the same stream by reference so that calls can be chained, e.g. cout << a << b.
1.
★ Final term subjective (Moaaz)Lecture 375 marks
Why the first parameter of operator function for << operator must be passed by reference?
Show answerJawab dekhein
The first parameter of operator<< is the output stream (ostream &os). It must be a reference because: (1) stream objects cannot be copied - the copy constructor of ostream is not accessible, so passing by value does not compile; (2) the function must write to the original stream (e.g. cout or a file stream), not a copy, so that the output really appears there and the stream's state changes are kept; (3) the function returns this same stream by reference (return os;) so that calls can be chained, e.g. cout << obj1 << obj2. Example: friend ostream& operator<<(ostream &os, const Date &d);