在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C++/ 二元運(yùn)算符重載
類的訪問(wèn)修飾符
D語(yǔ)言運(yùn)算符
D語(yǔ)言邏輯運(yùn)算符
類指針
D語(yǔ)言元組
D語(yǔ)言指針
D語(yǔ)言模塊
D語(yǔ)言sizeof運(yùn)算符
D語(yǔ)言混合類型
D語(yǔ)言封裝
D語(yǔ)言條件編譯
類的靜態(tài)成員
D語(yǔ)言do...while循環(huán)
D語(yǔ)言結(jié)構(gòu)體
重載
D語(yǔ)言字符串-String
D語(yǔ)言決策語(yǔ)句
D語(yǔ)言接口
D語(yǔ)言for循環(huán)
D語(yǔ)言switch語(yǔ)句
D語(yǔ)言關(guān)聯(lián)數(shù)組
D語(yǔ)言范圍
D語(yǔ)言枚舉Enums
契約式編程
D語(yǔ)言并發(fā)
D語(yǔ)言開(kāi)發(fā)環(huán)境設(shè)置
D語(yǔ)言別名
D語(yǔ)言常值
D語(yǔ)言常量
D語(yǔ)言函數(shù)
D語(yǔ)言if嵌套語(yǔ)句
D語(yǔ)言循環(huán)
D語(yǔ)言概述,D語(yǔ)言是什么?
D語(yǔ)言運(yùn)算符優(yōu)先級(jí)
D語(yǔ)言continue語(yǔ)句
D語(yǔ)言異常處理
D語(yǔ)言break語(yǔ)句
D語(yǔ)言if...else語(yǔ)句
D語(yǔ)言類和對(duì)象
類繼承
D語(yǔ)言字符
D語(yǔ)言教程
D語(yǔ)言關(guān)系運(yùn)算符
比較操作符重載
構(gòu)造函數(shù)和析構(gòu)函數(shù)
D語(yǔ)言抽象類
D語(yǔ)言if語(yǔ)句
D語(yǔ)言賦值運(yùn)算符
D中算術(shù)運(yùn)算符
D語(yǔ)言類成員函數(shù)
D語(yǔ)言位運(yùn)算符
D語(yǔ)言變量
D語(yǔ)言數(shù)據(jù)類型
D語(yǔ)言文件I/O
D語(yǔ)言數(shù)組
一元運(yùn)算符重載
D語(yǔ)言嵌套switch語(yǔ)句
D語(yǔ)言基本語(yǔ)法
二元運(yùn)算符重載
this指針
D語(yǔ)言聯(lián)合體
D語(yǔ)言模板
D語(yǔ)言嵌套循環(huán)
D語(yǔ)言while循環(huán)

二元運(yùn)算符重載

下表顯示了二元運(yùn)算符,其目的列表。

函數(shù)名稱 運(yùn)算符 目的
opBinary + 加法
opBinary - 減法
opBinary * 乘法
opBinary / 除法
opBinary % 求余
opBinary ^^
opBinary & 按位與
opBinary | 按位或
opBinary ^ 按位異或
opBinary << 左移
opBinary >> 右移
opBinary >>> 邏輯右移
opBinary ~ 串連
opBinary in 是否包含

一個(gè)例子如下,解釋如何重載一個(gè)二元運(yùn)算符。

import std.stdio;

class Box
{
   public:

      double getVolume()
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
         length = len;
      }

      void setBreadth( double bre )
      {
         breadth = bre;
      }

      void setHeight( double hei )
      {
         height = hei;
      }
      Box opBinary(string op)(Box b)
      {
         if(op == "+")
         {
            Box box = new Box();
            box.length = this.length + b.length;
            box.breadth = this.breadth + b.breadth;
            box.height = this.height + b.height;
            return box;
          }
      }
   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};
// Main function for the program
void main( )
{
   Box box1 = new Box();    // Declare Box1 of type Box
   Box box2 = new Box();    // Declare Box2 of type Box
   Box box3 = new Box();    // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here

   // box 1 specification
   box1.setLength(6.0);
   box1.setBreadth(7.0);
   box1.setHeight(5.0);

   // box 2 specification
   box2.setLength(12.0);
   box2.setBreadth(13.0);
   box2.setHeight(10.0);

   // volume of box 1
   volume = box1.getVolume();
   writeln("Volume of Box1 : ", volume);

   // volume of box 2
   volume = box2.getVolume();
   writeln("Volume of Box2 : ", volume);

   // Add two object as follows:
   box3 = box1 + box2;

   // volume of box 3
   volume = box3.getVolume();
   writeln("Volume of Box3 : ", volume);

}

讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果: