"Fight Break CPP" Chapter 1---- A First Look at CPP

I’m probably bored, and I can’t comfort myself either vertically or horizontally. The emotion of writing the “Dou Po CPP” series came spontaneously. I was thinking about the result of writing this series. I am happy, and I am also surprised. I always disdain to go. Write a nutritious blog, but now it produces a series of nutritious blogs, or it is probably the era of "Dou Po CPP" .

content

Preamble

import of header files

use std namespace

Output and alarm on console screen

free writing format

Do not insert whitespace characters in words

Cannot wrap a line in the middle of a string literal

Do not wrap a line in the middle of a preprocessor directive

variable

initialization and assignment

Input from keyboard with constant object

Read in a string (getline)

To be continued


Preamble

The "Dou Po CPP" series lives up to its name, and it doesn't write any printf ("Hello world"\n) like C used before; this kind of program that is useless to friends who have learned C language, it is necessary to We will talk about extremely important, novel, and difficult to understand knowledge points and procedures , hoping to bring a lot of gains to the readers who read carefully!

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

import of header files

Let’s start with the first piece of code in this series:

#include <iostream>
 
using namespace std;
 
int main()
{
	cout << "\a第一个C++程序。"<<endl;
}

As shown in the above code: the first line starts with #. #include <iostream> indicates:

Embed the content of <iostream>, which contains the relevant content of the library (the building group that implements processing) "used to perform output on the screen or input from the keyboard, etc.".

In addition to the content of <iostream>, C++ also provides <string> etc. These are called header files . srring and iosteram in <> are header file names. Also, the act of embedding the contents of a header file is called importing .

use std namespace

Anyone who has studied CPP knows that the next line of #include is the using directive. using namespace std; means: use the std namespace . I'll learn about namespaces in a follow-up blog, but for now we just need to know that it's a "fixed statement" required when using the standard library provided by C++.

If we delete using namespace std;, we need to change all cout to std::cout. For unnecessary trouble, we need to develop the habit of writing fixed statements before writing the implementation code .
 

Output and alarm on console screen

A piece of code in the main() function: cout << "\aThe first C++ program."<<endl;  

cout is the stream connected to the console screen, called the standard output stream; and << means insertion, called the caret, which is responsible for inputting the content in " " into the console.

The \a appearing in the code is an escape character to indicate a warning . After inserting an alarm symbol into cout, the program will prompt visually or audibly, and you will know when you try it out. As for what escape characters are, the representation of escape characters, etc. I will share with you in the next blog.
 

free writing format

Let's look at the following piece of code:

#include <iostream>
 
using 
namespace std;
 
int main(
){
	cout
 << "\a第一个C++程序。"<<endl;
}

C++ adopts a free format that allows you to write programs in free places , and no matter how free you are, there are some constraints.

Do not insert whitespace characters in words

Such as int, main. cout, <<, //, etc. are all words. No whitespace characters can be inserted between them.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_15,color_FFFFFF,t_70,g_se,x_16

Cannot wrap a line in the middle of a string literal

cout << "\a第一个
C++程序。"<<endl;         \\这样写是错误的;
 
 
cout << "\a第一个“
                "C++程序。"<<endl;         \\这样写是正确的。

From this, it can be seen that adjacent string literals with whitespace characters in between are treated as one string literal connected together.

Do not wrap a line in the middle of a preprocessor directive

If you really need a line break in the middle, write a backslash (\) at the end of the line.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_19,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

variable

Variables are like boxes for storing values. Once you put a value in a box, it will keep the value as long as the box exists. Also, we are free to override the value and dispose of it. However, when we use variables, we must declare them first, otherwise it seems that there are many boxes on the ground, and we can't tell which box is which. So we have to declare it when using the variable.

The declaration statement to declare a variable named x is as follows:

int x;    //int型变量x的声明

  The variable x can only store integer values, and cannot store real values ​​with decimals like 1.2. This is the nature of the int type. As for the data types, it will be shared with friends later.

initialization and assignment

(1) Initialization: Assign a value when a variable is created.

(2) Assignment: Assign a value after creating a variable.

Let's look at the following example:

int x=66;     (1)
 
int x;
x=66;         (2)

There are other forms of initialization, let's dabble first:

int x(66);
int x{66};
int x={63};

Next, consider the following expression, where the variables a and b are both of type int.

a=b=5

The above assignment is obviously possible, and let's look at the following expression:

int a=b=5;

We can try it on the compiler, it's wrong. So we have to use assignment and initialization with caution .

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

Input from keyboard with constant object

#include <iostream>
 
using namespace std;
 
int main()
{
	double r;				
 
	cout << "半径:";		
	cin >> r;				
 
	cout << "周长是" << 2 * 3.14 * r <<endl;
	cout << "面积是" << 3.14 * r * r <<endl;		
}

If there is output, then there must be input, the output is cout, and the input is cin . I will bring in the usage of cin in the following program.

cin is the standard input stream combined with the keyboard, and >> used for cin is an extractor that removes characters from the input stream.

And 3.14 This constant with a fractional part is called a floating-point number literal. There are only two occurrences of 3.14 in this small program, whereas in a large program there may be hundreds of 3.14 occurrences. Using the compiler's replace function, we can easily change 3.14 to 3.1415. However, we have to find and fix hundreds of 3.14s, which is a huge waste of our time.

In such cases, const objects come into play. And look at the following code:

#include <iostream>
 
using namespace std;
 
int main()
{
	const double PI = 3.1416;	
	double r;					
 
	cout << "半径:";			    
	cin >> r;					
 
	cout << "周长是" << 2 * PI * r << endl;		
	cout << "面积是" << PI * r * r << endl;		
}

When declaring the variable PI, add const and initialize it to 3.1415, and the variable PI can become a constant object. The value of a constant object cannot be modified . We will discuss const in a later section. The benefits of using constant objects in this program are: (1) centrally manage values ​​in one place; (2) make the program easier to read.

Constant objects must be initialized when they are declared, and look at the following image:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_17,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

const is a cv qualifier used to specify properties of object type. In addition to const, the cv qualifier also has volatile.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

Read in a string (getline)

#include <string>
#include <iostream>
 
using namespace std;
 
int main()
{
	string name;	
 
	cout << "姓名:";		
	cin >> name;				
 
	cout << "你好," << name << endl;		

We can try to enter: Doubaqiong , the console will display Doubaqiang; and if we want to input: Doubaqiang (with a space in the middle), the console will only display: Doubaqiang. Why is this? This is because whitespace characters are skipped when using the extractor to read in . Therefore, when running the program, only "Dou Po" is read into the name due to the space added to the input string.

Next, I will bring you a solution for you guys ! And look at the following code:

#include <string>
#include <iostream>
 
using namespace std;
 
int main()
{
	string name;	
 
	cout << "姓名:";	
	getline(cin, name);			
 
	cout << "你好," << name << "。\n";		

A string including spaces when read by getline(cin, variable name) . All characters typed before the enter key will be put into a variable of type string.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pa95b6LLg==,size_20,color_FFFFFF,t_70,g_se,x_16

To be continued

If you think this blog is helpful to you who are learning programming, please give Shi Lu. more support and attention ! In the next period of time, Shi Lu. will fight against CPP with my friends. I hope that I can provide you with better blog content next time, and I hope that the next blog will have you!
 

Guess you like

Origin blog.csdn.net/qq_64263760/article/details/124053651