19 Lecture

CS304

Midterm & Final Term Short Notes

STREAM INSERTION OPERATOR

The stream insertion operator (<<) is used in C++ to insert data into a stream, typically for output to the console or a file. It is often overloaded to work with user-defined classes, allowing them to be printed using standard syntax. The opera


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the return type of the stream insertion operator (<<)? a) void b) istream& c) ostream& d) string Answer: c) ostream& Which of the following is an example of overloading the stream insertion operator for a custom class? a) int x; cin >> x; b) cout << "Hello, World!" << endl; c) cout << obj; d) cin << obj; Answer: c) cout << obj; Which of the following is an example of using the stream insertion operator to output multiple values? a) cout << "The value of x is " << x; b) cout << "The sum of " << x << " and " << y << " is " << x + y; c) cout << "Enter a value: "; d) cout << "The result is " << result << endl; Answer: b) cout << "The sum of " << x << " and " << y << " is " << x + y; Which of the following is a possible implementation of the stream insertion operator for a custom class? a) ostream& operator<<(ostream& out, MyClass obj) { /* implementation / } b) istream& operator<<(istream& in, MyClass obj) { / implementation / } c) MyClass& operator<<(MyClass obj) { / implementation / } d) void operator<<(MyClass obj) { / implementation / } Answer: a) ostream& operator<<(ostream& out, MyClass obj) { / implementation */ } Which of the following is a correct syntax for using the stream insertion operator to output an object? a) cout << MyClass; b) cout << object.MyClass; c) object << cout; d) cout << object; Answer: d) cout << object; Which of the following is a correct syntax for overloading the stream insertion operator for a custom class? a) void operator<<(); b) void operator<<(ostream& out); c) void operator<<(ostream& out, MyClass obj); d) void operator<<(MyClass obj); Answer: c) void operator<<(ostream& out, MyClass obj); Which of the following is a correct implementation of the stream insertion operator for a custom class that has private data members? a) ostream& operator<<(ostream& out, MyClass obj) { out << obj.privateMember; } b) ostream& operator<<(ostream& out, MyClass obj) { obj.privateMember << out; } c) ostream& operator<<(ostream& out, MyClass obj) { obj.getPrivateMember() << out; } d) None of the above. Answer: c) ostream& operator<<(ostream& out, MyClass obj) { obj.getPrivateMember() << out; } Which of the following is an example of using the stream insertion operator to output a literal value? a) cout << "Hello, World!"; b) cout << x; c) cin >> x; d) cout << "Enter a value: "; Answer: a) cout << "Hello, World!"; Which of the following is a correct implementation of the stream insertion operator for a custom class that has a non-static data member? a) ostream& operator<<(ostream& out, MyClass obj) { obj.dataMember << out; } b) ostream& operator<<(ostream& out, MyClass obj) { obj.dataMember >> out; } c) ostream& operator<<(ostream& out, MyClass obj) { out << obj.dataMember; } d) None of the above. Answer: c) ostream& operator<<(ostream& out, MyClass obj) { out << obj.dataMember; } Which of the following is an example of using the stream insertion operator



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the purpose of the stream insertion operator in C++? The stream insertion operator << is used to output data to a stream, such as cout. How do you overload the stream insertion operator for a user-defined class? To overload the stream insertion operator << for a user-defined class, you need to define a non-member function that takes an output stream std::ostream& and the class object as arguments. Can the stream insertion operator be used to output multiple variables in a single statement? Yes, the stream insertion operator can be chained to output multiple variables in a single statement, for example std::cout << a << " " << b << std::endl; How can you control the formatting of output using the stream insertion operator? You can use various manipulators such as std::setw and std::setprecision to control the formatting of output using the stream insertion operator. What is the difference between the << operator and the put method of the output stream class? The << operator is used to output data to a stream in a formatted way, whereas the put method is used to output individual characters to a stream. How can you overload the stream insertion operator for a class that has private member variables? You can make the stream insertion operator a friend function of the class, which allows it to access private member variables. Can you use the stream insertion operator to output objects of built-in types like int or double? Yes, the stream insertion operator can be used to output objects of built-in types like int or double. What is the return type of the stream insertion operator? The return type of the stream insertion operator is a reference to the output stream. Can the stream insertion operator be used with input streams? No, the stream insertion operator << is used only for output streams. The stream extraction operator >> is used for input streams. How do you handle errors that occur while using the stream insertion operator? You can use the std::ios::failbit flag to check for errors that occur while using the stream insertion operator, and handle the errors appropriately.
The stream insertion operator << is used to output data to the standard output device. It is overloaded for various data types to allow them to be easily printed to the console or written to a file. When the stream insertion operator is used with a user-defined object, it must be overloaded to provide the appropriate output behavior. The overloaded operator function must take two parameters: the first parameter is a reference to the output stream object, and the second parameter is a reference to the object being printed. For example, consider a Person class with member variables name and age. To print a Person object to the console, the << operator can be overloaded as follows:
c
class Person { std::string name; int age;public: Person(std::string n, int a) : name(n), age(a) {} friend std::ostream& operator<<(std::ostream& os, const Person& p) { os << p.name << " (" << p.age << " years old)"; return os; } };
This overloaded operator function can be called as follows:
c
Person john("John Smith", 30); std::cout << john << std::endl;
This will output John Smith (30 years old) to the console. The << operator can also be overloaded for other purposes, such as writing data to a file or a network socket. This can be done by creating a custom output stream class that implements the << operator in the desired way.