/tags/rel-1-3-24/SWIG/Examples/ruby/operator/example.h

# · C++ Header · 36 lines · 28 code · 7 blank · 1 comment · 0 complexity · 61c61e3db6a1c5379ed98540e5b11d84 MD5 · raw file

  1. /* File : example.h */
  2. #include <math.h>
  3. class Complex {
  4. private:
  5. double rpart, ipart;
  6. public:
  7. Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
  8. Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { }
  9. Complex &operator=(const Complex &c) {
  10. rpart = c.rpart;
  11. ipart = c.ipart;
  12. return *this;
  13. }
  14. Complex operator+(const Complex &c) const {
  15. return Complex(rpart+c.rpart, ipart+c.ipart);
  16. }
  17. Complex operator-(const Complex &c) const {
  18. return Complex(rpart-c.rpart, ipart-c.ipart);
  19. }
  20. Complex operator*(const Complex &c) const {
  21. return Complex(rpart*c.rpart - ipart*c.ipart,
  22. rpart*c.ipart + c.rpart*ipart);
  23. }
  24. Complex operator-() const {
  25. return Complex(-rpart, -ipart);
  26. }
  27. double re() const { return rpart; }
  28. double im() const { return ipart; }
  29. };