Overloads of the stream insert operator << and stream extraction operator >> for C + +

Source: Internet
Author: User

The C + + stream insert operator << and stream extraction operator ">>" are provided by C + + in the class library, and all C + + compilation systems provide the input stream class IStream and the output stream class Ostream in the class library. CIN and cout are objects of the IStream class and the Ostream class, respectively. "<<" and ">>" have been overloaded in the header file provided by the class library as the stream insert operator and stream extraction operator, which can be used to output and enter data of the C + + standard type. Therefore, all the "cout<<" and "cin>>" to the standard type of data input and output, you have to use # include head file included in this program file.

User-defined types of data cannot be exported and entered directly with "<<" and ">>". If you want to use them to output and enter data of the type that you declare, you must overload them.

The functions for the "<<" and ">>" overloads are as follows:
IStream & operator >> (IStream &, custom classes &);
Ostream & operator << (ostream &, custom classes &);
The first parameter of the function that overloads the operator ">>" and the type of the function must be the istream& type, and the second argument is the class to enter the operation. The first argument and function of a function that overloads "<<" must be of type ostream&, and the second argument is the class to which the output operation is performed. Therefore, the functions of overloading ">>" and "<<" can only be used as friend functions or ordinary functions, and they cannot be defined as member functions;

File:complex.h

#pragma once#include <iostream>using namespace STD;classcomplex{ Public: Complex () {real =0; Imag =0;} Complex (DoubleRDoublei) {real = r; imag = i;}friendOstream &operator<< (Ostream&, complex&);friendIStream &operator>> (Istream&, complex&); Complexoperator+ (COMPLEX&AMP;C2);Private:DoubleRealDoubleImag;};

Fiel:Complex.cpp

#include"StdAfx.h"#include"Complex.h"ostream& operator<< (Ostream&output, complex&c) {output <<"("<< c.Real<<"+"<< c.Imag<<"i)"<< Endl;returnOutput;} istream& operator>> (Istream&intput, complex&c) {cout <<"Input real " and imaginary part of complex number "; Intput >> c.Real>> c.Imag;returnIntput;} Complex complex::operator+ (complex& C2) {returnComplex (Real+ c2.Real,Imag+ c2.Imag);}
#include "stdafx.h"#include "Complex.h"#include <iostream>usingnamespacestd;int _tmain(int argc, _TCHAR* argv[]){    Complex c1, c2, c3;    cin >> c1 >> c2;    c3 = c1 + c2;    cout << c3;    system("pause");    return0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Overloads of the stream insert operator << and stream extraction operator >> for C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.