(2)简单的程序

C++由一个或多个函数组成。

由 Visual C++ 中 Application Wizard 生成的 Win32 控制台程序具有一个名称为 _tmain 的主函数。

wmain 和 _tmain 是 Microsoft 专有的,符合 C++ 标准的主函数名称是 main。

简单的程序

通过简单的示例帮助更好的了解一个程序的元素。

(1)Ctrl + Shift + N ,项目类型中选择 Installed | Templates | Visual C++ | Win32,模板中选择 Win32 Console Application,项目命名为 Ex2_01。

image

(2)在 Win32 Console Application Wizard 窗口中选择 Application Settings,在 Additional Options 中勾选中 Empty project,并点击 Finish 按钮。

image

(3)现在创建好了一个空的项目,不包含任何文件。选择 Project | Properties 菜单或者 Alt + F7,显示项目属性对话框。

image

(4) 在 Ex2_01 Property Pages 对话框中选择 Configuration Properties,Configurations 中选择 All Configurations,然后在 General 中选择 Character Set,设置为 Not Set。

image

(5)在 Solution Explorer 窗体中选择 Source Files,点击右键选择 Add | New Item 菜单项

image

(6) 在 Add New Item 对话框中选择 Visual C++ 中的 C++ File(.cpp),在 Name 中输入文件名 Ex2_01,点击 Add 按钮添加。

image

(7)在新创建的 Ex2_01.cpp 中编辑代码如下:

复制代码
  1 // Ex2_01.cpp
  2 // A Simple Example of a Program
  3 #include <iostream>
  4 
  5 using std::cout;
  6 using std::endl;
  7 
  8 int main()
  9 {
 10 	int apples, oranges;
 11 	int fruit;
 12 
 13 	apples = 5;
 14 	oranges = 6;
 15 	fruit = apples + oranges;
 16 
 17 	cout << endl;
 18 	cout << "Oranges are not the only fruit..." << endl
 19 		 << "- and we have " << fruit << " fruits in all.";
 20 	cout << endl;
 21 
 22 	return 0;
 23 }
复制代码

(8)编译链接程序,通过 Build | Build Ex2_01 或者 F7,编译通过 Output 窗口显示

image

image

(9)编译成功,通过按 Ctrl + F5,或者从 Debug | Start Without Debugging 菜单项,执行这个程序。

image

image

posted on   pchmonster  阅读(207)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示