第一个C++程序(Visual Studio官方推荐): Create a C++ console app project


Create a C++ console app project

官网程序链接

Create a C++ console app project

- 右上方可以选择 "使用英文阅读"

1. 一些初始化需要的设置:字体,语言,窗口

  1. 字体设置:Tools >> Options >> Enviroment >> Fonts and Colors >> Size
    • 我设置成15号大小
  2. 界面设置成英文:Tools >> Options >> Enviroment >> International Settings >> Language
    • 如果没有想要的语言的话,就要自己安装语言包
    • 在程序最上方的Help右边的Search框中输入 “language” ,确定搜索。
    • 出现一个框之后,点击右下方的 “查看完整的安装详细信息”
    • 出现“语言包”,选择“英文”打钩,单击“修改”,等待安装完成,重启程序生效。
  3. 重置窗口布局:Window >> Reset window layout
    • 设置 Auto Hide all window 之后想要显示窗口也是通过重置窗口实现。
    • 更多的视图显示在 View 中设置。

2. 创建工程

File >> New >> Project >> Empty project (C++)

3. 快速创建class

  1. Project >> Add class >> 输入 class 的名称
  • 确定后会同时生成两个文件(xx.h 和 xx.cpp),自动存放在两个文件夹下,如下图:
    第一个C++程序(Visual Studio官方推荐): Create a C++ console app project

4. debug 快捷键

  1. F5 :debug
  2. Ctl + F5: run
  3. F10 : 单步调代码(不进入代码内,常用)
  4. F11:单步调代码(进入代码内,不常用)

5. 第一个面向对象的程序

  1. 含三个文件
  • Calculator.h : 列出所有的 类
  • Calculator.cpp :Calculator 类的内容
  • CalculatorAPP.cpp :主程序含main()

Calculator.h : 列出所有的 类

class Calculator
{
public:
	double Calculate(double x, char oper, double y);
};

Calculator.cpp :Calculator 类的内容

#include "Calculator.h"

double Calculator::Calculate(double x, char oper, double y)
{
	switch (oper)
	{
		case '+':
			return x + y;
		case '-':
			return x - y;
		case '*':
			return x * y;
		case '/':
			return x / y;
		default: // 默认值,缺乏相应输入的时候,就输出0.0
			return 0.0;
	}
}

CalculatorAPP.cpp :主程序含main()

// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include <iostream>  //  <>:C++ Standard Library, while "" :quotes are used for other files.
#include "Calculator.h"  // 标准库用<> ,其他库用"" // 相当于python的 import

using namespace std; // 相当于python的from std import * // tells the compiler to expect stuff from the C++ Standard Library to be used in this file

int main()
{
	cout << "Hello sdfWorld!\n" << endl;     // << endl 和 \n 效果相同,前者放在字符串外,后者放在字符串内 // std::cout   "\n" is better than "<< endl"
	double x = 0.0;
	double y = 0.0;
	double result = 0.0;
	char oper = '+';  // 注意 只能是单引号
	cout << "Calculator Console Application" << endl << endl;
	cout << "Please entere the operaton to perform. Format:a+b | a-b | a*b a/b" << endl;

	Calculator c; // line declares an object named 'c' as an instance of the Calculator class. 
	while (1)
	{
		cin >> x >> oper >> y;  // 接收三个非空的字符,分别赋值给三个变量
		if (oper == '/' && y == 0)
		{
			cout << "Division by 0 exception" << endl;
			continue;
		}
		else
		{
			result = c.Calculate(x, oper, y);   // Calculator.Calculate(x, oper, y);
		}		
		cout << "Result is: " << result << endl;
	}
	return 0; 
}

//下面是一些VS的基础知识
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

(完)