C++ Programming [Tan Haoqiang] The first article: Basic knowledge

Chapter 1 Preliminary Knowledge of C++

The basic standard program form of C++

# include <iostream>  //头文件没有.h
# include <string>    //string头文件

using namespace std;  //使用命名空间std

int main(void)
{
    string str;       //增加了字符串变量这个数据类型string
    
    return 0;
}

The simplest C++ program

# include <iostream>

using namespace std;

int main(void)
{
    cout<<"This is a C++ program";
    return 0;
}

C++ program to find a+b

#include <iostream>

using namespace std;

int main(void)
{
    int a, b, sum;
    cin >>a >>b;  //输入语句
    sum = a + b;
    cout << "a + b = " << sum << endl;   //输出语句,其中endl是end line的缩写,表示本行结束,用于换行
    
    return 0;
}

PS: Notes are very important, sometimes they can account for 1/3 of a program

Class is a newly added important data type of C++, and it is the most important development of C++ to C. With classes, functions such as encapsulation, information concealment, inheritance, derivation and polymorphism in object-oriented can be realized.

A program written in a high-level language is called a source program. The suffix name of a C++ source program is .cpp, which is actually the abbreviation of C plus plus

Chapter 2 Data Types and Expressions

C++ data types

Data types and data structures: For example, using pointers and structure types can form complex structures such as tables, trees, and stacks

constant

Including numeric constants, character constants and symbolic constants

Why floating-point numbers are called floating-point numbers: In memory, floating-point numbers can be expressed in exponential form (of course, they can also be expressed in decimal form). In the process of exponent expression, the position of the decimal point can be floated by changing the exponent part , This is the origin of the name floating point number

Note in character constants: ordinary character constants are represented by single quotation marks, while strings are represented by double quotation marks

cout << '\n';
//等价于
cout << endl;

Arithmetic operations between character constants and integers (case conversion, because character constants are ASCII codes,'a' is 97,'A' is 65)

#include <iostream>
using namespace std;

int main(void)
{
	char c1, c2;
	c1 = 'a';
	c2 = 'b';
	c1 = c1 - 32;
	c2 = c2 - 32;
	cout << c1 << ' ' << c2 << endl;
	return 0;
}

A B

Examples of symbolic constants:

#define PRICE 30

variable

Variable: the amount whose value can be changed during the running of the program

Hungarian nomenclature: cSex represents that the variable is of character type, iCount represents that the variable is of integer type

The reason for determining the data type is: when the variable is assigned to a certain data type, the corresponding storage unit can be allocated for it at compile time (the importance of memory allocation in the program)

Read-only variable (constant variable): The value in the storage unit is not allowed to change, and its definition method is

const int a;

Operator

I think there are a few things to pay attention to here

(1) Logical operators : && and, || or ,! non-

(2) Bitwise operators : & bitwise AND, | bitwise OR, ~ bitwise negation , ^ bitwise exclusive OR, << bitwise left shift and >> bitwise right shift

(3) Member operator:.

(4) Operators pointing to members: ->

(5) Subscript operator: []

(6) Modulus (remainder) operator:%

(7) Increment and decrement operators:

I have to mention the difference between ++i and i++

Regarding the value change of i itself, ++i is no different from i++, but if you want to get the return value j of the statement ++i or i++ (such as j=i++), then the difference between them is big. j = i++ means that the value of i is first added to j and then i is incremented, and j = ++i means that i is first incremented and then the value of i is given to j. My point is, try to make them indistinguishable when you write your own code, that is, don’t write statements like j = i++ or j=++i, and use i++ or ++i alone to increase i each time. The procedure will be clearer. The core here is to distinguish that the value of an expression and the value of a variable are different concepts.

Guess you like

Origin blog.csdn.net/weixin_43450646/article/details/106933151