D-programming-overloading

提供:Dev Guides
移動先:案内検索

Dプログラミング-オーバーロード

Dを使用すると、同じスコープ内の*関数*名または*演算子*に対して複数の定義を指定できます。これらは、それぞれ*関数のオーバーロード*および*演算子のオーバーロード*と呼ばれます。

オーバーロードされた宣言は、同じスコープ内の前の宣言と同じ名前で宣言された宣言です。ただし、両方の宣言が異なる引数と明らかに異なる定義(実装)を持っている点が異なります。

オーバーロードされた function または operator を呼び出すと、コンパイラは、関数または演算子の呼び出しに使用した引数の型を定義で指定されたパラメーターの型と比較することにより、使用する最も適切な定義を決定します。 最も適切なオーバーロード関数または演算子を選択するプロセスは、*オーバーロード解決*と呼ばれます。

関数のオーバーロード

同じスコープ内で同じ関数名に複数の定義を設定できます。 関数の定義は、引数リスト内の引数のタイプまたは数、あるいはその両方によって互いに異なる必要があります。 戻り型のみが異なる関数宣言をオーバーロードすることはできません。

次の例では、同じ関数* print()*を使用して異なるデータ型を印刷します-

import std.stdio;
import std.string;

class printData {
   public:
      void print(int i) {
         writeln("Printing int: ",i);
      }

      void print(double f) {
         writeln("Printing float: ",f );
      }

      void print(string s) {
         writeln("Printing string: ",s);
      }
};

void main() {
   printData pd = new printData();

  //Call print to print integer
   pd.print(5);

  //Call print to print float
   pd.print(500.263);

  //Call print to print character
   pd.print("Hello D");
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Printing int: 5
Printing float: 500.263
Printing string: Hello D

オペレータの過負荷

Dで使用可能な組み込み演算子のほとんどを再定義またはオーバーロードできます。 したがって、プログラマはユーザー定義型の演算子も使用できます。

演算子は、文字列opの後にオーバーロードする演算子に基づいて、Add、Subなどを使用してオーバーロードできます。 以下に示すように、演算子+をオーバーロードして2つのボックスを追加できます。

Box opAdd(Box b) {
   Box box = new Box();
   box.length = this.length + b.length;
   box.breadth = this.breadth + b.breadth;
   box.height = this.height + b.height;
   return box;
}

次の例は、メンバー関数を使用した演算子のオーバーロードの概念を示しています。 ここでは、オブジェクトは引数として渡され、そのプロパティはこのオブジェクトを使用してアクセスされます。 この演算子を呼び出すオブジェクトは、以下で説明するように this 演算子を使用してアクセスできます-

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 opAdd(Box b) {
         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);
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

演算子のオーバーロードの種類

基本的に、以下にリストする3つのタイプの演算子のオーバーロードがあります。

Sr.No. Overloading Types
1 Unary Operators Overloading
2 Binary Operators Overloading
3 Comparison Operators Overloading