C++ Basic Introductory Tutorial (1)

1 Introduction to C++

1.1 The first C++ program

Writing a C++ program is divided into 4 steps.
Create a project
, create a file
, write code
, and run the program

. 1.1.1 Create a project.
Visual Studio is the main tool we use to write a C++ program. Let’s open it first.

insert image description hereinsert image description here
insert image description here
1.1.2 Create a file
and right-click the source file , select Add->New Item

insert image description here
to give the C++ file a name, and then click Add.
insert image description here
1.1.3 Write code

#include<iostream>
using namespace std;

int main() {
    
    

	cout << "Hello world" << endl;

	system("pause");

	return 0;
}

1.1.4 Running the program
insert image description here

1.2 Notes

Function: Add some descriptions and explanations to the code, so that you or other programmers can read the code.
Two formats of single-

line comments: // Description information
is usually placed above a line of code, or at the end of a statement, to explain the line of code
Multi-line comment: /
Description information /
is usually placed above a piece of code, giving an overall description of the code Tip

: When the compiler compiles the code, it will automatically ignore the content of the comment

1.3 Variables

Function: Name a specified memory space to facilitate operation of this memory
Syntax: data type variable name = initial value;
example:

#include<iostream>
using namespace std;
int main() 
{
    
    
	//变量的定义
	//语法:数据类型  变量名 = 初始值
	int a = 10;
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

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

1.4 Constants

Function: used to record unchangeable data in the program
C++ defines constants in two ways
#define macro constants: #define constant name constant value
is usually defined above the file, indicating a constant
const modified variable const data type constant name = constant value
usually Add the keyword const before the variable definition to modify the variable as a constant and cannot be modified
Example:

//1、宏常量
#define day 7
int main() 
{
    
    
	cout << "一周里总共有 " << day << " 天" << endl;
	//day = 8;  //报错,宏常量不可以修改
	//2、const修饰变量
	const int month = 12;
	cout << "一年里总共有 " << month << " 个月份" << endl;
	//month = 24; //报错,常量是不可以修改的
	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 extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default f riend register true
delete goto reinterpret_cast try
Tip: when giving variables or When naming constants, do not use C++ keywords, otherwise ambiguity will arise

1.6 Identifier Naming Rules

Role: C++ stipulates that when naming identifiers (variables, constants), there is a set of rules of its own.
Identifiers cannot be keywords.
Identifiers can only consist of letters, numbers, and underscores.
The first character must be a letter or an underscore
letter in an identifier. Suggestion for case sensitivity

: When naming identifiers, strive to achieve the effect of knowing the name and knowing the meaning, so as to facilitate reading by yourself and others

2 data types

C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise memory cannot be allocated to the variable

2.1 Integer

Function: Integer variables represent integer data.
In C++, there are several ways to represent integer types. The difference is that the memory space occupied is different:

the data type takes up space and the range of values
​​is short (short integer) 2 bytes. (-2^15 ~ 2^15-1)
int (integer) 4 bytes (-2^31 ~ 2^31-1)
long (long integer) Windows is 4 bytes, Linux is 4 bytes ( 32 bits), 8 bytes (64 bits) (-2^31 ~ 2^31-1)
long long (long long integer) 8 bytes (-2^63 ~ 2^63-1)

2.2 sizeof keyword

Function: Use the sizeof keyword to count the memory size occupied by the data type
Syntax: sizeof(data type/variable)

Example:

int main() 
{
    
    
	cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
	cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
	cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
	cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;
	system("pause");
	return 0;
}

Integer conclusion: short < int <= long <= long long

2.3 Real type (floating point type)

Function: Used to represent decimals.
There are two types of floating-point variables:
single-precision float
and double-precision double.
The difference between the two lies in the range of valid numbers represented.

Data Type Occupied space Valid figure range
float 4 bytes 7 valid figures
double 8 bytes 15-16 valid figures

Example:

int main()
 {
    
    
	float f1 = 3.14f;
	double d1 = 3.14;
	cout << f1 << endl;
	cout << d1<< endl;
	cout << "float  sizeof = " << sizeof(f1) << endl;
	cout << "double sizeof = " << sizeof(d1) << endl;
	//科学计数法
	float f2 = 3e2; // 3 * 10 ^ 2 
	cout << "f2 = " << f2 << endl;
	float f3 = 3e-2;  // 3 * 0.1 ^ 2
	cout << "f3 = " << f3 << endl;
	system("pause");
	return 0;
}

2.4 Character type

Function: Character variables are used to display a single character
Syntax: char ch = 'a';

Note 1: When displaying character variables, enclose the characters with single quotes
instead of double quotes Note 2: There can only be A character cannot be a string.

Character variables in C and C++ only occupy 1 byte.
Character variables do not store the character itself in memory, but put the corresponding ASCII code into the storage unit.

int main() 
{
    
    
	char ch = 'a';
	cout << ch << endl;
	cout << sizeof(char) << endl;
	//ch = "abcde"; //错误,不可以用双引号
	//ch = 'abcde'; //错误,单引号内只能引用一个字符
	cout << (int)ch << endl;  //查看字符a对应的ASCII码
	ch = 97; //可以直接用ASCII给字符型变量赋值
	cout << ch << endl;
	system("pause");
	return 0;
}

ASCII code table:
insert image description here
ASCII code is roughly composed of the following two parts:
ASCII non-printing control characters: numbers 0-31 on the ASCII table are assigned to control characters for controlling some peripheral devices such as printers
ASCII printing characters: numbers 32-126 Assigned to characters that can be found on the keyboard and appear when viewing or printing a document

2.5 Escape characters

Function: It is used to represent some ASCII characters that cannot be displayed.
At this stage, our commonly used escape characters are: \n \ \t

The meaning of the escape character ASCII code value (decimal)
\a Alarm 007
\b Backspace (BS), will Move the current position to the previous column 008
\f Form feed (FF), move the current position to the beginning of the next page 012 \n Line feed
(LF), move the current position to the beginning of the next line 010
\r Carriage return (CR), move the current position Move the position to the beginning of the line 013
\t Horizontal tab (HT) (jump to the next TAB position) 009
\v Vertical tab (VT) 011
\ represents a backslash character "" 092
' represents a single quote (apostrophe number) character 039
" represents a double quote character 034
? represents a question mark 063
\0 number 0 000
\ddd octal escape character, d range 0~7 3-digit octal
\xhh hexadecimal escape character, h Range 09, af, A~F 3-digit hexadecimal

example:

int main() 
{
    
    
	cout << "\\" << endl;
	cout << "\tHello" << endl;
	cout << "\n" << endl;
	system("pause");
	return 0;
}

2.6 String type

Function: used to represent a string of characters
in two styles
C-style string: char variable name [] = "string value"
Example:

int main() 
{
    
    
	char str1[] = "hello world";
	cout << str1 << endl;
	system("pause");
	return 0;
}

Note: C-style strings should be enclosed in double quotes
C++-style strings: string variable name = "string value"
Example:

int main()
 {
    
    
	string str = "hello world";
	cout << str << endl;
	system("pause");
	return 0;
}

Note: C++ style strings need to add header files ==#include==

2.7 Boolean type bool

Function: The Boolean data type represents true or false values.
The bool type has only two values:
true—true (essentially 1)
false—false (essentially 0)

The bool type occupies 1 byte
. Example:

int main()
 {
    
    
	bool flag = true;
	cout << flag << endl; // 1
	flag = false;
	cout << flag << endl; // 0
	cout << "size of bool = " << sizeof(bool) << endl; //1
	system("pause");
	return 0;
}

2.8 Data input

Function: Used to get data from the keyboard
Keyword: cin
Syntax: cin >> Variable

example:

int main()
{
    
    
	//整型输入
	int a = 0;
	cout << "请输入整型变量:" << endl;
	cin >> a;
	cout << a << endl;
	//浮点型输入
	double d = 0;
	cout << "请输入浮点型变量:" << endl;
	cin >> d;
	cout << d << endl;
	//字符型输入
	char ch = 0;
	cout << "请输入字符型变量:" << endl;
	cin >> ch;
	cout << ch << endl;
	//字符串型输入
	string str;
	cout << "请输入字符串型变量:" << endl;
	cin >> str;
	cout << str << endl;
	//布尔类型输入
	bool flag = true;
	cout << "请输入布尔型变量:" << endl;
	cin >> flag;
	cout << flag << endl;
	system("pause");
	return EXIT_SUCCESS;
}

The end of this chapter!

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131298276
Recommended