How do I return an object from a friend function with 2 members?

Hello,

I have been tasked to modify a complex number calculator. I face an issue where I am not sure of how to return an object from a friend function that will add up each component and then be passed to a display class member function to output the contents to the screen.


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
#include <iostream>
#include <cmath>
using namespace std;
// class declaration section
class Complex
{
// friends list
friend double addrealandimag(Complex&, Complex&);

private:
    double real;
    double imag;

public:
    Complex(double = 0, double = 0); // constructor
    void display();
};

// class implementation section
Complex::Complex(double rl, double im)
{
    real = rl;
    imag = im;
}

void Complex::display()
{
    char sign = '+';

    if(imag < 0) sign = '-';

    cout << real << sign << abs(imag) << 'i';

    return;
}

// friend implementations
double addrealandimag(Complex &a, Complex &b)
{

    double real_total, imag_total;
    Complex Total;

    real_total = a.real + b.real;
    imag_total = a.imag + b.imag;

    return Total(real_total, imag_total);
}

int main()
{
    Complex a(1,1), b(1,1);

    double re_im;

    cout << "\nThe first complex number is ";

    a.display();

    cout << "\nThe second complex number is ";

    b.display();

    Complex c(addrealandimag(a,b)); // create a new Complex object

    cout << "\n\nThe sum of these two complex numbers is ";

    c.display();

    return 0;
}
Last edited on
Make the return type of addrealandimag Complex rather than double? Also, since neither of the reference parameters are expected to be modified, either take them by value or make them const correct.
Hello,

Thanks for your corrections. Here is what I came up with in the end.

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
#include <iostream>
#include <cmath>
using namespace std;
// class declaration section
class Complex
{
// friends list
friend Complex addrealandimag(Complex&, Complex&);

private:
    double real;
    double imag;

public:
    Complex(double = 0, double = 0); // constructor
    void display();
};

// class implementation section
Complex::Complex(double rl, double im)
{
    real = rl;
    imag = im;
}

void Complex::display()
{
    char sign = '+';

    if(imag < 0) sign = '-';

    cout << real << sign << abs(imag) << 'i';

    return;
}

// friend implementations
Complex addrealandimag(Complex &a, Complex &b)
{
    Complex total;

    total.real = a.real + b.real;
    total.imag = a.imag + b.imag;

    return total;
}

int main()
{
    Complex a(1,1), b(1,1);


    cout << "\nThe first complex number is ";

    a.display();

    cout << "\nThe second complex number is ";

    b.display();

    Complex c(addrealandimag(a,b)); // create a new Complex object

    cout << "\n\nThe sum of these two complex numbers is ";

    c.display();

    return 0;
}
Topic archived. No new replies allowed.