程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++讀書筆記之 賦值運算符= 重載 前綴自減運算符--重載 求負運算符- 重載

C++讀書筆記之 賦值運算符= 重載 前綴自減運算符--重載 求負運算符- 重載

編輯:C++入門知識

/**重載賦值運算符 = **/
      void operator=(const Distance &D )
      {
         feet = D.feet;
         inches = D.inches;
      }
      /**重載負號運算符 -  **/
        Distance operator-()
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
      /**重載前綴自減運算符 --  **/
        Distance operator--()
        {
            --feet;
            --inches;
            return Distance(feet, inches);
        }
         /**重載後綴自減運算符 --  **/
        Distance operator--(int)
        {
            Distance Temp(feet,inches);
            feet--;
            inches--;
            return Temp;

        }

 

 

 


[cpp] 
#include <iostream>  
using namespace std; 
 
class Distance 

   private: 
      double feet;             // 0 to infinite  
      double inches;           // 0 to 12  
   public: 
      // required constructors  
      Distance() 
      { 
         feet = 0.0; 
         inches = 0.0; 
      } 
      Distance(double f, double i) 
      { 
         feet = f; 
         inches = i; 
      } 
      /**重載賦值運算符 = **/ 
      void operator=(const Distance &D ) 
      { 
         feet = D.feet; 
         inches = D.inches; 
      } 
      /**重載負號運算符 -  **/ 
        Distance operator-() 
      { 
         feet = -feet; 
         inches = -inches; 
         return Distance(feet, inches); 
      } 
      /**重載前綴自減運算符 --  **/ 
        Distance operator--() 
        { 
            --feet; 
            --inches; 
            return Distance(feet, inches); 
        } 
         /**重載後綴自減運算符 --  **/ 
        Distance operator--(int) 
        { 
            Distance Temp(feet,inches); 
            feet--; 
            inches--; 
            return Temp; 
        } 
 
      // method to display distance  
      void displayDistance() 
      { 
         cout << "Feet: " << feet <<  " Inches:" <<  inches << endl; 
      } 
 
}; 
int  main() 

    Distance D1(19.91, -20.13),D2; 
    cout << "\nD1 Distance now: \n"; 
    D1.displayDistance(); 
    --D1; 
    cout << "\nD1 Distance after  --D1  : \n"; 
    D1.displayDistance(); 
 
    -D1; 
    cout << "\nD1 Distance after  -D1  : \n"; 
    D1.displayDistance(); 
 
    cout << "\nD1--.displayDistance():\n"; 
    D1--.displayDistance(); 
 
    // use assignment operator  
    D2 = D1--; 
    cout << "\nafter D2=D1-- Distance of D2 :\n"; 
    D2.displayDistance(); 
 
    cout << "\nafter D2=D1-- Distance of D1 :\n"; 
    D1.displayDistance(); 
 
    return 0; 

/**************************
運行結果:
 
D1 Distance now:
Feet: 19.91 Inches:-20.13
 
D1 Distance after  --D1  :
Feet: 18.91 Inches:-21.13
 
D1 Distance after  -D1  :
Feet: -18.91 Inches:21.13
 
D1--.displayDistance():
Feet: -18.91 Inches:21.13
 
after D2=D1-- Distance of D2 :
Feet: -19.91 Inches:20.13
 
after D2=D1-- Distance of D1 :
Feet: -20.91 Inches:19.13
 
Process returned 0 (0x0)   execution time : 0.819 s
Press any key to continue.
 
***************************/ 

#include <iostream>
using namespace std;

class Distance
{
   private:
      double feet;             // 0 to infinite
      double inches;           // 0 to 12
   public:
      // required constructors
      Distance()
      {
         feet = 0.0;
         inches = 0.0;
      }
      Distance(double f, double i)
      {
         feet = f;
         inches = i;
      }
      /**重載賦值運算符 = **/
      void operator=(const Distance &D )
      {
         feet = D.feet;
         inches = D.inches;
      }
      /**重載負號運算符 -  **/
        Distance operator-()
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
      /**重載前綴自減運算符 --  **/
        Distance operator--()
        {
            --feet;
            --inches;
            return Distance(feet, inches);
        }
         /**重載後綴自減運算符 --  **/
        Distance operator--(int)
        {
            Distance Temp(feet,inches);
            feet--;
            inches--;
            return Temp;
        }

      // method to display distance
      void displayDistance()
      {
         cout << "Feet: " << feet <<  " Inches:" <<  inches << endl;
      }

};
int  main()
{
    Distance D1(19.91, -20.13),D2;
    cout << "\nD1 Distance now: \n";
    D1.displayDistance();
    --D1;
    cout << "\nD1 Distance after  --D1  : \n";
    D1.displayDistance();

    -D1;
    cout << "\nD1 Distance after  -D1  : \n";
    D1.displayDistance();

    cout << "\nD1--.displayDistance():\n";
    D1--.displayDistance();

    // use assignment operator
    D2 = D1--;
    cout << "\nafter D2=D1-- Distance of D2 :\n";
    D2.displayDistance();

    cout << "\nafter D2=D1-- Distance of D1 :\n";
    D1.displayDistance();

    return 0;
}
/**************************
運行結果:

D1 Distance now:
Feet: 19.91 Inches:-20.13

D1 Distance after  --D1  :
Feet: 18.91 Inches:-21.13

D1 Distance after  -D1  :
Feet: -18.91 Inches:21.13

D1--.displayDistance():
Feet: -18.91 Inches:21.13

after D2=D1-- Distance of D2 :
Feet: -19.91 Inches:20.13

after D2=D1-- Distance of D1 :
Feet: -20.91 Inches:19.13

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

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


 

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