Operator << Overloading,why have to use reference

//This is a question I encountered while solving the challenge on hankerrank.com
//Here is the original question :https://www.hackerrank.com/challenges/overload-operators

//See where I comment my question .
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
  //Operator Overloading

#include<iostream>
#include<string>

using namespace std;

class Complex
{
public:
    int a,b;

    void input(string s)
    {
        int v1=0;
        string::size_type i=0;
        while(s[i]!='+')
        {
            v1=s[i]-'0';
            i++;
        }
        while(s[i]==' ' || s[i]=='+'||s[i]=='i')
        {
            i++;
        }
        int v2=0;
        while( i<s.length())
        {
            v2=s[i]-'0';
            i++;
        }
        a=v1;
        b=v2;
    }

};

Complex operator+  (Complex &x,Complex &y)
{
    Complex Ret ;
    Ret.a = x.a + y.a ;
    Ret.b = x.b + y.b ;
    return Ret;
}

ostream& operator<< (ostream& output,Complex &z)
 /* 1. Why do I have to reference the object output and 
the return ostream object ?   
2. Why have to use two parameters ,since there is already a 
return ostream object ? 
3.What if the return type can be void ? return nothing,
since I just need it print something .*/
{
    output << z.a <<"+i"<<z.b << endl;
    return output ;
}

int main()
{
    Complex x,y;
    string s1,s2;
    cin>>s1;
    cin>>s2;
    x.input(s1);
    y.input(s2);
    Complex z=x+y;
    cout << z;
}



Sample input and output :
input :
1+i2
4+i5
output :
5+i7
//This program works only when the numbers are no bigger than 10 .
Last edited on
1. Why do I have to reference the object output and the return ostream object ?

You don't want to copy the stream object so you have to pass/return it by reference.

2. Why have to use two parameters ,since there is already a return ostream object ?

The << operator takes two arguments. The first argument is the stream object and the second argument is the object that you want to output.

cout << z; is the same as calling operator<<(cout, z); If you write it like this it will compile and do exactly the same.

The return value allows you to chain more calls to << right after.

cout << z << x; is the same as operator<<(operator<<(cout, z), x); It simply passes the return value of the first << as the first arguments to the next <<.

3.What if the return type can be void ? return nothing, since I just need it print something .

If the return type is void you can't chain any more << after it. You would have to split the printing over multiple statements.

1
2
3
4
5
6
// This would not work if << had return type void.
cout << z << x; // error!

// Instead you would have to write it like this.
cout << z;
cout << x;
Last edited on
Why is the Complex & z argument non-const in your output operator? Same with operator+.
Don't you ever need to print or add const Complexes?
Topic archived. No new replies allowed.