类 Complex

属性:
   double imag;
   double real;
操作:
   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,它是对三个 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
  3. 使用动画程序来创建动画时序图 (ASD),其中包含 MainClass 和实例 Complex[0]:ComplexComplex[1]:ComplexComplex[2]:Complex
  4. 在“动画”栏上单击执行来观察构造函数和重载的运算符消息在 MainClass 及其组分实例之间传递的情况。

    动画时序图将类似于下图。

反馈