Class function problem

Hello. I'm trying to create a function in a class Complex that assigns one complex number to another one and return the result in a complex object, and another function that compares 2 complex numbers and returns a bool value but I'm not sure how to do them. Here's my program so far:

#include<iostream>
using namespace std;
class Complex {

public:
Complex(float r = 0.0, float g = 0.0) { rel = r; img = g; };

Complex(const Complex & c) {
rel = c.rel;
img = c.img;
}

Complex assginComplex(const Complex & c) {};
bool isEqual(const Complex & c) const {};
Since your class just has 2x floats in it, the assignment operator that the language provides for you automatically is already good enough. So you can already do
1
2
3
Complex a;
Complex b;
a = b;


If you really need to define it yourself as part of the instructions, it will look something like:
1
2
3
4
5
6
7
8
9
10
11
class Complex {
    // ...
    Complex& operator=(const Complex& c)
    {
        // copy c's components into the object
        // (TODO)

        // return reference to the object we just assigned to
        return *this;
    }
};


For equality, you need to overload the == operator.
It will look something like:
1
2
3
4
5
6
7
class Complex {
    // ...
    bool operator==(const Complex& c) const
    {
        return /* something */;
    }
}
(Compare each x component and y component to see if they are both equal)
Last edited on
Topic archived. No new replies allowed.