• <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í)
            哈哈哈哈哈哈
            久久国产影院| 久久天天躁狠狠躁夜夜2020老熟妇 | 久久精品国产亚洲AV无码娇色| 久久成人国产精品| 国产AV影片久久久久久| 久久www免费人成看片| 99热热久久这里只有精品68| 亚洲综合伊人久久综合| 久久国产精品免费一区| 久久99热只有频精品8| 亚洲国产精品狼友中文久久久| 国产精品久久久久国产A级| 日韩欧美亚洲综合久久影院Ds| 久久狠狠色狠狠色综合| 久久人人爽人人爽人人爽| 久久九九免费高清视频| 国产成人久久激情91| 无码人妻久久久一区二区三区| 久久久久噜噜噜亚洲熟女综合| 久久国产高清字幕中文| 久久久久久国产精品免费无码| 亚洲日本久久久午夜精品| 久久久久这里只有精品| 国产高潮国产高潮久久久91| 成人国内精品久久久久一区| 精品国产乱码久久久久久人妻 | 热综合一本伊人久久精品| 国产精品久久毛片完整版| 久久综合给久久狠狠97色| 久久久久亚洲av综合波多野结衣| 久久婷婷色综合一区二区| 国产免费久久精品丫丫| 久久免费视频观看| 99久久无码一区人妻| 久久青青草原国产精品免费| 久久99热国产这有精品| 久久99毛片免费观看不卡 | 亚洲国产日韩欧美综合久久| 久久久久久久亚洲精品| 久久久久无码国产精品不卡| 精品熟女少妇aⅴ免费久久|