程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++讀書筆記之 關系運算符重載 大於號> 小於號< 等於號== 重載 overload

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

編輯:C++入門知識

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;

        }

 

 

 


[cpp]
#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.
 
****************************/ 

#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

 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved