2

Like the title said, i'm trying to implement an operator^(int n) which will calculate a complex number to the nth power. I know that this is a pointer that point to the current class object so i came up with this code:

    class Complex{
    protected:
      float a,b;
    public:
      Complex() {a=0;b=0;}
      Complex(float x, float y){a=x;b=y;}
      void set(float x, float y){a=x;b=y;}
      Complex operator*(Complex C){
                Complex temp;
                temp.a=a*C.a-b*C.b;
                temp.b=a*C.b+b*C.a;
                return temp;
      }
      Complex operator^(int n){
                Complex ONE=Complex(1,0);
                if (n<=0) return ONE;
                return ((*this)*((*this)^(n-1)));
      }
      void Display(){
                cout<<a<<' '<<b<<endl;
      }
      };
      int main() {
          Complex C;
          C.set(2,0);
          C=C^3;
          C.Display();
      }

The C.Display() is supposed to print 8 0 but when i ran in eclipse it display 2 0. Please tell me why this happens. Also really appreciate if anyone could tell me how to make ONE at line 15 a constant class object like BigInteger.ONE in Java.

Derek W
  • 9,708
  • 5
  • 58
  • 67
minhnhat93
  • 141
  • 1
  • 2
  • 13
  • 10
    Even though it's tempting – please don't use `^` as a power operator in C++, it has the wrong precedence. – leftaroundabout May 21 '12 at 15:30
  • 2
    What's wrong with `std::complex` and `std::pow`? –  May 21 '12 at 15:33
  • @leftaroundabout sorry but i couldn't find any operator better than ^ for power. – minhnhat93 May 21 '12 at 15:34
  • 4
    @minhnhat93: So use a named function, instead of an operator. No C++ (or C#, or Java) programmer expects power to be an operator. – Ben Voigt May 21 '12 at 15:36
  • @Fanael This is my homework for Class and Struct lecture and it says i have to implement an operator for the power function – minhnhat93 May 21 '12 at 15:38
  • 1
    @minhnhat93, then you should change place where you are taking lectures; no sane lecturer will tell you to use `operator^()` as power in C++. – Griwes May 21 '12 at 15:43
  • 2
    @Griwes: Changing course might not be an option, but he should do it the right way (a function named `pow`) and be prepared to defend that by pointing out that the C++ Standard Committee, who are no fools, decided that was the better approach. – Ben Voigt May 21 '12 at 15:49
  • @BenVoigt, true, but he doesn't seem interested in sane solutions. – Griwes May 21 '12 at 15:51
  • please go easy on me. this is my homework, what else can I do? and nobody's answering my question? – minhnhat93 May 21 '12 at 15:56
  • Perhaps the answers are a bit vague because we cannot find any errors. The code works fine for me... – Bo Persson May 21 '12 at 16:02
  • 1
    @minhnhat93: Are you sure this is the code that prints `2 0`? It prints `8 0` as expected for me: see http://ideone.com/MatpM (Also, please ignore anything your lecturer tries to teach you about C++, and read [a good book](http://stackoverflow.com/questions/388242) instead). – Mike Seymour May 21 '12 at 16:09
  • @MikeSeymour this is strange, it still show `2 0` on my computer, perhap this is because of the compiler. it seems that the problem has been solved. thank you all for the help :)), love you all. and i'll find my new c++ book too :D – minhnhat93 May 21 '12 at 16:19

2 Answers2

3

Are you aware that there is an std::complex template type, with its own std::pow specialization?

#include <complex>
#include <iostream>

int main() {

  std::complex<double> c(2,0);
  std::complex<double> c3 = pow(c, 3);
  std::cout << c3 << "\n";
}

produces

(8,0)

furthermore, operator^ is bitwise XOR. Reusing this as a power operator will result in very confusing code.

Other than that, your code produces the result you expect, so the problem must lie elsewhere.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Also really appreciate if anyone could tell me how to make ONE at line 15 a constant class object like BigInteger.ONE in Java.

Put this inside your Complex declaration:

class Complex {
   ...
   const static Complex ONE;
};

And, put this outside your Complex declaration:

const Complex Complex::ONE(1,0);

If you put your Complex declaration inside a header file (e.g., Complex.hpp), you should the 2nd line (the Complex::ONE definition) inside exactly one source-code file (e.g. Complex.cpp).

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • This kills optimization though... therefore not recommended. Better is `class Complex { /*...*/ public: static Complex ONE() { return Complex(1,0); } }`; which will be inlined and optimized away. – Ben Voigt May 21 '12 at 15:45
  • thanks Rob :D but can you please tell me why we need to write `const static` and not `const` only? – minhnhat93 May 21 '12 at 15:48
  • The `static` keyword in this context specifies a *static data member*. A static data member is not part of any object, but exists once for all objects of that class. This is similar to what Java calls a *class variable*. If that is not the behavior you asked for, disregard my answer. – Robᵩ May 21 '12 at 16:16
  • it worked perfectly, i asked because i want to understand things clearly. thank you! – minhnhat93 May 21 '12 at 16:22