ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#3 变量、赋值和数据类型 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#3 变量、赋值和数据类型

Variables, Assignment & Data Types

好的 在这段视频中 我们会先讲解变量的概念
All right, so in this video we are gonna be looking at the concept of the variable.
然后再讲解赋值语句 最后会讲解C++语言所支持的数据类型
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.
所以如果要用C++的话
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.
在我们的”age”程序中 “int”类型代表”整型”
So in our “age” case we gonna have to type in “int” to stand for integer, so this is one of the 
这是一种C++语言所支持的原生数据类型
data types, built-in data types that C++ supports.
变量的名字是“age”(年龄)
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,
与这个内存相关连的是它的名字”age”
and associated with that chunk is the actual name “age”.
它的类型是”int” 我们标记一下
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.
接下来”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,
就可以对它的名字”age”操作
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.
接下来可能会学习浮点型(float) 双精度浮点型(double)
So we’ll learn about maybe the float, the double
短整型(short)以及字节型(byte)
the short, the byte later on.
当然还有字符型(char)
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.
变量名字为”firstnName”
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,
这块内存的名字是”firstName”
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.
这的类型是字符串(string)
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.
先写类型(string)
It’s actually we could do “string”,
然后名字”firstName”
“firstName”.
再来赋值
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,
这儿就写个”Bob”吧
or assign a value to a variable, so we’ll set the “firstName” here just simply “Bob”.
注意到我写”Bob”时外面的双引号吗
Did you notice that whenever I wrote “Bob”, I put it inside of double quotes? This is required
这是为了表明”Bob”是个字符串
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
在C++中 字符串字面值必须要写在双引号内
that we write out in C++ we have to put it inside double quotes.
所以这里 代表的是”Bob”
So over here, I’ll represent “Bob”
被存储在这块”firstName”的内存中
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.
好了 我们来说说其它C++语言中的
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.
“int”数据类型有它所支持的数值范围
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,
这是个字符串”Bob” 要把它存储在
we have the string “Bob”, and we were able to store it into some variable
变量”name”中
out of their memory with this name called “name”.
或者是前面提到过的”first name”中
Maybe “first name”, I think what’s our first example previously.
下面是我们可能会遇到的用字符串方式表达数字
And then we could have a string representation of
比如这个”21″
a number, so we have “21” here.
因此”21″外面也有双引号
So you see that “21” has double quotes around it. I’ll store it into
我们把它存储在字符串变量memory中(不好意思 应该是movie中)
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,
这儿另外声明的是”int”类型 使用了”num”做变量名
where we have another “int” data type being declared, this time we’re using the variable name “num”,
然后赋值”21″
and we’re taking “21”
保存在整型的特定内存中”num”的指定位置上
store in an “int” particular memory location designated by the name “num”.
所以这个21和这个”21″在内存中是完全不同的
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
对这个叫做”num”的变量
so with this particular variable here called “num”.
我们可以进行数值计算
We can actually perform numerical calculations. So we
比如让”num”乘或除以一些值
could actually multiply “num” by some other value or divided by some value.
但如果我们要对”movie”进行一些
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.
好了 下一个数据类型是”double(双精度浮点型)”
Alright, so another numeric type is “double”.
来看看这个例子
We see an example here where we
先声明一个”double(双精度浮点型)”的变量再赋值165.8
declare a variable of type “double” and assign the value 165.8
放到分配给”weight”的内存中
to that particular memory location designated by weight.
所以这儿的double型赋值可以有小数位
So here with double values we can actually support numbers that have a fractional part,
也就是说可以存实数
or a representation of real numbers.
这和整型(int)不同 整型不支持带有小数
And that’s not the case with “ints”, with “ints” you cannot support fractional parts.
“int”型只能是整数
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.
名字是”letter” 还有个字符字面量
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
如果我们对这赋值”A”
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
布尔型只有两个值true(真)和false(假)
is used for storing true/false values.
我们可以写一个游戏程序 变量名叫做 “gameWon”
So we may be writing a game program in which we have a variable called “gameWon”.
在某些情况下我们可以把它赋为”true”
And we can set that particular value to “true” maybe under certain circumstances.
这样一来 “gameWon”只能存储两个值:”true”或”false”
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.
以上就是C++中所用到的一些简单的数据类型
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.

发表评论

译制信息
视频概述

简单地介绍了C++中的变量,数据类型以及赋值操作。

听录译者

GraphiCon-origamidance

翻译译者

秣子

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=jLC8_oDMFcg

相关推荐