Important Introduction to Programming Short Questions
Chapter: 37
Question 1:
Why must the second parameter of the stream extraction (>>) operator function be passed by reference?
Answer:
The second parameter must be passed by reference because the operator function modifies the object, and passing by reference ensures that changes are reflected outside the function.
Chapter: 37
Question 2:
How does the operator overloading for stream insertion (<<) and extraction (>>) support cascading operations?
Answer:
For cascading operations, both << and >> operators return a reference to the stream object (ostream& for << and istream& for >>), allowing multiple operations to be chained together.
Chapter: 37
Question 3:
What is the purpose of declaring a non-member operator function as a friend of a class?
Answer:
Declaring a non-member operator function as a friend allows it to access private and protected members of the class, which is necessary for properly implementing the operator functions for custom classes.
Chapter: 37
Question 4:
How are stream insertion (<<) and extraction (>>) operators defined for primitive data types in C++?
Answer:
For primitive data types, these operators are predefined in the standard library (iostream.h), and they handle input and output operations without needing explicit overloading for each type.
Chapter: 37
Question 5:
What is the typical signature of the stream insertion (<<) operator for a custom class?
Answer:
ostream& operator<<(ostream& output, const CustomClass& obj); where CustomClass is the name of the user-defined class.
Chapter: 37
Question 6:
Why is it important to pass the first parameter of the stream insertion (<<) and extraction (>>) operators by reference?
Answer:
Passing the first parameter by reference is crucial because it allows modifications to the stream object to be reflected outside the function and supports chaining of multiple operations.
Chapter: 37
Question 7:
What would be an example of overloading the extraction (>>) operator for a custom class Date?
Answer:
Example: istream& operator>>(istream& input, Date& d) { input >> d.day >> d.month >> d.year; return input; } This function reads day, month, and year into a Date object from the stream.
Chapter: 37
Question 8:
In what scenarios is it useful to overload the stream insertion (<<) and extraction (>>) operators for a class?
Answer:
Overloading these operators is useful when you want to simplify the input and output operations for objects of a class, making code more readable and maintaining a consistent syntax similar to that used with built-in types.
Chapter: 37
Question 9:
What is the key difference between overloading stream operators for primitive data types and user-defined types?
Answer:
Primitive data types have built-in support for these operators, while user-defined types require custom implementations to define how the operators handle the class-specific data.
Chapter: 37
Question 10:
How can cascading operations be performed with the overloaded stream insertion (<<) operator?
Answer:
Cascading operations are achieved by returning the stream object (e.g., ostream&) from the overloaded operator function, allowing multiple insertions to be chained in a single statement, such as cout << obj1 << obj2;.