home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 4-1.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  82 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <iostream.h>
  7. #include <math.h>
  8.  
  9. class Complex {
  10. friend Imaginary;
  11. public:
  12.     Complex(double r = 0, double i = 0): rpart(r), ipart(i) { }
  13.     Complex(const Complex &c): rpart(c.rpart), ipart(c.ipart) { }
  14.     Complex& operator=(const Complex &c) {
  15.         rpart = c.rpart; ipart = c.ipart; return *this;
  16.     }
  17.     Complex operator+(const Complex &c) const {
  18.         return Complex(rpart + c.rpart, ipart + c.ipart);
  19.     }
  20.     friend Complex operator+(double d, const Complex &c) {
  21.         return c + Complex(d);
  22.     }
  23.     friend Complex operator+(int i, const Complex &c) {
  24.         return c + Complex(i);
  25.     }
  26.     Complex operator-(const Complex &c1) const {
  27.         return Complex(rpart - c1.rpart, ipart - c1.ipart);
  28.     }
  29.     friend Complex operator-(double d, const Complex &c) {
  30.         return -c + Complex(d);
  31.     }
  32.     friend Complex operator-(int i, const Complex &c) {
  33.         return -c + Complex(i);
  34.     }
  35.     Complex operator*(const Complex &c) const {
  36.         return Complex (rpart*c.rpart - ipart*c.ipart,
  37.                    rpart*c.ipart + c.rpart*ipart);
  38.     }
  39.     friend Complex operator*(double d, const Complex& c) {
  40.         return c * Complex(d);
  41.     }
  42.     friend Complex operator*(int i, const Complex& c) {
  43.         return c * Complex(i);
  44.     }
  45.     friend ostream& operator<<(ostream &o, const Complex& c) {
  46.     o << "(" << c.rpart << "," << c.ipart << ")"; return o;
  47.     }
  48.     Complex operator/(const Complex &c) const { return 0; }
  49.     operator double() {
  50.         return ::sqrt(rpart*rpart + ipart*ipart);
  51.     }
  52.     Complex operator-() const { return Complex(-rpart, -ipart); }
  53. private:
  54.     double rpart, ipart;
  55. };
  56.  
  57. class Imaginary: public Complex {
  58. public:
  59.     Imaginary(double i = 0): Complex(0, i) { }
  60.     Imaginary(const Complex &c): Complex(0, c.ipart) { }
  61.     Imaginary& operator=(const Complex &c) {
  62.         rpart = 0; ipart = c.ipart; return *this;
  63.     }
  64. };
  65.  
  66. int main() {
  67.     Complex c = 5;
  68.     Complex d = Complex(2,-7.0);
  69.     Complex e = d + c;
  70.     cout << e << " = " << d << " + " << c << endl;
  71.     d = c;
  72.     Imaginary i(10);
  73.     cout << "i = " << i << endl;
  74.     Imaginary j = e;
  75.     cout << "j = " << j << endl;
  76.     Imaginary imaginary2 = 2;
  77.     Complex f = Complex(1,2);
  78.     Complex sum = f - imaginary2;
  79.     cout << sum << " = " << f << " - " << imaginary2 << endl;
  80.     cout << "2 * Complex(1,2) = " << 2 * Complex(1,2) << endl;
  81. }
  82.