/* A class used to model Complex Numbers * Anthony Larrain * Spring 2006, csc309 */ #include #include #include using namespace std; class Complex1 { private: double real; double imag; public: Complex1() : real(0.0), imag(0.0) {} Complex1(double r) : real(r), imag(0.0) {} Complex1(double r, double i) : real(r), imag(i) {} double getReal() const; double getImag() const; Complex1& setReal(double r); Complex1& setImag(double i); Complex1 plus(const Complex1 & operand2) const; string toString() const; bool equals(const Complex1 & that) const; }; int main(int argc, char *argv[]) { Complex1 c1(2.3,3.2), c2(2), c3; cout << c1.getReal() << endl; cout << c2.getReal() << endl; cout << c3.getReal() << endl; c3.setReal(2.5).setImag(4.6); cout << c3.toString() << endl; c3 = c1.plus(c2); cout << c3.toString() << endl; c1.setReal(2).setImag(3); c2.setReal(2).setImag(3); cout << c1.equals(c2) << endl; cout << c2.equals(c2) << endl; cout << c1.equals(c3) << endl; system("PAUSE"); return EXIT_SUCCESS; } double Complex1::getReal() const { return real; } double Complex1::getImag() const { return imag; } Complex1 & Complex1::setReal(double r){ real = r; return *this; } Complex1 & Complex1::setImag(double i){ imag = i; return *this; } Complex1 Complex1::plus(const Complex1 & operand2) const{ return Complex1(getReal() + operand2.getReal(), getImag() + operand2.getImag()); } string Complex1::toString() const { ostringstream str; str << real << " + " << imag << "i"; return str.str(); } bool Complex1::equals(const Complex1 & that) const { if(this == &that){ return true; } return getReal() == that.getReal() && getImag() == that.getImag(); }