封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作: 1. CPLX::CPLX(double, double)构造:参数为实部、虚部,用传入的参数初始化对象,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部; 2. CPLX::~CPLX()析构:产生一行以“RELEASE()”开始的输出,并输出复数的实部和虚部; 3. CPLX::print():产生一行以“PRINT()”开始的输出,并以格式“(a, bi)”的形式输出复数; ----------------------------------------------------------------------------- 你设计一个CPLX类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
时间: 2023-06-11 14:09:58 浏览: 145
下面是CPLX类的实现,包括构造函数、析构函数和print()函数的实现:
```c++
#include <iostream>
#include <iomanip>
using namespace std;
class CPLX {
private:
double real, imag;
public:
CPLX(double r, double i) : real(r), imag(i) {
cout << "CREATE():" << fixed << setprecision(2) << "(" << real << ", " << imag << ")" << endl;
}
~CPLX() {
cout << "RELEASE():" << fixed << setprecision(2) << "(" << real << ", " << imag << ")" << endl;
}
void print() const {
cout << "PRINT():(" << fixed << setprecision(2) << real << ", " << imag << ")" << endl;
}
};
```
下面是main()函数的实现:
```c++
int main() {
CPLX a(1.23, 4.56);
a.print();
return 0;
}
```
运行结果为:
```
CREATE():(1.23, 4.56)
PRINT():(1.23, 4.56)
RELEASE():(1.23, 4.56)
```
阅读全文