123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #include <iostream>
- using namespace std;
- class Complex {
- private:
- double real;
- double imag;
- public:
- Complex(double r, double i = 0) : real(r), imag(i) {}
- void add(const Complex &other) {
- real += other.real;
- imag += other.imag;
- }
- void show() const {
- cout << real;
- if (imag >= 0)
- cout << "+" << imag << "i" << endl;
- else
- cout << "-" << -imag << "i" << endl;
- }
- };
- int main() {
- Complex c1(3, 5);
- Complex c2 = 4.5;
- cout << "c1=";
- c1.show();
- cout << "c2=";
- c2.show();
- c1.add(c2);
- cout << "c1+c2=";
- c1.show();
- return 0;
- }
|