I am still confused as to when these setter functions take effect. |
They take effect when you call them. Keep in mind that in a C++ program, execution goes from one step to the next to the next in a (mostly) defined order. It isn't like a spreadsheet where you put in a bunch of formulas and the spreadsheet figures out how to evaluate them until everything is consistent.
In this example, the setters aren't called but you can still look at them to understand them. The key is to realize that the class is storing a whole lot of
redundant data. All it really needs to define the rectangle are
left
,
top
,
bottom
and
right
. It stores the 4 private points so it can quickly return them in getUpperRight(), getUpperLeft() etc. The alternative would be to construct new points at runtime from the 4 edges. For example getUpperLeft() could be implemented as
1 2 3 4 5 6
|
getUpperLeft() {
Point result;
result.setX(left);
result.setY(top);
return result;
}
|
But again, the point here is that there is redundant data in the class.
So what should Rectangle::setUpperLeft() do? It must change the upperLeft point member, but that would leave the top and left members with the old values. So those must change too. And if you change the upper left corner, then the lower left corner must move also, so you change that as well.
By the way, this is an interesting example, but you should avoid storing redundant data in practice. The problem is that it's too easy to forget to update all data everywhere you need to, so it's a recipe for bugs. Do it if you really have to (usually for performance) but be careful when you do.