Explain overloading of insertion and extraction operators with an example.

Estudies4you
overloading of insertion and extraction operators

 4.5. Overloading Operators

Q21. Explain overloading of insertion and extraction operators with an example.
Answer:
In C++, the operator << is called an insertion operator because it inserts the characters into a stream and the operator >> is called an extraction operator because it extracts characters from a stream. Like other operators such as +, —, *, etc. the operators >> and << can/also be overloaded. The functions that overload the insertion and extraction operators are called inserter and extractor respectively.
The prototypes definition for insertion (<<) and extraction (>>) operators are shown below.
Prototypes:

friend istream& operator>>(istream& streamobj, ClassName& classobj);

friend ostream& operator<<(ostream& streamobj, const ClassName& classobj);

 Definitions

istreams& operator>>(istream& streamobj, ClassName& classobj)

{

// code

}

ostream& operator<<(ostream& streamobj, const ClassName& classobj)

{

/ code

}

Extraction Operator Overloading

The statement cin >> obj; is a statement that overloads the operator >>, like any other operator such as ‘+’ operator, >> operator has two operands, one is cin i.e., the object of input stream and the other operand is an object of a class that receives the input value. Therefore the second argument passed to an overloaded operator >>( ) function must be a call by reference parameter. The purpose of this statement is to accept an input from the keyboard. When this statement is executed it calls the operator >>() function. The value returned by this function is an objegt of an input stream (istream).

Insertion Operator Overloading
The statement cout<<“Hello”; is an overloaded statement that overloads the operator <<. This operator also has two operands cout (object of output stream) and a string “Hello”. The second operand may be either a string, a variable or a number.
The purpose of this statement is to print the string “Hello” to the screen. When this statement is executed it calls the overloaded operator >>() function. The value returned by this function is an object of the output stream (ostream).
Both the overloaded operator functions return a stream. The symbol ‘&’ at the end of name of the stream represents that the operator function returns a reference i.e., it returns an object of the stream rather than returning the value of the stream itself.
The program below illustrates the operation of overloading both the insertion and extraction operators.

Example

#include <iostream>

using namespace std;

class Complex

{

private:

double real, imag;

public:

Complex() { }

Complex(double r, double i)

{

real=r;

imag =1;

}

friend ostream& operator<<(ostream& output, Complex& obj);

friend istream& operator>>(istream& input, Complex& obj);

};

ostream& operator<<(ostream& output, Complex& obj)

{

output<<obj.real<< “,”<<obj imag;

return output;

}

istream& operator >>(istream& input, Complex& obj)

{

input >>obj.real>>obj.imag;

return input;

}

int main()

{

Complex obj1(2.5, 3.5);

Complex obj2(1.0, 4.6);

cout<<obj 1<<endl<<obj2;

}

 Output

Overloading of insertion operators in c++,overloading of extraction operators in c++,exmples of overloading operators in c++,overloading operators in c++,overloading examples in c++,what is overloading operators,how to insert overloading operator in c++,how to extract overloading operators in c++

To Top