Class Complex

Attributes:
   double imag;
   double real;
Operations:
   Complex()                  // Simple constructor   Body:
   {
      real = imag = 0.0;
   }
   Complex(const Complex& c)  //Copy constructor   Arguments: const Complex& c
   Body:
   {
      real = c.real;
      imag = c.imag;
   }
   Complex(double r, double i) // Convert constructor   Arguments:  double r
               double i = 0.0
   Body: 
   {
      real = r;
      imag = i;
   }

   operator-(Complex c)        // Subtraction   Return type: Complex
   Arguments: Complex c
   Body: 
   {
      return Complex(real - c.real, imag - c.imag);
   }
   operator[](int index)      // Array subscript   Return type: Complex&
   Arguments: int index       // dummy operator - only 
                              // for instrumentation
                              // check
   Body: 
   {
      return *this;
   }
   operator+(Complex& c)      // Addition by value   Return type: Complex
   
   Arguments: Complex& c
   Body: 
   {
      return Complex(real + c.real,imag + c.imag);
   }
   operator+(Complex* c)      // Addition by reference   Return type: Complex*
   Arguments: Complex *c
   Body: 
   {
      cGlobal = new Complex (real + c->real, 
         imag + c->imag);
      return cGlobal;
   }
   operator++()               // Prefix increment   Return type: Complex&
   Body: 
   {
      real += 1.0;
      imag += 1.0;
      return *this;
   }
   operator=(Complex& c)      // Assignment by value   Return type: Complex&
   Arguments: Complex& c
   Body: 
   {
      real = c.real,
      imag = c.imag;
      return *this;
   }



   operator=(Complex* c)      // Assignment by reference   Return type: Complex*
   Arguments: Complex *c
   Body: 
   {
      real = c->real;
      imag = c->imag;
      return this;
   }

以下に、これらのオーバーロードされた演算子用に生成されたコードの例を示します。

オーバーロードされた前置インクリメント演算子用に生成されたコード

Complex& Complex::operator++() {
   NOTIFY_OPERATION(operator++, operator++(), 0,
      operator_SERIALIZE);
   //#[ operation operator++()
   real += 1.0;
   imag += 1.0;
   return *this;
   //#]
};

オーバーロードされた + 演算子用に生成されたコード

Complex Complex::operator+(Complex&  c) {
   NOTIFY_OPERATION(operator+, operator+(Complex&), 1,
      OM_operator_1_SERIALIZE);
   //#[ operation operator+(Complex&)
   return Complex(real + c.real, imag + c.imag);
   //#]
};

オーバーロードされた代入演算子用に生成されたコード

Complex& Complex::operator=(Complex&  c) {
   NOTIFY_OPERATION(operator=, operator=(Complex&), 1,
      OM_operator_2_SERIALIZE);
   //#[ operation operator=(Complex&)
   real = c.real;
   imag = c.imag;
   return *this;
//#]
};

ブラウザーは MainClass を一覧表示します。これは、3 つの Complex クラスをインスタンス化するコンポジットです。

以下にその属性を示します。

Complex* c1
Complex* c2
Complex* c3
Body~MainClass()         //DestructorBody 
{
   delete c1;
   delete c2;
   delete c3;
}
e()                  // Event

ストリーム出力演算子 << は、それを使用するクラスのフレンドとして宣言する必要があるグローバル関数です。以下のように定義します。

operator<<
Return type: ostream&
Arguments:   ostream& s
            Complex& c
Body: 
{
   s << "real part = " "<< c.real<< 
      "imagine part = " << c.imag << "\n" << flush;
   return s;
}

各種コンストラクターおよびオーバーロードした演算子が呼び出される様子を観察するには、以下のようにします。

  1. 「コード」 > 「構成の設定」 > 「編集」 > 「設定」タブ を選択してプロジェクトにアニメーション設定を割り当てます。
  2. DefaultConfig.exe を make して実行します。
  3. アニメーターを使用して、MainClass およびインスタンス Complex[0]:ComplexComplex[1]:Complex、および Complex[2]:Complex を含む、アニメーション化したシーケンス図 (ASD) を作成します。
  4. 「アニメーション」バーの「進む」をクリックして、コンストラクターとオーバーロードされた演算子メッセージが、 MainClass とその部分インスタンスの間で渡されるのを観察します。

    アニメーション化したシーケンス図は、以下のダイアグラムのようになります。

フィードバック