
1.4 编写及运行程序
构建好开发环境后,就可以在该环境中编写程序了。本节将讲解如何在Visual Studio和考试版Visual C++ 2010中编写及运行程序。
1.4.1 使用Visual Studio
下面将讲解如何在Visual Studio中编写及运行程序。
1.首次启动Visual Studio
安装完Visual Studio后,就可以启动VS了。如果是首次启动VS,则需要对VS进行一些设置,具体操作步骤如下。
(1)在“开始”菜单中找到“Visual Studio 2019”并单击,进入Visual Studio 2019加载页面,如图1.19所示。
(2)一段时间后,进入“登录Visual Studio”界面,如图1.20所示。单击“以后再说”按钮,进入环境启动界面,如图1.21所示。这里开发者可以根据自己的需要选择。

图1.19 Visual Studio 2019加载页面

图1.20 “登录Visual Studio”界面
(3)单击“启动Visual Studio”按钮,进入“我们正为第一次使用做准备”界面,如图1.22所示。这时,需要等待软件自动配置完成以后才可以正常使用Visual Studio。

图1.21 环境启动界面

图1.22 “我们正为第一次使用做准备”界面
(4)等待一段时间后,就会进入Visual Studio的起始界面,如图1.23所示。

图1.23 Visual Studio的起始界面
注意:如果不是第一次启动VS,则在打开或启动VS后,首先会显示Visual Studio的Logo,一段时间后,就会进入Visual Studio的起始界面。
2.创建项目
项目的创建可以将C++语言开发中使用的文件保存起来,帮助用户管理程序文件和资源文件。以下就是创建一个Hello C++项目的具体操作步骤。
(1)在Visual Studio的起始界面,单击“创建新项目”选项,弹出“创建新项目”对话框,如图1.24所示。

图1.24 “创建新项目”对话框
(2)选择“控制台应用”选项,弹出“配置新项目”对话框,如图1.25所示。输入项目名称,此处输入Hello C++。

图1.25 “配置新项目”对话框
(3)单击“创建”按钮,弹出“Microsoft Visual Studio”对话框,如图1.26所示。

图1.26 “Microsoft Visual Studio”对话框
(4)一段时间后,Hello C++项目就创建好了,如图1.27所示。

图1.27 创建的项目
3.编写程序
创建好项目之后,编写如下程序,该程序的功能是输出一个字符串Hello C++。

4.运行程序
程序编写完成,单击工具栏中的“运行”按钮运行程序,如图1.28所示。

图1.28 单击“运行”按钮
程序的运行结果会在弹出的Microsoft Visual Studio 调试控制台中显示,如图1.29所示。

图1.29 Microsoft Visual Studio 调试控制台
1.4.2 使用考试版Visual C++ 2010
下面将讲解如何在考试版Visual C++ 2010中编写及运行程序。
1.首次启动Visual C++ 2010
安装完考试版Visual C++ 2010后,就可以启动Visual C++ 2010了。首次启动Visual C++2010的具体操作步骤如下。
(1)在“开始”菜单中找到“Microsoft Visual C++ 2010 Express”并单击,进入Visual C++2010加载界面,如图1.30所示。

图1.30 Visual C++ 2010加载界面
(2)一段时间后,弹出“Microsoft Visual C++ 2010学习版”对话框,如图1.31所示。

图1.31 “Microsoft Visual C++ 2010学习版”对话框
(3)一段时间后,会进入“起始页-Microsoft Visual C++ 2010 学习版”界面,如图1.32所示。

图1.32 “起始页-Microsoft Visual C++ 2010 学习版”界面
注意:如果不是第一次启动考试版Visual Studio,则在打开或启动VS后,首先会显示考试版Visual C++ 2010加载界面,一段时间后,就会进入“起始页-Microsoft Visual C++ 2010学习版”界面。
2.创建项目
对于Visual C++ 2010开发环境,在编写程序之前,也需要创建项目。创建Hello C++项目的具体操作步骤如下。
(1)在“起始页-Microsoft Visual C++ 2010 学习版”界面中单击“新建项目…”按钮,弹出“新建项目”对话框,然后选中“Win32 控制台应用程序”选项,在“名称”文本框中输入Hello C++,如图1.33所示。

图1.33 “新建项目”对话框
(2)单击“确定”按钮,弹出“欢迎使用Win32应用程序向导”对话框,如图1.34所示。

图1.34 “欢迎使用Win32应用程序向导”对话框
(3)单击“下一步”按钮,弹出“应用程序设置”对话框,如图1.35所示。

图1.35 “应用程序设置”对话框
(4)在“应用程序类型”选项组中选择“控制台应用程序”,在“附加选项”选项组中选择“空项目”选项。单击“完成”按钮,此时Hello C++项目就创建好了,如图1.36所示。

图1.36 Hello C++项目
3.编写程序
项目创建好后,要编写程序,还需要在“源文件”文件夹中添加一个.cpp文件,具体操作步骤如下。
(1)右击“源文件”文件夹,在弹出的快捷菜单中选中“添加”|“新项目”命令,弹出“添加新项-Hello C++”对话框,如图1.37所示。

图1.37 “添加新项-Hello C++”对话框
(2)选择“C++ 文件(.cpp)”选项,在“名称”文本框中输入Hello C++,单击“添加”按钮,此时一个名为Hello C++.cpp的文件就添加到了“源文件”文件夹中,如图1.38所示。

图1.38 文件添加成功
(3)添加了Hello C++.cpp文件后,就可以在该文件中添加以下程序了,该程序用于输出字符串Hello C++。

4.运行程序
程序编写无误后,就可以运行了。单击工具栏中的“运行”按钮,如图1.39所示,会弹出“Microsoft Visual C++ 2010 学习版”对话框,如图1.40所示。单击“是”按钮,弹出Hello C++.exe对话框,如图1.41所示。在此对话框中会看到程序执行的结果。

图1.39 单击“运行”按钮

图1.40 “Microsoft Visual C++ 2010 学习版”对话框

图1.41 Hello C++.exe对话框