Solved.

Pages: 12
Solved.
Last edited on
Yeah, I know that's what I need to do now but I don't know where to start. I have read through these forums on the subject as well. Thanks for the links though. Is it possible if someone could give me an example within my code and maybe I can go from there?
Last edited on
I have added Parametrised and Copy constructors and operators += and +:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Complex
{
  public:
    Complex& operator+=(const Complex& rhs)
    {
        this->r += rhs.r;
        this->i += rhs.r;
        return *this;
    }

    Complex(int r_, int i_) : r(r_), i(i_) {}
    Complex(const Complex& rhs) : r(rhs.r), i(rhs.i) {}

  private:
	int r;
	int i;
};

Complex operator+(const Complex& lhs, const Complex& rhs)
{
    Complex temp(lhs);
    temp += rhs;
    return temp;
}
EDIT: no need for operator+ to be friend or member.
Last edited on
Thank you very much. So, when you supplied those constructors and operators, now would my next step would be to formulate a code on how to add/subtract/multiply/as well as - (unary) the complex numbers?
Yes, it is like that. You migt want to provide default constrctor (creating 0, 0 number). Also provide operators @ and @= in pairs, so people will not be suprised by some unexpected behavior.
Solved.
Last edited on
Multiplication is slightly harder than that. Google it or look it up at wikipedia.

Unary minus should reverse signs of both real and imaginary part of copy of *this and return said copy

Example of << operator:

1
2
3
4
5
6
7
8
//in class body:
friend std::ostream& operator<<(std::ostream&, const Complex&)

//outside:
std::ostream& operator<<(std::ostream& lhs, const Complex& rhs)
{
    return lhs << '(' << rhs.r << " + " << rhs.i << "i)";
}
Solved.
Last edited on
1) operator== should return bool, so sigature would be
bool operator==(const Complex&, const Complex&)

2) It just calling itself now. Think, how do you decide that two complex numbers are equal and implement it in code (Hint: you will need access to cimplex internal, so you should declare it friend, as with operator<<)
Solved.
Last edited on
You need to watch your cut and pasting, I think. You have a repeated mistake, e.g.

1
2
3
4
5
6
Complex& operator+=(const Complex& rhs)
    {
        this->r += rhs.r;
        this->i += rhs.r; // <= ???
        return *this;
    }


Shouldn't this/lhs and rhs be working with r and i consistently??

Andy
Last edited on
Solved.
Last edited on
Yes - for addition and subtraction, and the comparison operators

But not multiplication (as MiiNiPaa has already mentioned) or division, which are more involved.

Andy

Complex Multiplication
http://mathworld.wolfram.com/ComplexMultiplication.html

Complex Division
http://mathworld.wolfram.com/ComplexDivision.html

(follow links from these pages for more info.)
Also, you don't need both a member function and a global comparison operator. Just use the member function.

And whatever is at line 37 is not a comparison operator (it shouldn't take a std::ostream&, it should return a bool value which is the result of the comparison, ...)

Andy
Thanks for your advice, its just I'm getting more confused the more I think about it. Do you think you can tell me what lines like I need to delete and which are my member functions?

I'm guessing the +,-,etc. are my member functions and the += and -=, etc. are the global comparison operators.

Again, I haven't taken a class yet, I'm teaching myself at the moment, sorry if I sound like I don't know anything at all. It's just I've been at this example for hours . At this point, I wish I could just look at the solution code to teach myself, but the book doesn't provide solutions for even numbers and the more I read into these new codes and variables and what not, the more confused I am...
Last edited on
Your multiplication is wrong. It's basically the foil process in mathematics, just with complex numbers and i.
Last edited on
Solved.
Last edited on
1) lines 48- 50 have no effect: there is return before them. You should remove excess code.
2) You cannot have friend definition inside class, only declaration. Move operator << definition outside class as you did there http://www.cplusplus.com/forum/general/112890/#msg617097 .
3) To give operator access to class internals you can use friend declaration. Look how it was done in case of operator<< and add similar declaration adapted to your class.
Solved.
Last edited on
Pages: 12