C++ Phase 01 Notes 01 [First Understanding of C++]

C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]

table of Contents

C++ course schedule

1 Introduction to C++

1.1 The first C++ program

1.1.1 Create Project

1.1.2 Create file

1.1.3 Writing code

1.1.4 Run the program

1.2 Notes

1.3 Variables

1.4 Constant

#define macro constant

const modified variable

Constant demo code

1.5 Keywords

1.6 Identifier naming rules


C++ course schedule

  • Clarify the learning stage and content of the C++ course

stage content aims Case study
The first stage Introduction to C++ Basic Syntax Have a basic understanding of C++ and have basic programming skills Address Book Management System
second stage C++ core programming Introduce C++ object-oriented programming to pave the way for large-scale projects Staff Management System
The third phase C++ improves programming Introduce C++ generic programming ideas and the basic use of STL Speech contest system
  • Comprehensive case: computer room reservation system

1 Introduction to C++

1.1 The first C++ program

Writing a C++ program is divided into 4 steps:

  1. Create project

  2. Create a file

  3. Write code

  4. Run the program

1.1.1 Create Project

Visual Studio is the main tool we use to write C++ programs. Let's open it first.

1.1.2 Create file

Right-click the source file and select Add->New Item:

Give the C++ file a name and click Add.

1.1.3 Writing code

#include <iostream>
using namespace std;

int main1()
{
	cout << "Hello world !" << endl;
	system("pause");
	return 0;
}

1.1.4 Run the program

1.2 Notes

Function : Add some explanations and explanations to the code, so that you or other programmers can read the code.

Two formats

  • Single line comment :// 描述信息
    • It is usually placed at the top of a line of code or at the end of a statement to describe the line of code.
  • Multi-line comments :/* 描述信息 */
    • It is usually placed above a piece of code to give an overall description of the piece of code.

Tip: When the compiler compiles the code, it ignores the content of the comment.

 

1.3 Variables

Function : Give a name to a specified memory space to facilitate the operation of this memory.

Syntax :数据类型 变量名 = 初始值;

Example:

Note: When C++ creates a variable, you must give the variable an initial value, otherwise an error will be reported!

1.4 Constant

Function : Used to record the unchangeable data in the program.

C++ defines constants in two ways:

  • #define macro constants:#define 常量名 常量值

    • It is usually defined at the top of the file and represents a constant.

  • const modified variable:const 数据类型 常量名 = 常量值

    • The keyword const is usually added before the variable definition to modify the variable as a constant and cannot be modified.

#define macro constant

Example:

const modified variable

Constant demo code

/*
作用:用于记录程序中不可更改的数据。

C++定义常量两种方式
1.#define 宏常量:#define 常量名 常量值
   通常在文件上方定义,表示一个常量

2.const 修饰的变量 :const 数据类型 常量名 = 常量值
   通常在变量定义前加关键字,修饰该变量为常量,不可修改
*/

#include <iostream>
using namespace std;

//1、宏常量
#define Day 7

int main4()
{
	//day = 14;  //报错(错误),Day是常量,宏常量day不可以修改,一旦修改就会报错!
	cout << "一周里总共有 " << Day << " 天" << endl;

	//2、const修饰变量
	const int month = 12;
	//month = 24; //报错(错误),常量是不可以修改的。const修饰的变量也称为常量!
	cout << "一年里总共有 " << month << " 个月份" << endl;

	system("pause");

	return 0;
}

1.5 Keywords

Function: Keywords are pre-reserved words (identifiers) in C++.

  • When defining variables or constants, do not use keywords.

C++ keywords are as follows:

asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break else long sizeof union
case enum mutable static unsigned
catch explicit namespace static_cast using
char export new struct virtual
class external operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true  
delete goto reinterpret_cast try  

提示:在给变量或者常量起名称时候,不要用C++得关键字,否则会产生歧义。

1.6 Identifier naming rules

Function : C++ stipulates that it has its own set of rules when naming identifiers (variables, constants).

  • Identifiers cannot be keywords.

  • The identifier can only consist of letters, numbers, and underscores.

  • The first character must be a letter or underscore.

  • The letters in the identifier are case sensitive.

Suggestion: When naming an identifier, strive to achieve the effect of knowing the meaning of the name and making it easier for yourself and others to read.

  

#include <iostream>
using namespace std;

//标识符命名规则
//1、标识符不可以是关键字
//2、标识符是由字母、数字、下划线构成
//3、标识符第一个字符只能是字母或下划线
//4、标识符是区分大小写的

int main()
{
	//1、标识符不可以是关键字
	//int int = 10; // 报错

	//2、标识符是由字母、数字、下划线构成
	int abc = 10;
	int _abc = 20;
	int _123abc = 30;

	//3、标识符第一个字符只能是字母或下划线
	//int 123abc = 40; // 报错

	//4、标识符是区分大小写的
	int aaa = 100;
	//cout << AAA << endl; // AAA和aaa不是同一个名称

	//建议:给变量起名的时候,最好能够做到见名知意。
	int num1 = 10;
	int num2 = 20;
	int sum = num1 + num2;
	cout << sum << endl;

	system("pause");

	return 0;
}

Come on~

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/114795200