Nearly all programming languages have the ability to change the value
of a variable.
In c++, we can change the value stored in a variable,
using the assignment operator.
The assignment operator is a single equal sign.
It's important to understand that this does not represent equality.
In the code sample on this page,
we are not asserting that the left-hand side is equal to the right-hand side
nor are we comparing the left-hand side in the right-hand side.
In this case, we're evaluating the value of the expression on the right-hand side
and storing that value into the variable represented by the left-hand side.
C++ is statically typed.
That means that the compiler will be checking to see if it makes sense
to store the value from the right-hand side to the left-hand side.
If it doesn't make sense, you'll get a compiler error saying exactly that.
In order to store value into the left-hand side.
The left-hand side must be a signable. It can't be a literal.
It can't be a constant.
If it is, the compiler will produce an error message.
We can assign multiple variables all at once in a single assignment statement.
This is possible because the assignment expression
evaluates to what was just assigned.
So it's easy to chain assignment statements together.
Let's head over to the IDE will work with the assignment operator in life code.
Okay. So now I'm in the CodeLite IDE.
I'm in the section 8 workspace, and the project is called AssignmentOperator.
Who got you some examples of how to use the assignment operator
in the effects of the assignment operator in this video.
So what I've d1 here is that created
2 variables, num1 and num2.
I'm declaring and initializing them to 10 and 20, respectively.
And then I just got a simple output statement that prints out num1 and num2.
Right now this is not a assignment.
What you what you see up here right now and let me just mark that up,
what you see here is not assignment, it's initialization.
It's important understand the difference.
Initialization happens when the variable is declared,
and it gets that value for the very first time.
Assignment is when you change a value that already exists in the variable.
Okay. So let's try to change 1 of those values.
I'm going to type in here. Just something really simple like num 1
= 100.
You notice I just said num1 = 100.
This is 1 of the ways that programmers read this, num1 = 100,
assigned 100 to num1, move 100 to num1.
You here this sort of jargon, butt off and you'll hear num1 = 100.
And what we mean there is, it's an assignment statement. We're not saying that num1
is actually equivalent to 100.
This is an assignment statement. Num1 in this case is 10, remember.
So what we're doing is removing a 100 into that value.
Okay. So let's break this down
and go through this 1 step at a time. So you really see what's going on here.
What we're doing is again, we have a left-hand side
on assignment operator and the right-hand side, right?
That's an assignment expression. We put a semicolon in the end.Now it's an assignment statement.
In this case, we're evaluating the right-hand side expression.
In this case, it's a 100.
It's a literal. It's going to validate to 100.
Now here is my variable num1.
Currently it has a 10 in there. You can see that right there, I initialized to 10.
When we say something like num1
= 100 or again assigned 100 to num1
or evaluating that right-hand side in this case, as I said, it's a 100,
and we're storing that 100 into num1,
but num1 is 10.
Yes, but when you use num1, this variable num1 on the left-hand side.
We're using its location, not its value.
In c ++, there's a concept of L value
and R value,
and it gets pretty complicated when we're talking about d1 more advanced features in c++.
But at this level, it's really pretty simple.
The R-value is the con10ts of somebody bullet stay.
In this case,
the con10ts of num1 is 10. So the R-value of num1 will be 10.
The L-value is the location of that variable.
So again, what this means is
move 100 or store 100 into the location of num1.
So what's going to happen is that we just changed the 10 to a 100.
Okay. Again, conceptually is pretty straightforward thing.
So let me clear this, and we'll run it. And what we expect now is we expect num1 to be a
100 and num2 to still be 20 because we haven't messed with it at all.
Let me compile and run.
And you can see num1 is a 100 because we changed it in the assignment statement
and num2 is 20.
Okay. It's pretty simple. We don't have to use literals here.
We can use anything we like. In this case, I could say num2.
Okay. So remember, because this variable num2
is on the right-hand side of that assignment statement,
we're using its R-value or its value or its contents.
We're using the 20 basically. So we're taking the 20,
and we're signing it to the location where num1 is that box where num1 lives.
So it's going to change that guy to a 20.
Okay. Well, we'll put a 20 in num1. However, you want to look at,
and draw means the same thing. So now when we print these 2 values,
we should get 20 and 20. So let's give it a run.
So I'm building and running. And now we've got 20 and 20.
Remember, we've got a left-hand side,
the assignment operator and the right-hand side followed by semicolon.
This right-hand side could be really, really complex.
Mean, it could be some really difficult.
It could be some really complex mathematical expression
that just goes on and on and on and on, doesn't really matter.
What the compilers want to do is going to evaluate this expression to some of value.
And then it's going to assign that value that was just calculated
into that variable.
The compiler does type checking for us.
Remember, c++ is statically typed which means that
a lot of errors are caught by the compiler.
This is very different from other languages like Python and Ruby and so forth,
where they do all this type checking at runtime.
C++ does it at compile time. So when the program is running,
your guarantee that this is going to be correct because it's already done a lot of
this checking for you.
So what the compiler does now is it's going to check to see,
does it make sense for me to to assign whatever value was in here to here.
How does it know? It's looking the types.
It's saying num2 to is an integer, num1 is an integer. Okay. If I did something
like num1 = 200,
it's going to say okay. That's an integer, does it make sense to assign an integer
to an integer? Sure, it does. It's got its rules internally.
And we'll talk about mixed mode assignment and mixed mode math
in a couple of videos from now.
That's where we're adding and
multiplying integers and real numbers and assigning
potentially a real number to an integer,
it's not just int with int, right. We've got different types going working together.
And that all works really well too, but we'll talk about that in a couple of videos.
So that takes care of this. I hopefully you can see the conceptually is really really straightforward.
Let's say that we wanted to change these together.
Okay. So what we can do here as we can chain
those assignment operators together.
And again, let's break this down so you can see what's happening.
We're going to say it num1,
num2 and some value could be a big old
mathematical expression. But in this case, I'm just going to use the 1000 to make things easier.
Okay. We've got 2 assignment operators here.
What's important understand with assignment operators and examine expressions
is that they return a value themselves.
They returned what was just assigned.
And they associate right to left,
which makes sense, right. Another words, this will be the last thing done.
So the first thing the compiler does is it sees this expression right here.
And it says okay, I want to assign 1000 into num2.
Okay, lets do that. So num2 right here is going to get a 1000.
Now the value of this expression evaluates to
is 1000 because that's the number that was just assigned.
So now you've got num1
= 1000. So what we do is we assign
a 1000 to num1.
Okay. And now we're done.
So you can see that you can chain these things together and they go right to last.
It's a handy way to initialize a bunch of variables
to the same value. Again, we can have all sorts of expressions
in here and so forth if we want.
That's really not considered good practice.
The only time that you really should even consider changing his assignment operators
is when you were signing the same thing to everything.
Like num1, num2, num3, num4 4 = 10
or 0 or something like that,
that makes sense. But if you've got really complicated
expressions in these pieces here, that's probably not a good idea to do.
Okay. So let's take a look at that. And let's run this.
And you can see now that when they print out,
you get a 1000 and 1000, just like you would expect. Num1 is 1000, num2 is 1000.
Okay. Let's do an error here.
Let's say we wanted to do something like that.
Now the c++ compilers pretty smart.
And when we talked about classes and objects, we'll talk about how we can make the
assignment operator our own so it works with our own classes.
C++ is pretty smart. It's going to try
to see if it's possible to convert whatever is on the right-hand side to so the left-hand side.
In this case, you're telling the compiler
store a string Frank, write a sequence of characters
into an integer, that makes no sense.
The compiler's going to try to see if it can convert that strength when integer somehow.
And if it's got some sort of code that maybe you've written,
it'll execute that code and converted. In this case, there is no such code.
So if I build and run this, I'm going to get an error.
And the error is going to say invalid conversion from
a character pointer. We'll talk about what that is in a few videos to an integer.
So we again, it's conversion issue. It's saying I don't know.
These are apples and oranges as far as the compilers concern.
So let me clear this. And let's do one more.
And let's say we 1 was assigned 100 to num1.
We've seen that, that works fine. But what if num1 is a constant.
So let's say we made num1 a conts.
This shouldn't be allowed, right, because you just said I've got a constant ended you're here, num1,
and I initialized it to 10, and now you want to change it to a 100.
Obviously, the complier is going to say whoa, wait a minute.
You just told me that was a constant. I can't do that for you. So let's try that.
We'll run it. And you should see an error something about read only. There you go.
Error assignment of read only variables not allowed. So you can't do that.
Similarly, I can't do something like this.
100, let's say = num1,
right.
What are you trying to do here? Your compiler is thinking what do you want to do.
Okay, we know what num1 is, right. Its value is 10 because it's on the right-hand side.
But now you want to assign it to a 100. A 100 is a literal.
A 100 doesn't have an L-value. It doesn't have a location in memory.
So when you run this, you'll see something like error,
L-alue required as the left operand of an assignment.
I need to store this somewhere. I need a location
and 100 is a literal, does it have a location?
Okay. So that's it. Assignment, at this level, is pretty simple.
We're going to use it at this level for a bit.
Once we get to our own classes and objects, we can make
assignment work for us in a really,really powerful way,
and we'll talk about that when we get there.
In the next video, what we'll do is we'll talk about the mathematical operator.
So the arithmetic operators.
The plus, the minus, the multiply, the divided and the remainder.