Add two complex numbers

Hi,

I am beginner in c++, and i need help.
Thank you.

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
#include <iostream>

using namespace std;

class Complex
{
    private:
        int a;
        int b;
    public:
        Complex();
        Complex(int x, int y, char *str);
        Complex Suma(Complex z);
        ~Complex();
};

Complex::Complex()
{
    a = 0;
    b = 0;
}

Complex::Complex(int x, int y, char *str)
{
    cout << "\n Give the " << str <<endl;
    cout << "\n\t Give the real part : ";
    cin >> x;
    a = x;
    cout << "\n\t Give the imaginary part : ";
    cin >> y;
    b = y;
}

Complex::Complex Suma(Complex z)
{
    Complex Z;
    Z.a = a + z.a;
    Z.b = b + z.b;
    return Z;
}

int main()
{
    int x, y;
    Complex R(), R(x, y, "first number"), R(x, y, "second number");
    return 0;
}  
Please always specify what your problem is. What do you want from us?

Complex::Complex Suma(Complex z)

This is not how it works. Change it to - Complex Complex::Suma(Complex z)
Last edited on
look for the changes
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
#include <iostream>

using namespace std;

class Complex
{
    private:
        int a;
        int b;
    public:
        Complex();
        Complex(int x, int y, const char *str);
        Complex Suma(Complex z);
        ~Complex();
};

Complex::Complex()
{
    a = 0;
    b = 0;
}

Complex::Complex(int x, int y, const char *str)
{
    cout << "\n Give the " << str <<endl;
    cout << "\n\t Give the real part : ";
    cin >> x;
    a = x;
    cout << "\n\t Give the imaginary part : ";
    cin >> y;
    b = y;
}
Complex::~Complex()
{}
Complex Complex::Suma(Complex z)
{
    Complex Z;
    Z.a = a + z.a;
    Z.b = b + z.b;
    return Z;
}

int main()
{
    int x, y;
    Complex R(), R1(x, y, "first number"), R2(x, y, "second number");
    return 0;
} 
Also gotta initialize x and y first - int x = 0, y = 0;
TarikNeaj i will be more precisely next time.
Thank you guys!
Topic archived. No new replies allowed.