Operator Overloading: Overloading Extraction Operator
What is it?
C++ Overloading Extraction Operator >>
is a feature of C++ that allows you to redefine the functionality of the extraction operator when used with user-defined types. The extraction operator is typically used for built-in types to extract values from input streams (like cin
) and assign them to variables. When overloading this operator for a custom class, it allows you to control the behavior of how the class instances are read from the input stream.
Syntax
The syntax of overloading the extraction operator is:
istream& operator>> (istream& in, ClassName& object);
Where,
istream& in
: Reference to the input stream.ClassName& object
: Reference to the object of the user-defined class.
How to use it
To overload the extraction operator, follow the below steps:
- Define a custom class.
- Within that class, or as a friend function if necessary, overload the
>>
operator. - When overloading, ensure that you receive the
istream
reference as the first argument and your class type as the second argument. - Within the operator function, manually extract the values from the input stream into the class members.
Program code snippet example
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
// Overloading the extraction operator
friend istream& operator>> (istream& in, Student& student);
};
istream& operator>> (istream& in, Student& student) {
cout << "Enter student's name: ";
in >> student.name;
cout << "Enter student's age: ";
in >> student.age;
return in;
}
int main() {
Student s;
// Using the overloaded extraction operator
cin >> s;
cout << "Student's name: " << s.name << endl;
cout << "Student's age: " << s.age << endl;
return 0;
}
Output:
Enter student's name: John
Enter student's age: 21
Student's name: John
Student's age: 21
Important to know
- Overloading the extraction operator allows us to define custom behaviors for input stream operations for our user-defined types.
- Make sure to return a reference to the input stream from your overloaded extraction operator function. This allows chaining of input operations.
- If you are defining the overloaded operator function outside the class, it must be declared as a friend function inside the class if it needs to access private or protected members of the class.
Best practices
When overloading the extraction operator, it is important to keep the following best practices in mind:
Always return a reference to the input stream: This allows chaining of input operations, which is a common practice in C++.
Use a const reference for the second parameter: This avoids unnecessary copying of objects.
Handle possible failures: Reading from input streams can fail (for example, when the user enters a string when an integer was expected). Make sure to handle such cases in your code to prevent issues.
Keep it simple: The overloaded function should ideally only extract the necessary values and assign them to class members. Any complex logic should be placed elsewhere in the code.
Overload the extraction operator for the Integer
class.