PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/octave/operator/example.h

#
C++ Header | 35 lines | 31 code | 3 blank | 1 comment | 0 complexity | c2bb9614533296fa70da20cf50d10c4c MD5 | raw file
 1/* File : example.h */
 2#include <math.h>
 3
 4class ComplexVal {
 5private:
 6  double rpart, ipart;
 7public:
 8  ComplexVal(double r = 0, double i = 0) : rpart(r), ipart(i) { }
 9  ComplexVal(const ComplexVal &c) : rpart(c.rpart), ipart(c.ipart) { }
10  ComplexVal &operator=(const ComplexVal &c) {
11    rpart = c.rpart;
12    ipart = c.ipart;
13    return *this;
14  }
15  ComplexVal operator+(const ComplexVal &c) const {
16    return ComplexVal(rpart+c.rpart, ipart+c.ipart);
17  }
18  ComplexVal operator-(const ComplexVal &c) const {
19    return ComplexVal(rpart-c.rpart, ipart-c.ipart);
20  }
21  ComplexVal operator*(const ComplexVal &c) const {
22    return ComplexVal(rpart*c.rpart - ipart*c.ipart,
23		   rpart*c.ipart + c.rpart*ipart);
24  }
25  ComplexVal operator-() const {
26    return ComplexVal(-rpart, -ipart);
27  }
28  
29  double re() const { return rpart; }
30  double im() const { return ipart; }
31};
32
33ComplexVal operator*(const double &s, const ComplexVal &c) {
34  return ComplexVal(s*c.re(), s*c.im());
35}