1

I have so big problem. I write a program which is about complex numbers. This program read and write complex numbers, add them, etc. He said that I should read that Why should I overload a C++ operator as a global function (STL does) and what are the caveats? and

1) I must create 5 operators which are member function of class and which have one argument: +, −, !, ++, −−. Then

2) I must create 5 operators which are member function of class and which two arguments: =,+=, −=, *=, /=; Then

3) I must create 8 operators which are global friend function +, −, *, /, ==, !=, <<, >> and take two parameters. I have no problem with the last one:

    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;
        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;
    }

except this?????? What I should return here????? When I compare It should be bool yeah????

    friend const Comp operator==(const Comp& x, const Comp& y)
    {

    }
    friend const Comp operator!=(const Comp& x, const Comp& y)
    {

    }

I think that I found solution


    friend bool operator==(const Comp& x, const Comp& y)
    {
            return (x.real == y.real && x.imag == y.imag);
    }
    friend bool operator!=(const Comp& x, const Comp& y)
    {
            return (x.real != y.real && x.imag != y.imag);
    }

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;


class Comp {
    double real, imag;

public:
    Comp(){
    real;
    imag;
    }
    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;
        -im();
        return temp;
    }
    ~Comp(){}
    /*
    Comp operator+(const Comp& x);
    Comp operator-(const Comp& x);
    bool operator!(void);
    const Comp& operator++();
    const Comp operator++(int);
    const Comp& operator--();
    const Comp operator--(int);
    Comp operator=(const Comp x);
    Comp operator-=(const Comp& x);
    Comp operator+=(const Comp& x);
    Comp operator*=(const Comp& x);
    Comp operator/=(const Comp& x);
    */
    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;
        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;
        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 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;
        return wart2>>c>>b.real>>c>>b.imag>>c; 
    }
};

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;
}  

Jakub
  • 679
  • 5
  • 16
  • Please only ask one question to keep focus. – Yunnosch Jun 14 '20 at 20:11
  • Hmm so I will edit my question – Jakub Jun 14 '20 at 20:14
  • Pop quiz: what value do you expect `a == b`, or `a != b` to produce? No matter what `a` and `b` is? They could be `std::string`s. They could be anything else. Do you expect the result of the comparison to be another value, of the same type? Of course not, that would be quite silly. The result of the comparison is either true or false, does that make sense to you? Now, search the deepest cavities of your mind, asking: what in C++ represents `true` or `false`? Why, it's `bool`, of course! So, ask yourself again the same question: what should your overloaded `==` and `!=` operators return, then? – Sam Varshavchik Jun 14 '20 at 20:33
  • 1 is for true and 0 is false. So I should use if? ```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; }``` ???????????? – Jakub Jun 14 '20 at 20:39
  • Did you notice that showing code in comments does not work? Please edit code into your question. – Yunnosch Jun 14 '20 at 21:06
  • @SamVarshavchik Please rephrase with the goal of being helpful and kind in mind. That comment is borderline flaggable. – Yunnosch Jun 14 '20 at 21:08
  • Mark you still need to focus your question even more on one single problem you want help with. I propose to skip the whole "Why so many?" part. That for you to discuss with your teacher. Because it is definitly possible with fewer. Otherwise "many" is just a question of working one by one with patience. But that is not a question to be discussed here, just to be helped with - one by one. – Yunnosch Jun 14 '20 at 21:11

1 Answers1

1

If you implement operator== correctly (which you did in your block titled "I think that I found solution"), then you can leverage it for operator!=:

friend bool operator!=(const Comp& x, const Comp& y)
{
    return !(x == y);
}

Your version was incorrect since it would report that they are not unequal if they had the same real part and a different imaginary part.


Also, in part (1) it refers to unary + and - (not binary versions which we get in part 3). So your first two declarations in the commented-out block are incorrect, they should be:

Comp operator+();
Comp operator-();
M.M
  • 138,810
  • 21
  • 208
  • 365