未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#2 第一个C++程序

First C++ Program

我们先来看看如何在 Eclipse 中创建第一个简单的程序
Ok, so let’s take a look at creating our first simple programme in Eclipse.
因为 Eclipse 开发以项目为单位
Since Eclipse is project based,
所以我们写程序前要先创建项目
all the programmes that we construct have to be associated with a project.
那么我们首先创建一个 C++ 项目
So the first thing we need to do is actually create a C++ project.
点击菜单“File”下的“New”
And we could do that by going to ‘File’, ‘New’,
然后选择“C++ Project”
and then coming over here to ‘C++ Project’,
左键单击
left clicking on it.
弹出了一个对话框
And now we get a little dialogue box here,
要求我们输入“Project name”
and it wants us to specify the ‘Project name’.
我们不妨给它取名叫“FirstProject”
So the project name here is just going to be, maybe ‘FirstProject’,
因为这是我们创建的第一个项目
since this is our first project.
“Project type”选择“Empty Project”
Under ‘Project type’, select ‘Empty Project’.
“Toolchains”选择“MinGW GCC”
And under ‘Toolchains’, make sure ‘MinGW GCC’ is selected.
好了 做完之后点击“Finish”
Ok, once you’ve done that, you can click on ‘Finish’.
现在在“Project Explorer”里就能看到“FirstProject”了
And now over here under ‘Project Explorer’ we should see ‘FirstProject’.
如果你没找到“Project Explorer”的话
If you don’t see ‘Project Explorer’ here,
点击菜单“Window”
you should be able to go under ‘Window’,
鼠标移动到“Show View”
‘Show View’,
选择“Project Explorer”
and then select ‘Project Explorer’.
不过项目资源管理器应该是默认显示的
But it should be showing up by default.
现在我们能看到创建的项目了
So over here we do have our project.
其实这个“FirstProject”就是保存在硬盘工作区下的一个目录
And basically this ‘FirstProject’ is a folder on our system stored in our workspace,
而工作区的路径是你自己设定的
wherever you designated your workspace to be.
现在我们就向这个项目添加一个 C++ 源文件
So what we can do now is actually add in a C++ source file to this project.
右键单击“FirstProject”
So let’s go ahead and right click on ‘FirstProject’,
鼠标移动到“New”
go to ‘New’,
选择“Source File”
and then we can select ‘Source File’.
我们就在这儿给这个源文件起名
Ok, so this is where we’re actually going to name our ‘Source File’.
既然我们把项目叫做“FirstProject”
In our case, since we named the project ‘FirstProject’,
那么就把源文件叫做“FirstProgram”吧
let’s name the ‘Source File’, eh, ‘FirstProgram’.
我们需要在这里输入扩展名“.cpp”
We need to provide and extension here of ‘.cpp’.
“cpp”是 C++ 源文件的惯用扩展名
So ‘cpp’ is just the default naming convention for C++ source files.
“Template”这里 如果你原本选择的是“Default C++ source template”
For ‘Template’, if you have it selected to ‘Default C++ source template’,
请改为选择“None”
change that to be ‘None’,
然后点击“Finish”
and then click ‘Finish’.
好了 现在我们准备就绪 可以写程序了
Ok, so now we’re ready to start typing in our first programme.
这里的标签显示的是源文件的名字
So you should see a tab here that has the name of your source file.
如果你起的名字不是“FirstProgram.cpp”
If you named it something else other than ‘FirstProgram.cpp’,
那你在这看见的就是你起的另外的名字
then you’ll see that name appear there.
在标签下方 左键单击
So right below that, let’s just left click,
就可以输入我们的第一行代码了
and start typing our first line of code.
第一行代码是 #include 然后空格
So the first line of code is going to be ‘pound’, include, ‘space’,
左尖括号
open angle bracket,
Eclipse 会自动补全右尖括号
and Eclipse should automatically insert a close angle bracket there for you.
在尖括号之间
So in between the open angle bracket and the close angle bracket,
输入“iostream”
let’s type ‘iostream’.
那么这行代码到底是啥意思呢
So what does this line of code actually mean?
我们把“#include”叫做包含指令
Well the ‘#include’ is what we call an include directive,
它的作用是把其他人写好的一些功能代码
and it’s saying to make some set of facilities provided in some other code,
包含到程序中 让我们使用
available to us, in this programme.
这里我们包含了 iostream
In this case we’re saying, include iostream.
iostream 是 C++ 标准库的一部分
An iostream is part of the C++ standard library.
我们想从 iostream 中
And what we want to use from the iostream,
调用“从键盘获取输入”
is the facility that allows us to get input from the keyboard,
以及“把文字输出到屏幕”这两个功能
and to output text to the monitor.
接下来 按下回车键
Ok, so go ahead and hit ‘Enter’,
输入下一行代码
and we’ll type in our let…next line of code.
下一行代码是
So the next line of code is going to look like this,
“using”
‘using’,
空格
space,
“namespace”
‘namespace’,
空格 “std”
space, ‘std’,
分号
semicolon.
目前我不想对这行代码做太多解释
I don’t want to say too much about this particular line of code at this time,
你只要知道它可以帮我们节省一些代码就行了
other than it’s going to save us a few key strokes.
我们后面讲到函数 类 作用域等概念时
But we’ll revisit the concept of name spaces later,
再细讲这个 namespace 是啥意思
when we would discuss such concepts as functions, classes and scope.
好了 现在可以继续下一行代码了
Ok, so we’re ready to write our next few lines of code now.
按两次回车键
Go ahead and hit the ‘Enter’ key twice,
我们先写几行代码
and we’re going to write several lines of code,
写完再回过头来解释
and then come back and explain these lines of code.
输入“int” 空格 “main”
So type in ‘int’, space, ‘main’,
左括号
open parenthesis,
Eclipse 会自动补全右括号
and Eclipse should automatically insert a close parenthesis for you.
把光标移动到右括号右边
Come on the other side of that close parenthesis,
按回车键
hit ‘Enter’ again,
输入一个花括号
and now you want to enter in a brace.
花括号要怎么输呢
Ok, so,
在键盘上同时按下“Shift”和左中括号的键
on your keyboard that will probably be the ‘Shift’ key and the open square bracket.
就能输入一个左花括号了
So that will get you to the open brace.
再按回车键
Hit ‘Enter’ again,
你会发现 Eclipse 自动补全了右花括号
and you’ll notice that Eclipse automatically inserts a close brace for you.
而且输入位置还自动空了几格(缩进)
And it’s also moved our insertion space over some,
这是正常的
and this is perfectly fine.
一般来说 左花括号和右花括号之间
In general, when we have an open brace and a close brace,
所有的代码
we want to move all the text within that open brace and close brace,
都要缩进 这里的缩进是四个空格
over by some level of indentation, I think here we’re doing four spaces.
继续输入“cout” 空格
So now we’re going to type in ‘cout’, space,
输入两个左尖括号
and then we’re going to use the open angle bracket twice,
空格
space,
输入一个双引号
and then we’re going to use double quotes.
你会发现 Eclipse 自动补全了右双引号
So you’ll notice that Eclipse automatically inserts the close double quotes for us.
然后输入“C++ is FUN”
And then we’ll type ‘C++ is FUN’,
加上个感叹号
exclamation point.
再加上反斜杠和“n”
And then we want to have a slash, ‘n’.
把光标移动到
And then we’re going to come on the other side of the close parenthesis,
右双引号右边
now excuse me, the close quotation mark.
然后
double quotes,
输入分号
and hit the semicolon,
按回车键
and then hit ‘Enter’.
输入“return”
And then we’re going to type in ‘return’,
空格 “0”
space, ‘0’,
分号
semicolon.
好 来解释一下上面这些代码
Ok, so let’s talk about the code that we written.
这段代码叫做 main 函数(主函数)
This code here is what we call, the main function.
主函数从这里开始
So it starts here,
到这里结束
and it ends here.
这一行叫做主函数的头部
The very first line that we have here is what’s called the main function’s header.
剩下几行叫做主函数的函数体
And then the rest of it is what’s called the body of the main function.
一般来说 我们所说的函数
In general, when we talk about a function,
就是指起了名的一系列指令
it’s just some sort of named set of instructions,
这些指令能完成一些特定的任务
related to performing some particular task.
后面我们会介绍如何编写主函数以外的函数
And later on we’ll look at writing additional functions besides the main function.
每个 C++ 程序都要有一个主函数
So every single C++ programme requires a main function.
这是因为程序是从主函数开始运行的
Because this is where programme execution actually starts.
不管运行什么程序
Whenever we run our particular programme,
首先执行的都是主函数里的代码
it’s going to start executing in the main function.
现在我们来看看函数体中的代码
So let’s look at the actual code within the body.
函数体从左花括号开始
So the body starts here at the open brace,
到右花括号结束
and stops here at the close brace.
这之间的代码都是函数体的一部分
And all the code that’s within that is part of the body of that function.
花括号里第一行代码开头是“cout”
So here our first line of code has this ‘cout’.
“cout”是“character output stream”的缩写
And ‘cout’ stands for character output stream,
它的作用是在屏幕上显示出字符
and this is providing us with the ability to display characters to the screen.
所以我们每次运行这个程序
So whenever we run this particular programme,
屏幕上都会显示出一些字符
hopefully we’ll get something displayed on the screen.
下面这部分
The next part here,
是两个左尖括号
we have these two open angle brackets.
这叫做输出运算符 或者叫插入运算符
And this is what’s called the output or the insertion operator.
这个运算符将自己右边的东西
And it takes whatever we have here on the right hand side,
重定向到左边的输出流去
and redirects it to what we have on the left hand side.
在这里也就是把“C++ is FUN!”
So in this particular case it’s sending the text ‘C++ is FUN!’,
重定向到“cout”这个流 从而在屏幕上显示字符
to ‘cout’ which in turn, displays the text on the screen.
然后这串字符后边还有’\n’
Now toward the end of this you’ll see this ‘\n’,
这个字符有点奇怪
and that’s a little bit peculiar, unusual.
其实 这个字符叫做
And really what’s going on here, is
换行符(new line character)
we’re denoting a new line character.
‘\n’是个特殊的字符
So we have a special character here, ‘\n’,
它的作用是表示换行
and that denotes a new line character.
这串字符在屏幕上打印出来
So anyway we…whenever we print this out on a screen,
效果会是“C++ is FUN!”
It’s going to have ‘C++ is FUN!’ and then,
然后换行
a new line character.
注意 所有 C++ 语句都要以分号结尾
So, it turns out that all C++ statements should be terminated by a semicolon.
所以这行代码其实就是一个语句
So this is in fact a statement that we have,
在末尾也的确有个分号
and at the end you’ll see that we do in fact have a semicolon.
好 我们接着看下一行
Alright, so let’s look at the next line…
下一行 也是主函数的最后一行
eh, next and last line of code here, in the main function.
是“return 0;”
It says ‘return 0;’,
看来又是个语句
So it looks like a statement once again.
它的意思是让 main 函数返回一个值 0
And in this case, this function ‘main’ is returning the value of 0.
事实上
And it turns out that,
所有函数都要指定返回值的类型
with all functions, we can have a return type specified.
回头看看主函数的头部
Here if we look back at the main function header,
头部里第一个东西就是返回值的类型
the very first thing that we have here is specifying the return type.
这里的返回值是整数型(int)
So the return type specified here was an integer type.
这表示这个函数做完所有事后
So this means that this function, when all is set and done,
必须返回一个整数型的返回值
has to return a value that’s of integer type.
这就是这行代码的作用
And that’s exactly what we’re doing here,
返回整数值 0
we’re returning the value of zero.
0 代表什么呢
And this ‘0’ here,
0 代表程序运行成功
just simply indicates that our programme terminanted or ran successfully.
你可能会好奇返回值返回给了谁
You may be wondering where is this being returned to.
其实 返回值返回给了调用主函数的函数
Well, it’s returning to whoever called main, whoever invoked main.
后面我们就会看到如何调用其他函数
Later on we’ll be invoking other, or calling other functions,
我们可以从任何函数中调用其他函数
from main, or from other functions.
因此 我们所说的返回值
So whenever we talk about something being returned,
都是返回给了函数的调用者
it’s being returned to whoever invoked, or called that particular function.
好了 到这里我们就把代码解释完了
Ok, so now that we’ve discussed the code in our first programme,
差不多可以运行看看了
we’re about ready to run it.
不过还有点工作要做
But there’s a couple more things that we need to do.
首先我们要保存这份代码
One thing that we need to do is actually save this particular programme file.
注意到“FirstProgram.cpp”旁边
You’ll notice here that next to the ‘FirstProgram.cpp’ name,
有个小小的星号
that we have a little asterisks, or star here.
这表示这个文件已经被修改过
This is indicating that we have not actually saved this particular file,
但还未保存
back since we last modified it.
所以我们先保存
So what we’ll do now is actually save it.
同时按下键盘“Ctrl”和“S”键
So you can click on, or hold down the ‘Control’ key and hit ‘S’,
这是一种保存的方法
that’s one way to do a save.
或者你也可以在工具栏上单击这个软盘按钮
Or you can come over here on the toolbar and click the little diskette.
我们就单击它
That’s what I’m going to do, is just click on the little diskette icon,
现在那个星号没了
and now you’ll see that the little asterisks, or star has been removed,
就表示保存好了
indicated that this is up to date.
现在的文件是最新的
We have the latest and greatest.
接下来我们要构建这个项目
Ok, so the next thing that we’re going to do is actually build this particular project.
要构建项目
And the way that we’re going to do that,
只要点击这个按钮
is to use this little button here,
这是“Build”按钮
which is our build tool button, or our ‘Build’ button.
构建分为两个阶段
And build process really takes place in two stages.
第一阶段是编译
The first stage, is going to be a compilation process,
编译所有 C++ 代码
in which it’s going to compile all the C++ code.
编译又是什么意思呢
What does that mean?
意思是把我们输入的代码转换为机器使用的代码
That means it’s going to transform this particular code, here into machine code.
也就是机器能理解并运行的代码
That means code that is particular computer system can understand and run.
构建过程的第二阶段
And the next thing that’s going to take place in the build process,
叫做链接
is a linking phase.
链接的意思是
And what’s going to transpire there,
把第一阶段转换出来的
is it’s link the machine code,
机器代码
that was generated from our first programme,
和来自 iostream 等
along with other machine code,
其他文件的机器代码链接到一起
from some of the library files that we made use of from iostream.
链接过程的产物
And at that point in time,
是一个可执行文件
it’s going to generate an executable file.
而这个可执行文件
On that executable file is the whole kit and caboodle,
就是实实在在能运行的最终程序了
that will actually run this particular programme.
我们继续 单击“Build”按钮
Go ahead and click on the ‘Build’…’Build’ button here,
构建项目
and we’ll build our project.
注意到控制台里出现了一些东西
You’ll notice here on the console that we have some things going on as well,
可以看到编译过程调用了哪个编译器
you can see what compiler was actually invoked,
以及用了什么选项调用这个编译器
there were some options associated with this particular compiler.
还可以看到生成了一个“FirstProject.exe”
And you can also see, that we generated a ‘FirstProject.exe’,
这就是实际的可执行文件
so this is an actual executable file.
在“Project Explorer”里
And if we come over here to ‘Project Explorer’,
也能看到这个可执行文件
you can also see that we have an executable file.
左键单击“FirstProject”旁边的按钮
So go ahead and left click on the little button next to ‘FirstProject’,
这个按钮可能是个小三角 也可能是个加号
it may be a little triangle, it may be a little plus symbol.
我这里是个小三角 然后会展开列表
In my case, it’s a little triangle, and that should expand out the listing.
然后在“Binaries”旁边也有个小三角
And then it’s, the…also the same case here with the ‘Binaries’,
点击它展开列表
so we want to expand out the listing there.
可以看到“FirstProject.exe”
So here you can see that we have ‘FirstProject.exe’,
还有“x86”
and you can also see the ‘x86’,
“x86”是这个程序运行所需的平台架构
this is indicating the architecture that this particular executable will actually run on.
这里的意思就是这程序运行在 x86 架构上
So in this case it’s saying that it’ll run x86,
也就是英特尔和 AMD 的处理器上
so those are your intel processors and your…your AMD processors.
好了 现在我们运行这个程序 看看效果
Alright, so let’s try to actually run this programme and see what transpires.
往上看到工具栏
So I’m going to go up again to the toolbar,
点击“Run”按钮
and click on the ‘Run’ button.
就是这个绿色的小按钮
So again it’s the little green button,
上面有个白色的播放标志 白色的三角
with the like, play…the white play button or the white triangle button.
点击它
So go ahead and left click on it,
看看会发生什么
and we’ll see what happens.
看下边的控制台
Ok, down here, if we look at our console,
我们的确看到了“C++ is FUN!”
you’ll see that we did in fact get ‘C++ is FUN!’.
看来我们的程序正确运行了
So it looked like our programme did exactly what we expected it to do.
它向控制台 或者说向屏幕
To print out, to the console or to the screen, to the monitor,
打印出了“C++ is FUN!”
‘C++ is FUN!’.

发表评论

译制信息
视频概述

用 Eclipse 编写第一个 C++ 程序

听录译者

positiveme

翻译译者

HX

审核员

知易行难

视频来源

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

相关推荐