클래스 복합체

속성:
   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;
//#]
};

브라우저는 세 개의 Complex 클래스를 인스턴스화하는 컴포지트인 MainClass를 나열합니다.

해당 속성은 다음과 같습니다.

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. 애니메이터를 사용하여, MainClass와 인스턴스 Complex[0]:Complex, Complex[1]:ComplexComplex[2]:Complex를 포함하는 애니메이션 시퀀스 다이어그램(ASD)을 작성하십시오.
  4. 애니메이션 막대에서 이동을 클릭하여 MainClass와 해당 파트 인스턴스 사이에 전달되는 생성자와 오버로드된 연산자 메시지를 감시하십시오.

    애니메이션 시퀀스 다이어그램은 다음 그림과 유사합니다.

피드백