Untitled
unknown
plain_text
2 years ago
1.3 kB
1
Indexable
Never
#include <iostream> using namespace std; class ComplexNumber{ private: double real; double imaginary; public: ComplexNumber(const double r = 0, const double i = 0); ComplexNumber operator+(ComplexNumber other); friend ComplexNumber operator-(ComplexNumber first, ComplexNumber second); void print() { cout << real << " + " << imaginary << "i" << endl; } }; ComplexNumber operator-(ComplexNumber first, ComplexNumber second); ComplexNumber operator-(ComplexNumber first, ComplexNumber second) { ComplexNumber temp; temp.real = first.real - second.real; temp.imaginary = first.imaginary - second.imaginary; return temp; } ComplexNumber::ComplexNumber(const double r, const double i) : real(r), imaginary(i) { } ComplexNumber ComplexNumber::operator+(ComplexNumber other) { real += other.real; imaginary += other.imaginary; return *this; } int main() { ComplexNumber test1(2.3, 4.4); test1.print(); ComplexNumber test2(1.1, 2.2); test2.print(); (test1 + test2).print(); cout << endl; ComplexNumber test3(2.3, 4.4); test1.print(); ComplexNumber test4(1.1, 2.2); test2.print(); (test1 - test2).print(); }