- There's a few very common operators
that you're probably already familiar with and
for completeness sake, let's go over them very quickly.
This is working.cpp from chapter four of the exercise files.
I'm going to come down here and declare a couple of variables,
X and Y, and we'll go ahead and print their values.
I'm going to build and run you see we're
displaying those values.
So here you'll notice I've used an assignment operator
that's the equal sign here is the assignment operator
to initialize these variables
and what the assignment operator actually does
is it copies, it's really a copy operator.
And so it's copying the right-hand side
into the left-hand side.
And so it evaluates right to left
and it's copying this literal value 47 into X.
So it makes a copy of it in the variable X
and the same thing happens here.
So if I now were to say X equals Y and build and run
now you notice that they both have the value of five,
I'm actually taking a copy of Y and putting it in X
and that's what the copy operator does.
The assignment operator
is really a copy operator in that way.
So there's also arithmetic operators.
So I can say, int Z equals X plus Y
and I'll go ahead and I'll make another print here for Z.
And when I build and run you see that the value is 52.
That's the sum of these two values.
So the addition operator
it adds the right-hand side to the left-hand side
and then it returns what's called a temporary object.
And so it doesn't actually change the value of X
nor does it change the value of Y it creates a new value,
which is anonymous and temporary
and then the assignment operator here makes a copy of that
temporary value, copies it into Z
and then when the expression is done
the temporary object is released.
And so in very technical terms that's how addition works.
That's how all of the four common arithmetic functions work.
So we have addition, we have subtraction.
So X minus Y is 42.
We have multiplication with the asterisk.
And we have division and this is integer division,
so it discards any remainder
and you can get the remainder with the modulus operator,
which is a percent sign
and so the remainder you can see is two.
And there's two more common operators,
there's the unary minus.
So if I come here and I
put a minus sign in front of that Z you'll see that we get
the negative of the value in the result it negates it.
And likewise the unary plus operator
returns the positive of it
which essentially does nothing
but it's there for completeness.
We use these common operators so much
that it's easy to take them for granted.
It's good to take a moment and think about how they work.