All three of them set x to the particular a value given.
The difference is in their result types.
setx returns "*this" (the current class's object) by reference
sety returns "*this" by value, or in other words by copying it.
setz doesn't return anything.
In your example, this behavior has no purpose because it is not used at all.
But what you could do is something like this instead:
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
Test tst(5);
tst.print();
tst.setx(6).setz(100);
tst.print();
tst.sety(7).setz(200);
tst.print();
return 0;
};
|
The output is:
Now you may ask yourself, why is x = 7 on that third line, if I set it to 200?
It's because sety returns a Test object by value, which copies it. The object being assigned to in the setz(200) call is a temporary unnamed object that doesn't affect the existing tst object.