• <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 閱讀(500) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++學(xué)習(xí)
            哈哈哈哈哈哈
            久久婷婷国产麻豆91天堂| 久久精品国产精品亚洲精品 | 久久午夜综合久久| 要久久爱在线免费观看| 久久综合国产乱子伦精品免费| 久久美女人爽女人爽| 国产精品乱码久久久久久软件| 色狠狠久久AV五月综合| 久久精品?ⅴ无码中文字幕| 少妇高潮惨叫久久久久久| 久久99精品九九九久久婷婷| 亚洲精品无码专区久久久 | 99久久综合国产精品免费| 99久久99久久久精品齐齐| 色婷婷综合久久久久中文字幕 | 国产精品久久久久久久app| 韩国三级大全久久网站| 一本久久a久久精品vr综合| 精品久久久久国产免费| 久久精品国产69国产精品亚洲| 久久国产色av免费看| 亚洲国产日韩欧美综合久久| 精品久久久久久无码人妻热| 久久发布国产伦子伦精品| av色综合久久天堂av色综合在| 国产免费福利体检区久久| 国产亚洲欧美成人久久片| 久久精品国产第一区二区三区| 精品国产乱码久久久久久呢| 亚洲成av人片不卡无码久久 | 久久66热人妻偷产精品9| 久久久久国产精品嫩草影院| 久久久亚洲欧洲日产国码是AV| 日韩欧美亚洲综合久久影院Ds | 国产精品无码久久久久久| 亚洲女久久久噜噜噜熟女| 2020国产成人久久精品| 久久久久久精品久久久久| 久久人人爽人人爽人人片AV高清 | 久久精品国产精品亚洲| 91精品久久久久久无码|