(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

原文地址:https://www.cnblogs.com/pchmonster/p/14077121.html