C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will learn how to overload the input (>>
) and output (<<
) operators for a custom class in C++. Overloading these operators allows you to provide a more natural way to perform input and output operations with your custom class objects.
Let's demonstrate overloading the input and output operators using a simple Person
class as an example.
Person
class:First, let's create a basic Person
class with two private data members: name
and age
:
#include <iostream> #include <string> class Person { std::string name; int age; public: // Constructor Person() : name(""), age(0) {} // Rest of the implementation goes here };
<<
):To overload the output operator for the Person
class, define a friend function with the following syntax:
friend std::ostream& operator<<(std::ostream& os, const Person& person);
In the implementation, you should output the name
and age
of the Person
object to the output stream, and then return the output stream object.
Here's the complete implementation for overloading the output operator:
// Overloading the output operator friend std::ostream& operator<<(std::ostream& os, const Person& person) { os << "Name: " << person.name << ", Age: " << person.age; return os; }
Add the above code inside the Person
class to overload the output operator.
>>
):To overload the input operator for the Person
class, define a friend function with the following syntax:
friend std::istream& operator>>(std::istream& is, Person& person);
In the implementation, you should read the name
and age
of the Person
object from the input stream and then return the input stream object.
Here's the complete implementation for overloading the input operator:
// Overloading the input operator friend std::istream& operator>>(std::istream& is, Person& person) { std::cout << "Enter name: "; is >> person.name; std::cout << "Enter age: "; is >> person.age; return is; }
Add the above code inside the Person
class to overload the input operator.
Now you can use the overloaded input and output operators with your custom Person
objects:
int main() { Person person; // Read a person's data using the overloaded input operator std::cin >> person; // Output the person's data using the overloaded output operator std::cout << person << std::endl; return 0; }
In this example, we create a Person
object and use the overloaded input operator to read the person's data from the user. We then use the overloaded output operator to display the person's data.
That's it for our tutorial on overloading the input and output operators in C++. By overloading these operators, you can provide a more natural way to perform input and output operations with your custom class objects.
How to Overload the >> and << Operators in C++:
>>
) and output (<<
) stream operators, you define friend functions in your class.Example:
class MyClass { private: int data; public: // Overloading >> operator as a friend function friend std::istream& operator>>(std::istream& input, MyClass& obj); // Overloading << operator as a friend function friend std::ostream& operator<<(std::ostream& output, const MyClass& obj); };
Using Input and Output Operator Overloading for Custom I/O Behavior in C++:
>>
and <<
allows custom behavior during input and output operations.Example:
class Complex { private: double real; double imag; public: // Overloading >> operator for custom input friend std::istream& operator>>(std::istream& input, Complex& obj) { input >> obj.real >> obj.imag; return input; } // Overloading << operator for custom output friend std::ostream& operator<<(std::ostream& output, const Complex& obj) { output << obj.real << " + " << obj.imag << "i"; return output; } };
Overloading Stream Insertion and Extraction Operators in C++:
<<
) and extraction (>>
) operators can be overloaded to provide customized input and output.Example:
class CustomType { private: int value; public: // Overloading >> operator for custom input friend std::istream& operator>>(std::istream& input, CustomType& obj) { input >> obj.value; return input; } // Overloading << operator for custom output friend std::ostream& operator<<(std::ostream& output, const CustomType& obj) { output << obj.value; return output; } };
Chaining Input and Output Operator Overloading in C++:
Example:
class ChainedIO { private: int data; public: // Overloading >> operator for custom input friend std::istream& operator>>(std::istream& input, ChainedIO& obj) { input >> obj.data; return input; } // Overloading << operator for custom output friend std::ostream& operator<<(std::ostream& output, const ChainedIO& obj) { output << obj.data; return output; } };
Overloading >> and << Operators for User-Defined Types in C++:
Example:
class CustomType { private: int value; public: // Overloading >> operator for custom input friend std::istream& operator>>(std::istream& input, CustomType& obj) { input >> obj.value; return input; } // Overloading << operator for custom output friend std::ostream& operator<<(std::ostream& output, const CustomType& obj) { output << obj.value; return output; } };
Handling Input and Output Errors with Operator Overloading in C++:
Example:
class ErrorHandling { private: int data; public: // Overloading >> operator with error handling friend std::istream& operator>>(std::istream& input, ErrorHandling& obj) { if (!(input >> obj.data)) { // Handle input error obj.data = 0; // Default value input.clear(); // Clear error flag input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input } return input; } // Overloading << operator for custom output friend std::ostream& operator<<(std::ostream& output, const ErrorHandling& obj) { output << obj.data; return output; } };
Combining >> and << Operator Overloading with Other Operators in C++:
>>
and <<
can be combined with other operators for more complex behavior.Example:
class ComplexOperation { private: double real; double imag; public: // Combine >> and + operator overloading friend std::istream& operator>>(std::istream& input, ComplexOperation& obj) { input >> obj.real >> obj.imag; return input; } friend ComplexOperation& operator+(const ComplexOperation& lhs, const ComplexOperation& rhs) { ComplexOperation result; result.real = lhs.real + rhs.real; result.imag = lhs.imag + rhs.imag; return result; } };
Overloading Const Version of >> and << Operators in C++:
operator>>
and operator<<
is used when the object should not be modified during the operation.Example:
class ReadOnlyIO { private: int data; public: // Const version of >> operator for custom input friend std::istream& operator>>(std::istream& input, ReadOnlyIO& obj) { input >> obj.data; return input; } // Const version of << operator for custom output friend std::ostream& operator<<(std::ostream& output, const ReadOnlyIO& obj) { output << obj.data; return output; } };