속성:
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;
}
다양한 생성자와 오버로드된 연산자를 호출되는 대로 감시하려면 다음을 수행하십시오.
