home *** CD-ROM | disk | FTP | other *** search
- // Program demonstrates array of instances
-
- #include <iostream.h>
-
- class Complex
- {
- protected:
- double x;
- double y;
-
- public:
- Complex(double real = 0, double imag = 0)
- { assign(real, imag); }
- Complex(Complex& c);
- void assign(double real = 0, double imag = 0);
- double getReal() const { return x; }
- double getImag() const { return y; }
- Complex& operator =(Complex& c);
- Complex& operator +=(Complex& c);
- friend Complex operator +(Complex& c1, Complex& c2);
- friend ostream& operator <<(ostream& os, Complex& c);
- };
-
- Complex::Complex(Complex& c)
- {
- x = c.x;
- y = c.y;
- }
-
- void Complex::assign(double real, double imag)
- {
- x = real;
- y = imag;
- }
-
- Complex& Complex::operator =(Complex& c)
- {
- x = c.x;
- y = c.y;
- return *this;
- }
-
- Complex& Complex::operator +=(Complex& c)
- {
- x += c.x;
- y += c.y;
- return *this;
- }
-
- Complex operator +(Complex& c1, Complex& c2)
- {
- Complex result(c1);
-
- result.x += c2.x;
- result.y += c2.y;
- return result;
- }
-
- ostream& operator <<(ostream& os, Complex& c)
- {
- os << "(" << c.x << " + i" << c.y << ")";
- return os;
- }
-
- main()
- {
-
- Complex c[5];
- c[1].assign(3, 5);
- c[2].assign(7, 5);
- c[4].assign(2, 3);
-
- c[3] = c[1] + c[2];
- cout << c[1] << " + " << c[2] << " = " << c[3] << "\n";
- cout << c[3] << " + " << c[4] << " = ";
- c[3] += c[4];
- cout << c[3] << "\n";
- return 0;
- }
-
-