1. 程式人生 > >C++讀書筆記之 關係運算符過載 大於號> 小於號< 等於號== 過載 overload

C++讀書筆記之 關係運算符過載 大於號> 小於號< 等於號== 過載 overload

public:
        static const double absError=0.0000001;
        static  bool IsEqual(double a, double b)
        {
            if (a==b)
                return true;
            if (fabs(a-b)<absError)
                return true;
            return false;
        } 

/**過載賦值運算子後 交換兩個類的資料**/

        void swap(Distance& d)
        {
            Distance temp;
            temp=*this;
            *this=d;
            d=temp;
        }
       
        /*** overloaded < operator 過載小於號 ***/
        bool operator <(const Distance& d)
        {
             if((feet - d.feet<absError)||(feet == d.feet && inches - d.inches<absError))
             {
                return true;
             }
             return false;
        }
        /*** overloaded > operator  過載大於號***/
        bool operator > (const Distance& d)
        {
             if((feet -d.feet>absError)||(feet == d.feet && inches - d.inches>absError))
             {
                return true;
             }
             return false;
        }
        /*** overloaded == operator 過載等於號 ***/
        bool operator ==(const Distance& d)
        {
             if(IsEqual(feet,d.feet) && IsEqual(inches,d.inches))
             {
                return true;
             }
             return false;

        }

#include <iostream>
#include <math.h>
using namespace std;

class Distance
{
    private:
        double feet;             // 0 to infinite
        double inches;
    public:
        static const double absError=0.0000001;
        static  bool IsEqual(double a, double b)
        {
            if (a==b)
                return true;
            if (fabs(a-b)<absError)
                return true;
            return false;
        }
        // required constructors
        Distance()
        {
             feet = 0;
             inches = 0;
        }
        Distance(double f, double i)
        {
             feet = f;
             inches = i;
        }
        /**重新設定資料**/
        void set(double f, double i)
        {
             feet = f;
             inches = i;
        }
        /**過載賦值運算子後 交換兩個類的資料**/
        void swap(Distance& d)
        {
            Distance temp;
            temp=*this;
            *this=d;
            d=temp;
        }
        // method to display distance
        void displayDistance()
        {
            cout <<" Feet: " << feet << " Inches:" << inches <<endl;
        }
        /*** overloaded minus (-) operator  ***/
        Distance operator- ()
        {
             feet = -feet;
             inches = -inches;
             return Distance(feet, inches);
        }
        /*** overloaded < operator 過載小於號 ***/
        bool operator <(const Distance& d)
        {
             if((feet - d.feet<absError)||(feet == d.feet && inches - d.inches<absError))
             {
                return true;
             }
             return false;
        }
        /*** overloaded > operator  過載大於號***/
        bool operator > (const Distance& d)
        {
             if((feet -d.feet>absError)||(feet == d.feet && inches - d.inches>absError))
             {
                return true;
             }
             return false;
        }
        /*** overloaded == operator 過載等於號 ***/
        bool operator ==(const Distance& d)
        {
             if(IsEqual(feet,d.feet) && IsEqual(inches,d.inches))
             {
                return true;
             }
             return false;
        }

};


int main()
{
    Distance D1(20.08,5.12 ), D2(20.13, 4.20);
    cout<<"before swap :\n";
    cout<<"D1:---->  ";
    D1.displayDistance();
    cout<<"D2:---->  ";
    D2.displayDistance();
    if( D1 < D2 )
    {
      cout << "D1 is less than D2 " << endl;
    }
    else
    {
      cout << "D2 is less than D1 " << endl;
    }
    D1.swap(D2);
    cout<<"after swap :\n";
    cout<<"D1:---->  ";
    D1.displayDistance();
    cout<<"D2:---->  ";
    D2.displayDistance();

     if( D1 < D2 )
    {
      cout << "D1 is less than D2 " << endl;
    }
    else
    {
      cout << "D2 is less than D1 " << endl;
    }

    double a=0.33,b=0.33;
    cout<<"\ndouble a=0.33,b=0.33 then Distance::IsEquall(a,b) = "
        <<Distance::IsEqual(a,b)<<endl;


    cout<<"after  set(199.1,11.05)\n";
    D1.set(199.1,11.05);
    D2.set(199.1,11.05);
    D1.displayDistance();
    D2.displayDistance();

    if(D1==D2)
    {
        cout<<"\n D1 equal D2 \n";
    }
    return 0;
}
/**************************
執行結果:
before swap :
D1:---->   Feet: 20.08 Inches:5.12
D2:---->   Feet: 20.13 Inches:4.2
D1 is less than D2
after swap :
D1:---->   Feet: 20.13 Inches:4.2
D2:---->   Feet: 20.08 Inches:5.12
D2 is less than D1

double a=0.33,b=0.33 then Distance::IsEquall(a,b) = 1
after  set(199.1,11.05)
 Feet: 199.1 Inches:11.05
 Feet: 199.1 Inches:11.05

 D1 equal D2

Process returned 0 (0x0)   execution time : 0.894 s
Press any key to continue.

****************************/

執行結果:
before swap :
D1:---->   Feet: 20.08 Inches:5.12
D2:---->   Feet: 20.13 Inches:4.2
D1 is less than D2
after swap :
D1:---->   Feet: 20.13 Inches:4.2
D2:---->   Feet: 20.08 Inches:5.12
D2 is less than D1


double a=0.33,b=0.33 then Distance::IsEquall(a,b) = 1
after  set(199.1,11.05)
 Feet: 199.1 Inches:11.05
 Feet: 199.1 Inches:11.05


 D1 equal D2