Overloading stream insertion (<>) operators in C++ Last Updated : 16 Jun, 2021 Comments Improve Suggest changes Like Article Like Report In C++, stream insertion operator "<<" is used for output and extraction operator ">>" is used for input. We must know the following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object of istream class 2) These operators must be overloaded as a global function. And if we want to allow them to access private data members of the class, we must make them friend. Why these operators must be overloaded as global? In operator overloading, if an operator is overloaded as a member, then it must be a member of the object on the left side of the operator. For example, consider the statement "ob1 + ob2" (let ob1 and ob2 be objects of two different classes). To make this statement compile, we must overload '+' in a class of 'ob1' or make '+' a global function. The operators '<<' and '>>' are called like 'cout << ob1' and 'cin >> ob1'. So if we want to make them a member method, then they must be made members of ostream and istream classes, which is not a good option most of the time. Therefore, these operators are overloaded as global functions with two parameters, cout and object of user-defined class.Following is a complete C++ program to demonstrate overloading of <> operators. CPP #include <iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) { real = r; imag = i; } friend ostream & operator << (ostream &out, const Complex &c); friend istream & operator >> (istream &in, Complex &c); }; ostream & operator << (ostream &out, const Complex &c) { out << c.real; out << "+i" << c.imag << endl; return out; } istream & operator >> (istream &in, Complex &c) { cout << "Enter Real Part "; in >> c.real; cout << "Enter Imaginary Part "; in >> c.imag; return in; } int main() { Complex c1; cin >> c1; cout << "The complex object is "; cout << c1; return 0; } Output: Enter Real Part 10 Enter Imaginary Part 20 The complex object is 10+i20 Comment More infoAdvertise with us Next Article Overloading stream insertion (<>) operators in C++ kartik Follow Improve Article Tags : C++ cpp-operator-overloading cpp-overloading Practice Tags : CPP Similar Reads Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read Input/Output Operators Overloading in C++ Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We c 2 min read Increment (++) and Decrement (--) Operator Overloading in C++ Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types. Why Operator Overloading? Let's say we have defined a class Integer for handling operations on integers. We can have functions add(), subtr 4 min read Rules for operator overloading In C++, following are the general rules for the things that are not allowed with operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) O 3 min read Operator Overloading '<<' and '>>' operator in a linked list class Prerequisite: Operator Overloading in C++, Linked List in C++ C++ comes with libraries that provide ways for performing Input and Output. In C++, Input and Output are performed as a sequence of bytes, also known as streams. Input and Output stream are managed by the iostream library. cin and cout ar 4 min read Operator overloading in C++ to print contents of vector, map, pair, .. Operator overloading is one of the features of Object oriented programming which gives an extra ability to an operator to act on a User-defined operand(Objects). We can take advantage of that feature while debugging the code specially in competitive programming. All we need to do is to overload the 3 min read Error: std::endl is of Unknown Type when Overloading Operator << In C++, the << operator is commonly overloaded for custom output for user-defined types using streams. However, an issue may arise when using std::endl with an overloaded operator<<. It is shown as the type of std::endl being unknown, leading to compilation errors.In this article, we wil 3 min read list::operator= in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign n 2 min read What are the Operators that Can be and Cannot be Overloaded in C++? There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are:Â + - Â * ? % ? Â & | Â ~ ! = Â < > += -= Â *= ?= %= ?= &= |= Â << 4 min read unordered_multimap operator= in C++ The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below. Syntax (copying elements from different container) : unordered_multimap_name1 operator= (unordered_multimap_name2)Parameters: The function does not accepts any parameter. Th 4 min read Like