39 lines
622 B
C++
39 lines
622 B
C++
#include <iostream>
|
|
using namespace std;
|
|
class Complex {
|
|
public:
|
|
Complex() {
|
|
real = 0;
|
|
imag = 0;
|
|
}
|
|
Complex(double r) {
|
|
real = r, imag = 0;
|
|
}
|
|
Complex(double r, double i) {
|
|
real = r, imag = i;
|
|
}
|
|
operator double() {
|
|
return real + 2; // 强制转换Complex对象为其实部real并返回
|
|
}
|
|
|
|
private:
|
|
double real;
|
|
double imag;
|
|
};
|
|
|
|
int main() {
|
|
Complex A(1, 2.3), B = A;
|
|
int D;
|
|
double E = 2.1;
|
|
D = 10 - A;
|
|
cout << "D=" << D << endl;
|
|
B = Complex(D);
|
|
B = E + 1 - B;
|
|
cout << "B=" << B;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
D=7
|
|
B=-3.9
|
|
*/ |