Can't wrap my head around operator overloading

I've been provided a driver function that is supposed to demonstrate the results of operator overloading involving complex numbers. After reading up on overloading a while I managed to write the code in a way that it successfully compiles, however somewhere along the way the correct values are not being output by the program. Are there any glaring errors I should be aware of?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class Complex {

private:
    double realPart;
    double imaginaryPart;

public:
    // friends
    friend ostream & operator<<(ostream &out, const Complex &c);
    friend istream & operator>>(istream &in,  Complex &c);

    // constructors
    Complex()
    {
        realPart = 0;
        imaginaryPart = 0;
    }

    Complex(double real)
    {
        realPart = real;
        imaginaryPart = 0;
    }

    Complex(double real, double imaginary)
    {
        realPart = real;
        imaginaryPart = imaginary;
    }
    // end of constructors

    // + overloading
    Complex operator+(Complex const &c)
    {
        Complex Add;
        Add.realPart = realPart + c.realPart;
        Add.imaginaryPart = imaginaryPart + c.imaginaryPart;
        return Add;
    }

    // - overloading
    Complex operator-(Complex const &c)
    {
        Complex Subtract;
        Subtract.realPart = realPart - c.realPart;
        Subtract.imaginaryPart = imaginaryPart - c.imaginaryPart;
        return Subtract;
    }

    // * overloading
    Complex operator*(Complex const &c)
    {
        Complex Multiply;
        Multiply.realPart = (realPart * c.realPart) - (imaginaryPart * c.imaginaryPart);
        Multiply.imaginaryPart = (realPart * c.imaginaryPart) - (imaginaryPart * c.realPart);
        return Multiply;
    }

    // = overloading
    Complex operator=(Complex const &c)
    {
        Complex Assignment;
        Assignment.realPart = realPart;
        Assignment.imaginaryPart = imaginaryPart;
        return Assignment;
    }

    // == overloading
    bool operator==(Complex const &c)
    {
        Complex Compare;
        if (Compare.realPart == realPart && Compare.imaginaryPart == imaginaryPart)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // != overloading
    bool operator!=(Complex const &c)
    {
        Complex NotEqual;
        if (NotEqual.realPart == realPart && NotEqual.imaginaryPart == imaginaryPart)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

};

// << overloading
ostream& operator<<(ostream& out, const Complex &c)
{
    out << c.realPart;
    if (c.imaginaryPart >= 0)
    {
        out << " + " << c.imaginaryPart << "i" << endl;
    }
    else
    {
        out << " - " << fabs (c.imaginaryPart) << "i" << endl;
    }

    return out;
}

// >> overloading
istream& operator>>(istream &in, Complex &c)
{
    in >> c.realPart;
    in >> c.imaginaryPart;
    return in;
}

// driver program
int main()
{
    for (double i = 1; i < 10; ++ i)
    {
        Complex y{i * 2.7, i + 3.2};
        Complex z{i * 6, i + 8.3};

        Complex x;
        Complex k;

        std::cout << "Enter a complex number in the form: (a, b)\n? ";
        std::cin >> k; // demonstrating overloaded >>
        std::cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: " << k << '\n'; // demonstrating overloaded <<

        x = y + z; // demonstrating overloaded + and =
        std::cout << "\nx = y + z:\n" << x << " = " << y << " + " << z << '\n';
        x = y - z; // demonstrating overloaded - and =
        std::cout << "\nx = y - z:\n" << x << " = " << y << " - " << z << '\n';
        x = y * z; // demonstrating overloaded * and =
        std::cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n";

        if (x != k)
        { // demonstrating overloaded !=
            std::cout << x << " != " << k << '\n';
        }

        std::cout << '\n';
        x = k;

        if (x == k)
        {
            // demonstrating overloaded ==
            std::cout << x << " == " << k << '\n';
        }

        std::cout << std::endl;
    }
}
Last edited on
If it helps, here's what happens inputting "3 5" in the console. As you can see it's the variable x that doesn't seem to want to work, leading me to believe it could be an issue with the assignment operator overloading or something.


x: 0 + 0i

y: 2.7 + 4.2i

z: 6 + 9.3i

k: 3 + 5i


x = y + z:
0 + 0i
 = 2.7 + 4.2i
 + 6 + 9.3i


x = y - z:
0 + 0i
 = 2.7 + 4.2i
 - 6 + 9.3i


x = y * z:
0 + 0i
 = 2.7 + 4.2i
 * 6 + 9.3i



0 + 0i
 == 3 + 5i
I get the following warning on line 60, 69 and 83.

GCC wrote:
warning: unused parameter ā€˜cā€™

For your copy assignment:
1
2
3
4
5
6
    Complex &operator=(Complex const &c)
    {
        realPart = c.realPart;
        imaginaryPart = c.imaginaryPart;
        return *this;
    }


Similarly for the other two routines that @Peter87 flagged. You are ignoring the argument c and instead comparing with a new complex number constructed as 0+0i.

Line 55 is also mathematically wrong.
Last edited on
Topic archived. No new replies allowed.