Important Introduction to Programming Short Questions
Chapter: 37
Question 1:
What kind of member function cannot be used to overload stream insertion and extraction operators, and why?
Answer:
Member functions cannot be used for overloading these operators because the left operand is a stream object (e.g., cout, cin), which cannot be a member of the class being operated on. Non-member functions, often declared as friends, are used instead.
Chapter: 37
Question 2:
Why is it advantageous to overload operators for classes rather than using methods for input and output?
Answer:
Overloading operators provides a more intuitive and readable syntax for input and output operations, similar to built-in types, and avoids the need for separate method calls, making code cleaner and easier to use.
Chapter: 38
Question 3:
What is a user-defined manipulator in C++?
Answer:
A user-defined manipulator is a function that modifies the output stream, similar to built-in manipulators like endl. It returns a reference to an ostream object.
Chapter: 38
Question 4:
How do you declare a user-defined manipulator?
Answer:
Declare it as ostream& manipulator_name(ostream& os), where os is the stream object passed to it.
Chapter: 38
Question 5:
What does the manipulator endl do?
Answer:
endl inserts a newline character into the output stream and flushes the buffer.
Chapter: 38
Question 6:
How does a user-defined manipulator differ from built-in ones?
Answer:
User-defined manipulators are customized functions that manipulate the output stream, whereas built-in manipulators are predefined and fixed.
Chapter: 38
Question 7:
What is the return type of a user-defined manipulator?
Answer:
It returns a reference to an ostream object.
Chapter: 38
Question 8:
How is a user-defined manipulator used with cout?
Answer:
It is used in a cascading style, e.g., cout << manipulator << data;.
Chapter: 38
Question 9:
What is the purpose of the spaceFirst manipulator in the example?
Answer:
spaceFirst sets the output stream's width to 33 using setw(33) for formatting.
Chapter: 38
Question 10:
What does the tab manipulator do?
Answer:
The tab manipulator inserts a tab character (\t) into the output stream.