Jakub
Jakub

Reputation: 699

Problem with adding, multiplicate, divide and subtract complex numbers C++ - operator overloading

I write a program, which works on complex numbers - write them, read, do some simple operations. My teacher said that I must define many operators (eighteen). I have a problem with one operator.

    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }

When I put this operator into my code, adding, multiplicate, divide and subtract don't work. I have in my output

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 

Instead of

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(2.80,5.14) 
(-0.60,-1.14) 
(-4.41,6.85) 
(0.64,-0.00)

This is my whole code

#include <fstream>
#include <cstdlib> 
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif
using namespace std;

namespace ComplexNumbers
{
class Comp {
    double real, imag;

public:
    Comp(){
    real=0;
    imag=0;
    }
    double re(void) const
    {
        return real;
    }
    double im(void) const
    {
        return imag;
    }
    double mod(void) const
    {
        return sqrt(re()*re() + im()*im());
    }
    double arg(void) const
    {
        double faza;
        if (im() >= 0)
            faza = acos(re()/mod());
        else
            faza = 2*M_PI - acos(re()/mod());

    return faza;
    }
    const Comp conj(void) const
    {
        Comp temp;
        temp.real = re();
        temp.imag = -im();
        return temp;
    }
    ~Comp(){}
    const Comp operator+();
    const Comp operator-();
    bool operator!(void);
    const Comp& operator++()
    { 
        return *this;
    }
    const Comp operator++(int)
    { 
        Comp temp(*this); 
        operator++(); 
        return temp;  
    }
    const Comp& operator--()
    {
        return *this;
    }
    const Comp operator--(int)
    {
        Comp temp(*this); 
        operator--(); 
        return temp; 
    }
    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }
    Comp& operator-=(const Comp& x)
    {
        int value = 0;
        value -= x.real;
        value -= x.imag;
        return *this;
    } 
    Comp& operator+=(const Comp& x)
    {
        int value = 0;
        value += x.real;
        value += x.imag;
        return *this;
    }
    Comp& operator*=(const Comp& x)
    {
        int value = 0;
        value *= x.real;
        value *= x.imag;
        return *this;
    }
    Comp& operator/=(const Comp& x)
    {
        int value = 0;
        value /= x.real;
        value /= x.imag;
        return *this;
    } 
    friend const Comp operator+(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real + y.real;
        temp.imag = x.imag + y.imag;
        return temp;
    }
    friend const Comp operator-(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real - y.real;
        temp.imag = x.imag - y.imag;
        return temp;
    }
    friend const Comp operator*(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = (x.real * y.real - x.imag * y.imag);
        temp.imag = (x.real * y.imag + x.imag * y.real);
        return temp;
    }
    friend const Comp operator/(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = ((x.real * y.real) + (x.imag * y.imag))/(y.real*y.real + y.imag*y.imag);
        temp.imag = ((x.imag * y.real) - (x.real * y.imag))/(y.real*y.real + y.imag*y.imag);
        return temp;
    }
    friend bool operator==(const Comp& x, const Comp& y)
    {
        if (x.real == y.real && x.imag == y.imag)
            return 1;
        else
            return 0;
    }
    friend bool operator!=(const Comp& x, const Comp& y)
    {

        if (x.real != y.real || x.imag != y.imag)
            return 1;
        else
            return 0;
    }

    friend std::ostream& operator<<(std::ostream& wart1,  const Comp& a)
    {
        return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
    }
    friend std::istream& operator>>(std::istream& wart2, Comp& b){
        char c = '0';
        return wart2>>c>>b.real>>c>>b.imag>>c; 
    }
};
}
using namespace ComplexNumbers;
int main(int argc, char* argv[])
{ 
    ifstream read(argv[1]);
    if (!read)
        { cerr << "Open error: " << argv[1] << endl; exit(1);}
    ofstream write(argv[2]);

    if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);} 
    read.clear();
    read.seekg(0);
    Comp x1;
    read >> x1;
    write << x1;
    cout << x1;
    Comp x2;
    read >> x2;
    write << x2;
    cout << x2;
    cout << x1.mod() << endl;
    cout << x2.mod() << endl;
    cout << x1.arg() << endl;
    cout << x2.arg() << endl;
    cout << x1.conj();
    cout << x2.conj();
    write << x2;
    write << x1.mod() << endl;
    write << x2.mod() << endl;
    write << x1.arg() << endl;
    write << x2.arg() << endl;
    write << x1.conj();
    write << x2.conj();
    Comp sum;
    sum = x1 + x2;
    cout << sum;
    write << sum;
    Comp sub;
    sub = x1 - x2;
    cout << sub;
    write << sub;
    Comp mult;
    mult = x1 * x2;
    cout << mult;
    write << mult;
    Comp div;
    div = x1 / x2;
    cout << div;
    write << div;

    return 0;
}  

Upvotes: 0

Views: 1343

Answers (1)

catnip
catnip

Reputation: 25388

Your copy assignment operator should assign to the object itself, like this:

Comp &operator=(const Comp x)
{
    real = x.real;
    imag = x.imag;
    return *this;
}

Please note also the change in function signature

A number of your other operators (operator-=, operator+=, operator*= and operator/=) suffer from the same problem, and you should also supply a copy constructor ('rule of 3').

Upvotes: 2

Related Questions