• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            voip
            風(fēng)的方向
            厚德致遠(yuǎn),博學(xué)敦行!
            posts - 52,comments - 21,trackbacks - 0
                  書上有一個(gè)用C++設(shè)計(jì)自己的輸出/輸入操作,涉及到運(yùn)算符重載和流提取符重載,挺不錯(cuò)的!
                  代碼如下: 
                  COMPLEX.HPP文件
            #include<iostream.h>
            class COMPLEX {
            public :
                COMPLEX(
            double r=0,double i=0);
                COMPLEX(
            const COMPLEX& other);
                COMPLEX 
            operator +(const COMPLEX& other);
                COMPLEX 
            operator -(const COMPLEX& other);
                COMPLEX 
            operator -();
                COMPLEX 
            operator =(const COMPLEX &other);
                friend ostream
            & operator <<(ostream& stream,COMPLEX& obj);
                friend istream
            & operator <<(istream& stream,COMPLEX& obj);
            public :
                
            double real,image;
            }
            ;
                  COMPLEX.CPP文件
            //COMPLEX.CPP文件


            #include 
            "COMPLEX.HPP" //將頭文件包括進(jìn)去,不能寫成include<COMPLEX.CPP>。。。

            COMPLEX::COMPLEX(
            double r,double i)   
            {
                real
            =r;
                image
            =i;
                
            return ;
            }


            COMPLEX::COMPLEX(
            const COMPLEX& other)
            {
                real
            =other.real;
                image
            =other.image;
                
            return ;
            }


            COMPLEX COMPLEX::
            operator +(const COMPLEX& other)   //運(yùn)算符+重載  參數(shù)為COMPLEX類型,返回值為COMPLEX類型的引用,下同
            {
                COMPLEX temp;

                temp.real
            =real+other.real;
                temp.image
            =image+other.image;
                
            return temp;
            }


            COMPLEX COMPLEX::
            operator -(const COMPLEX& other)
            {
                COMPLEX temp;

                temp.real
            =real-other.real;
                temp.image
            =image-other.image;
                
            return temp;
            }



            COMPLEX COMPLEX::
            operator =(const COMPLEX& other)
            {
                real
            =other.real;
                image
            =other.image;
                
            return *this;
            }


            ostream
            & operator<<(ostream& stream,COMPLEX& obj)   //流提取符重載有兩個(gè)參數(shù),第一個(gè)參數(shù)出現(xiàn)在<<操作符左側(cè),為ostream引用,第二個(gè)出現(xiàn)在操作符<<右側(cè),為COMPLEX引用,返回值是一個(gè)ostream的對(duì)象引用,下同
            {
                stream
            <<obj.real;
                
            if(obj.image>0)  stream<<"+"<<obj.image<<"i";
                
            else if(obj.image<0) stream<<obj.image<<"i";
                
            return stream;
            }


            istream
            & operator>>(istream& stream,COMPLEX& obj)
            {
                cout
            <<"Input real part:";
                stream
            >>obj.real;
                cout
            <<"input image part:";
                stream
            >>obj.image;
                
            return stream;
            }


            int main()
            {
                COMPLEX c1,c2;

                cout
            <<"Input the first complex number c1:"<<endl;

                cin
            >>c1;

                cout
            <<"Input the second compex number c2:"<<endl;

                cin
            >>c2;

                cout
            <<"c1 is"<<c1<<endl;
                
                cout
            <<"c2 is"<<c2<<endl;

                cout
            <<"c1+c2 is "<<c2+c1<<endl;

                cout
            <<"c1-c2 is "<<c1-c2<<endl;
                
            //操作符原功能任存在

                
            int a=50,b=10;

                cout
            <<"A="<<a<<"\tB"<<b<<endl;

                cout
            <<"A+B is "<<a+b<<endl;

                cout
            <<"A-B is "<<a-b<<endl;

                a
            =-b;
                cout
            <<"A=-B,A="<<a<<"\tB"<<b<<endl;

                a
            =b;
                cout
            <<"A=B,A="<<a<<"\tB"<<b<<endl;
                
            return 0;
            }

            輸出結(jié)果:
            posted on 2010-09-02 22:08 jince 閱讀(510) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++學(xué)習(xí)
            哈哈哈哈哈哈
            国内精品久久久久久99| 久久亚洲国产精品一区二区| 亚洲精品成人久久久| 久久婷婷色香五月综合激情 | 亚洲国产天堂久久久久久| 久久中文字幕精品| 久久97精品久久久久久久不卡| 久久久WWW成人| 97久久香蕉国产线看观看| 性做久久久久久久久浪潮| 精品久久一区二区| 国产偷久久久精品专区| 国产国产成人久久精品| 久久久久久亚洲AV无码专区| 欧洲性大片xxxxx久久久| 国产婷婷成人久久Av免费高清| 久久男人中文字幕资源站| 精品久久久久久亚洲精品| 伊人久久五月天| 青青草原综合久久大伊人导航| 91精品观看91久久久久久| 性做久久久久久久| 狠狠色噜噜色狠狠狠综合久久 | 久久国产精品99久久久久久老狼 | 精品久久久久久中文字幕人妻最新| 国产精自产拍久久久久久蜜| 久久99国产综合精品| 亚洲精品国精品久久99热一| 久久国产亚洲精品| 日韩影院久久| 久久综合亚洲色HEZYO国产| 亚洲欧美日韩精品久久| 99久久国产综合精品五月天喷水 | 亚洲精品高清一二区久久| 青青草国产精品久久久久| 久久夜色精品国产亚洲| 97超级碰碰碰碰久久久久| 精品久久久久久久久久久久久久久 | 久久久久国色AV免费看图片| 亚洲一区二区三区日本久久九| 国产成人综合久久综合|