• <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 閱讀(510) 評論(0)  編輯 收藏 引用 所屬分類: C++學習
            哈哈哈哈哈哈
            久久www免费人成看片| 手机看片久久高清国产日韩| 亚洲精品乱码久久久久久按摩| 亚洲中文字幕无码久久精品1| 午夜精品久久久久久久久| 久久精品国内一区二区三区| 久久精品无码一区二区三区免费| 久久免费看黄a级毛片| A级毛片无码久久精品免费| 亚洲欧美一级久久精品| 国产国产成人精品久久| 久久久WWW成人| 久久美女人爽女人爽| AV无码久久久久不卡蜜桃| 久久国产精品免费| 97久久久精品综合88久久| 中文成人无码精品久久久不卡| 久久精品草草草| 狠狠综合久久综合88亚洲| 日本免费久久久久久久网站| 久久久久亚洲av综合波多野结衣| 国产成人99久久亚洲综合精品| 久久亚洲精品人成综合网| 国产精品久久新婚兰兰| 久久久久亚洲AV成人网| 国产成人久久激情91| 久久久久成人精品无码中文字幕 | 久久66热人妻偷产精品9| 亚洲国产成人乱码精品女人久久久不卡| 久久99亚洲网美利坚合众国| 亚洲精品无码专区久久久 | 精品无码久久久久国产动漫3d | 久久国产精品-国产精品| 国产亚洲精品自在久久| 久久亚洲春色中文字幕久久久| 亚洲人成网亚洲欧洲无码久久| 人妻无码αv中文字幕久久琪琪布| 亚洲精品无码专区久久同性男| 亚洲欧美久久久久9999| 亚洲欧美一区二区三区久久| 人妻系列无码专区久久五月天|