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)                 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              {
                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 
#include 
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) 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) = "
        <   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


你可能感兴趣的:(C/C++读书笔记)