好的 在这段视频中 我们会先讲解变量的概念
All right, so in this video we are gonna be looking at the concept of the variable.
Also looking at the assignment statements, and finally various of data types that supported by C++.
And in the next video we’ll look at using some of these concepts in constructing a new program.
Okay, so oftentimes when we write a program we’re interested in being able to store some particular data value.
And then make use of that particular data later on.
And the way that we can go about doing that is introducing this concept of a variable.
A variable is just simply a named location in memory.
And the reason why we want to have a named location in memory as opposed to an unnamed location
is that we gonna be able to quickly and easily retrieve
会更容易 更快的 被检索到
some particular value that we’ve stored at a memory location.
If we did not have a name for that location then we would be relying upon the actual address and we have to keep track of some
sequence of numbers, sequence of digits of the address value in order to reference that
particular value that we’ve stored. But we don’t have to do that.
So again a variable is just simply a named location of memory.
So we may write a program in which we ask a user for their age.
And so the way we can do that in C++
is that we have to specify both the type of the variable we gonna have,
and also the name we associated with the variable.
So in our “age” case we gonna have to type in “int” to stand for integer, so this is one of the
data types, built-in data types that C++ supports.
And the name would be “age”.
后面再添加一个分号 好的 这就是一个声明语句的示例了
We’ll put a semicolon there, so this is just a simple declaration statement here where we’ve specified a type
in a name for a variable. So what happens is
we get a space or a chunk of memory allocated to us,
and associated with that chunk is the actual name “age”.
And then the type is gonna be “int” so we’ve got a type here.
And we have the name here, this is just a logical view of what’s going on.
And we can also assign a value to this particular memory location.
It’s actually gonna have some value in here whenever we first do this declaration.
But in reality, you want to assign a value straight away after you make a declaration.
所以良好的编程习惯是立刻给变量赋值 也就是age 等于 18
So it’s just a good programming practice to assign a value straight away so we’ll say “age” and we put 18.
So what exactly happens there is we end up with the value of 18
stored at that particular memory location.
So anytime that we wanna be able to get access and make use of this value of 18,
we can do it to its name “age”.
And there’re other data types other than just the integer data type, there’re other numeric types.
So we’ll learn about maybe the float, the double
the short, the byte later on.
But there’s also types support characters,
and the sequence of characters, so maybe we’re interested in getting someone’s first name or their last name.
Then we could introduce a variable of type string, so a string supports
a sequence of characters, so let’s go down here and actually
declare another variable, and we’ll say that it’s of type string.
And we’ll call the name of this variable just simply “firstName”.
So we’ve declared a new variable so we get a chunk of memory allocated for us,
and associated with that, is we have “firstName”.
Okay, so this is just simply the name of the variable, some people will refer to this as the identifier,
but identifier is basically a fancy word for name.
And the type here is gonna be “string”.
And the way we have this now is that we’ve simply declared the variable but we haven’t
initialized as anything, so there’ll be some sort of garbage here whenever we do this declaration.
You can actually do a declaration and initialization all in one line, so instead of doing what we just did, I’m gonna mark this out.
It’s actually we could do “string”,
And then do the assignment operation as well.
So we were assigning a value straight away, we don’t have to worry about this garbage.
This is good program practice whenever we declare a variable to assign it to a value,
or assign a value to a variable, so we’ll set the “firstName” here just simply “Bob”.
Did you notice that whenever I wrote “Bob”, I put it inside of double quotes? This is required
in order to represent the “Bob” is in fact a string.
So in each string that you’re gonna be writing out what we call a string literal
that we write out in C++ we have to put it inside double quotes.
So over here, I’ll represent “Bob”
as being stored at this memory location that has “firstName”.
That’s the basic idea behind variables and assignment statements: a variable is just simply a named location in memory,
and we can assign values to that particular named location in memory,
via the assignment operator, so this is in fact an assignment statement.
In which we are declaring a variable and assigning a value using the assignment operator.
Here we declared a variable,
select a value that we want to store there,
and then using assignment operator we’re storing at the memory location.
That’s basically it, so variables and assignment statements in a nutshell.
Alright, so let’s talk about some of the other types
数据类型 当我说数据类型或用术语”data types”时
that are supported by C++. And so when I say “data types” or use this term “data types”,
we are saying that in memory,
how much space is going to be allocated, and then what types of data can we actually stored there.
Can we store numeric values? can we store sequence of characters?
Can we store a single character? All this comes down to the data type or the type of data.
前面 我们简单地提到过”int(整型)” 也知道
Alright, so we mentioned briefly about the “int” and we know that “int”
data types can store integer values, so there are numbers will not have a fractional part.
And the “int” data type also has a specific range of values that it can support.
But I’m not gonna get into the actual range just yet.
We may look at that on another day, but,
in terms of the amount of space that is actually allocated on a lot systems that will be
four bytes. Now it depends on the underlying architecture processor,
and also what compiler in terms of how much space is actually allocated.
We also looked at the string data type.
So we saw maybe an example here,
we have the string “Bob”, and we were able to store it into some variable
out of their memory with this name called “name”.
Maybe “first name”, I think what’s our first example previously.
And then we could have a string representation of
a number, so we have “21” here.
So you see that “21” has double quotes around it. I’ll store it into
a string variable in this case called memory(“movie”, excuse me).
So it maybe the title of some particular movie,
so always with our string values.
The string values will be surrounded by double quotes.
Just to contrast that we see here is in the next example,
where we have another “int” data type being declared, this time we’re using the variable name “num”,
and we’re taking “21”
store in an “int” particular memory location designated by the name “num”.
So this guy and this guy are two very different animals out there in memory.
In terms of the amount of space that’s actually gonna be allocated,
and also in terms of the actual operations that we can perform
so with this particular variable here called “num”.
We can actually perform numerical calculations. So we
could actually multiply “num” by some other value or divided by some value.
If we try to do the same sort of operations numerical operations with
相同的数值计算 就会出错 因为这是个字符串
“movie” here, it wouldn’t work because this is a string representation.
So to us there may look very similar, but in terms of
the way this is represented behind the scenes and the type of operations we can do,
they are very different animals.I don’t have a mention over here
for the actual space that’s going to be allocated for strings.
This varies according to not only the processor and compiler but also the size of the string.
So you can imagine that a very long string would require
more memory than a very short string.
Alright, so another numeric type is “double”.
We see an example here where we
declare a variable of type “double” and assign the value 165.8
to that particular memory location designated by weight.
So here with double values we can actually support numbers that have a fractional part,
or a representation of real numbers.
And that’s not the case with “ints”, with “ints” you cannot support fractional parts.
It is the name “int” means we’re talking about the counting numbers,
both in the positive and negative direction and zero as well.
Alright, so in a lot of systems, a double will be 8 bytes.
That varies again.
Next we’ll look at character representation, so if we’re interested in representing just a single character
in our program then we can use a character variable.
So we have a typing here.
The name here just being “letter”, and here we actually have a
character literal. So character literals are surrounded by single quotes,
whereas string literals are surrounded by double quotes.
And so we have single quotes there.
So we could not go in here and assign any
representation has double quotes to a character variable. So if we were to come here and
type out double quote “A” double quote and try to assign it here,
the compiler would complain about that because that is not
the types of values that characters support.
And it usually has a single byte allocated for a character.
And then finally we have the boolean data type, and the boolean data type
is used for storing true/false values.
我们可以写一个游戏程序 变量名叫做 “gameWon”
So we may be writing a game program in which we have a variable called “gameWon”.
And we can set that particular value to “true” maybe under certain circumstances.
So, in this case, only two different values can be stored in “gameWon”: “true” or “false”.
And typically you have a byte allocated in memory for
the boolean data type.
So again this is just a brief listing of some of the other data types that c++ uses.
And we’ll be using some of these in our future programs.
好的 在这段视频中 我们会先讲解变量的概念