what does the return function do in this case?

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
#include <iostream>
using namespace std;

class Cvector {
public:
int x, y;
Cvector () {};
Cvector (int, int);
Cvector  operator + (Cvector);
};

Cvector::Cvector (int a, int b)
{
x =a;
y = b;
}

Cvector Cvector:: operator +(Cvector param) // is the first Cvector for defining the x/y and the second for the operator?
{
Cvector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp); // Why return (temp?)? //
}

int main ()
{
Cvector a (3, 1);
Cvector b (1, 2);
Cvector c;
c= a + b;
cout << c.x << " ," << c.y << endl;
return 0;
}



wrote the questions in comments. I understand what return does when you have something like this

1
2
3
4
5
 class Crectangle {
int x, y;
public:
Crectangle (int, int)
int area() {return (x*y)}

so maybe if theres some way you could put it into perspective for me maybe? Im mostly confused on the return temp. Thanks :)
Because temp is object of Cvector class and we must need to return Cvector type for operator overloaded function..
Topic archived. No new replies allowed.