Files
BasicsOfComputerSoftwareEng…/OOP/13Exam/3.4.cpp
2023-06-02 00:03:58 +08:00

68 lines
1.2 KiB
C++

#include <iostream>
using namespace std;
class complex {
public:
complex(double r = 0.0, double i = 0.0) {
this->r = r;
this->i = i;
cout << "Constructor!";
display();
}
complex(complex &c) : r(c.r), i(c.i) {
cout << "Copy Constructor!";
display();
};
complex operator+(complex c2);
complex operator-(complex c2);
void display();
private:
double r;
double i;
};
complex complex::operator+(complex c2) {
complex result(r + c2.r, i + c2.i);
return result;
}
complex complex::operator-(complex c2) {
complex result(r - c2.r, i - c2.i);
return result;
// return complex(r - c2.r, i - c2.i);
}
void complex::display() {
cout << "(" << r << "," << i << ")" << endl;
}
int main() {
complex c1(5, 4), c2(2, 10), c3;
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
c3 = c1 - c2;
cout << "c3=c1-c2=";
c3.display();
c3 = c1 + c2;
cout << "c3=c1+c2=";
c3.display();
return 0;
}
/*
Constructor!(5,4)
Constructor!(2,10)
Constructor!(0,0)
c1=(5,4)
c2=(2,10)
Copy Constructor!(2,10)
Constructor!(3,-6)
c3=c1-c2=(3,-6)
Copy Constructor!(2,10)
Constructor!(7,14)
c3=c1+c2=(7,14)
*/