1 //Flow class2 //to design a plural class into a stream plural class3#include <iostream.h>4 5Template <classT>6 classcomplex{7Friend Ostream &operator<< (ostream &os,complex <T> &com);8Friend IStream &operator>> (IStream & is, Complex <T> &com);9 Ten Private: One T Real; A T image; - Public: - Complex () { theReal=0; -Image=0; - } - Complex (T real,t image) { + This->real=Real; - This->image=image; + } A~Complex () {} at -Complexoperator+(ConstComplex &x)Const{ - returnComplex ( This->real+x.real, This->image+x.image); - } - }; - inTemplate <classT> -Ostream &operator<< (ostream &os,complex <T> &com) { toos<<"("<<com.real<<","<<com.image<<")"<<Endl; + returnos; - } the * $Template <classT>Panax NotoginsengIStream &operator>> (IStream & is, Complex <T> &com) { -cout<<"real="; the is>>com.real; +cout<<"image="; A is>>Com.image; the return is; + } - $ $ intMain () { -Complex <int> A (3,5), B (2,3), c,d; -c=a+b; thecout<<"c="<<c<<Endl; - WuyiCin>>D; thecout<<"d="<<d<<Endl; - Wu return 0; -}
C + + Learning (44)