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