• <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
            風的方向
            厚德致遠,博學敦行!
            posts - 52,comments - 21,trackbacks - 0
                  書上有一個用C++設計自己的輸出/輸入操作,涉及到運算符重載和流提取符重載,挺不錯的!
                  代碼如下: 
                  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" //將頭文件包括進去,不能寫成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)   //運算符+重載  參數為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)   //流提取符重載有兩個參數,第一個參數出現在<<操作符左側,為ostream引用,第二個出現在操作符<<右側,為COMPLEX引用,返回值是一個ostream的對象引用,下同
            {
                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;
            }

            輸出結果:
            posted on 2010-09-02 22:08 jince 閱讀(513) 評論(0)  編輯 收藏 引用 所屬分類: C++學習
            哈哈哈哈哈哈
            久久精品视频一| 久久久久99这里有精品10| 国内精品伊人久久久久| 久久九九全国免费| 欧美与黑人午夜性猛交久久久| 亚洲国产一成久久精品国产成人综合 | 亚洲欧美国产精品专区久久 | 午夜欧美精品久久久久久久| 2022年国产精品久久久久 | 久久亚洲天堂| 久久成人国产精品二三区| 漂亮人妻被中出中文字幕久久| 久久伊人精品青青草原高清| 伊人久久综合精品无码AV专区| 久久97久久97精品免视看秋霞| 精品一区二区久久久久久久网站| 久久精品国产亚洲αv忘忧草| 久久久久久A亚洲欧洲AV冫| av无码久久久久不卡免费网站| 中文无码久久精品| 伊人久久大香线蕉AV一区二区| 国产精品99久久不卡| 久久99精品久久久久久动态图| 久久久久久久久波多野高潮| 久久精品国产亚洲一区二区三区| 久久精品一区二区国产| 久久久久久人妻无码| 午夜天堂精品久久久久| 亚洲AV日韩精品久久久久久| 手机看片久久高清国产日韩| 久久人人爽人人爽AV片| 久久九九久精品国产免费直播| 色综合久久综精品| 日本久久久精品中文字幕| 久久亚洲欧美日本精品| 9999国产精品欧美久久久久久| 色综合久久中文色婷婷| 国产成人久久777777| 久久亚洲2019中文字幕| 国产精品99久久久精品无码| 久久久久久精品成人免费图片|