C++读书笔记之 关系运算符重载 大于号; 小于号< 等于号== 重载 overload

Posted 王世晖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


以上是关于C++读书笔记之 关系运算符重载 大于号; 小于号< 等于号== 重载 overload的主要内容,如果未能解决你的问题,请参考以下文章

20190816

C++如何重载指针的比较符

重载小于号

c++中加号运算符重载

C++结构体使用及运算符重载

C++结构体使用及运算符重载