• <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久久久久中文字幕| 久久毛片免费看一区二区三区| 伊人久久大香线蕉无码麻豆| 久久久久久久91精品免费观看| 无码国内精品久久人妻蜜桃| 丰满少妇人妻久久久久久| 国产精品久久久天天影视香蕉 | 伊人久久大香线蕉AV色婷婷色| 精品久久人妻av中文字幕| 久久久久久极精品久久久| 久久人妻少妇嫩草AV无码专区| 久久99精品久久久久久水蜜桃| 亚洲精品高清国产一线久久| 欧美亚洲另类久久综合婷婷| 99国产精品久久| 久久夜色精品国产网站| 久久免费视频1| 亚洲欧洲精品成人久久曰影片| 99国产精品久久| 2021久久国自产拍精品| 蜜臀av性久久久久蜜臀aⅴ| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 伊人久久大香线蕉成人| 久久精品国产99国产精品澳门| 99久久夜色精品国产网站| 天堂无码久久综合东京热| 国产精品成人久久久久三级午夜电影| 久久丫精品国产亚洲av不卡| 亚洲AV无码一区东京热久久| 波多野结衣久久| 色天使久久综合网天天| 久久久国产视频| 久久这里只有精品首页| 区久久AAA片69亚洲| 久久久久se色偷偷亚洲精品av| 久久嫩草影院免费看夜色| 久久九色综合九色99伊人| 久久丝袜精品中文字幕| 久久综合九色欧美综合狠狠| 婷婷久久综合九色综合绿巨人| 热久久国产欧美一区二区精品|