C++ Tutorial - How to C++ Series Column Part 2

about column

This column is a high-quality C++ tutorial column, if you haven't read the first one, click here to go to the first one

This column consistently uses operating system: macOS Ventura, code editor: CLion, and C++ compiler: Clang

Thanks to the supportive friends, the last article became the first in the field of C/C++! No. 64 on the comprehensive hot list! (It is still improving), and it was also included in Geek Daily's August 6th worth watching today! The author also became the 61st place in Beijing Force (still improving) ^ _ ^

The blogger’s feedback is very timely. If you encounter problems while reading, you can directly ask questions in the comment area, and the blogger will give you feedback within 24 hours after seeing it

C++ Tutorial - How to C++ Series Column Part 2


Table of contents

about column

update record

August 6, 2023

foreword

C++ preprocessor and header files

preprocessor

head File

namespace 

cout

cout

endl

C++ source code format

epilogue

quick page turning

This article references articles, books


update record

August 6, 2023

Publish the first article


foreword

What is a preprocessor? What are header files? Can I eat it? What are namespaces? Where is it named? source code format? Laugh dead, write code and format?


C++ preprocessor and header files

preprocessor

If the program uses C++ input or output tools, please provide these two lines of code

#include <iostream>
using namespace std;

Line 2 can be replaced with other code, it is used here to simplify the program. That's all you need to know for the program to work. These are covered in more depth below.
C++, like C, uses a preprocessor that processes source files before the main compilation, that is, it processes compilation directives whose names begin with # and does not have to do anything special to call The preprocessor , which runs automatically when the program is compiled. The #include compilation directive
was used in the previous code

#include <iostream>

This pragma will add the contents of the iostream file to the current program, which is a typical preprocessor operation: before the source code is compiled , replace or add text

However, why should iostream files be added to the program?

Because the program needs to communicate with the outside, io in iostream refers to input ( input ) and output ( output )

The C++ input/output scheme involves many definitions in the iostream file that the first program needs in order to use cout to display messages. The #inciude compilation directive sends the contents of the iostream file to the compiler along with the contents of the source code file . In effect, the contents of the iostream file will replace the line #include <iostream> in the program . The original file is not modified, but the source code file and iostream are combined to form a compound file, which is used in the next step of compilation .

head File

Files like iostream are called header files because they are included at the beginning of the article. The C++ compiler comes with many header files , and each header file has its own tool. The tradition of the C language is: header files use The extension .h , as a convenient way to represent the file type, such as math.h file can provide many mathematical functions for C

However, the usage of C++ has changed

Old-style C header files are still available, but a considerable number of header files have been converted to C++ header files. These files have been renamed and prefixed with "c" to indicate that they are from C. For example: the C++ version of math.h The header file becomes cmath .

For pure C++ header files, such as iostream , removing h is not only a change in the name, but may also include a namespace

header file name
header file type form example illustrate
C++ old style end with .h iostream.h C++ programs can use
C old fashioned end with .h math.h C and C++ file headers can be used
C++ new style no extension iostream

C++ programs can use

Converted C

no extension

prefix with c

cmath

C++ files can be used, and features that are not C can be used


namespace 

If iostream is used instead of iostream.h , the following namespace pragma must be used to make the definitions in iostream available to the program

using namespace std;

This is the using compilation directive , we will explain it later, now let's briefly explain what a namespace is

Namespaces are a C++ feature that make it easier to write large programs and programs that combine existing code from multiple vendors and help organize programs

A potential problem we may be dealing with in the future:

It is possible to use two packaged products, and they both contain a function called hello() . That way, when using the hello() function , the compiler won't know which version to refer to. Namespaces enable vendors to package their products in a unit called a namespace , so that you can use the name of the namespace to indicate which vendor's product you want to use

Therefore, vendor A can put its definition into a namespace named A, so that the full name of its hello() function is A::hello();

Similarly, vendor B's version of hello() can be expressed as B::hello()

In this way, the program can use the namespace to distinguish different versions:

A::hello("hello?");
B::hello("hello!");

In this way, classes , functions , and variables are standard components of the C++ compiler , and they are now placed in the namespace std. This is only the case if the header file does not have the extension .h . This means that the cout variable defined in iostream for output is actually std::cout and endl is actually std::endl . So we can omit the compilation instruction using and write it in the following form

std::cout << "How to C++" << std::endl;

Of course, there is no need to use " std:: " after using using namespace std;

Another way is to use each function individually

using std::cout;
using std::endl;

In this case, there is no need to add " std:: " when using cout


cout

cout

A piece of output code of first.cpp is as follows:

cout << "This is my first C++ program!" << endl;

 The part enclosed in double quotes is the message to be printed. In C++, a series of characters enclosed in double quotes is called a string because it is composed of many characters

The "<<" symbol indicates that the statement will send this string to cout; this symbol indicates the path of information flow

What is cout ?

It is a predefined object that knows how to display strings, numbers, and single characters , etc.
Difficult because objects are not introduced until a few chapters later. This demonstrates the strength of objects: you can use them without knowing their internals. Just need to know its interface and how to use it

The cout object has a simple interface, if a is a string, the following code will display the string:

cout << a;

For subdisplay strings, just know this

Now let's see how C++ conceptually explains this process: Conceptually, the output is a stream, which is a bunch of characters flowing out of the program. The cout object represents this stream, and its properties are defined in the iostream file. The object properties of cout include an insertion operator << , which can insert the information on its right side into the stream, such as the following example

cout << "欢迎订阅专栏";

It inserts the string "Welcome to the Subscribe Column" into the output stream. So instead of the program displaying a message, it inserts a string into the output stream

endl

The meaning of endl is: restart a line, move the cursor to the beginning of the next line

From the perspective of hardware (such as a monitor), input/output is very time-consuming, so the program will temporarily store the things that are usually written into cout , and will not display them immediately. The cached data will be displayed to the console only when a certain amount is stored, or when endl is received

So it can be understood as: end line

C++ also has a newline method: " \n "

"\n" cannot be regarded as two characters, it is actually a character and exists as a whole, but "\n" does not have the function of endl to refresh the cache in cout , so we generally use endl , when using " \n "You need to add double quotes, such as the following:

cout << "快点赞收藏关注" << "\n";

C++ source code format

In some languages, a line is regarded as a statement, such as Python. When bloggers transition to C++, they are not used to semicolons. There will always be a problem of missing semicolons at the end. A statement in C++ is determined by a semicolon. So your program could also be written like this:

#include <iostream>

int
main
        (void)
{
    using
    namespace
            std; cout
            <<
            "一定要点赞收藏关注"
            << endl;
    cout
            << "订阅专栏哦";
    cout
            <<
            endl;
    return
            0;}

Although this will be scolded [doge], it can still be compiled (the blogger tried it himself, and you can try it too), but you must not add spaces or line breaks between elements, for example:

i nt mai n (void)
---
re
turn 0;
---
cout << "点赞收藏
关注";

It is recommended that you abide by the following code style:

  1. Each statement occupies one line
  2. There must be a curly brace at the beginning and end of the function, and the curly braces occupy one line each
  3. The statements in the function should be indented relative to the curly braces (4 spaces or a tab)
  4. no whitespace around parentheses relative to function names 

epilogue

The author will often help friends with problems to solve problems. If you have any questions, you can directly point them out in the comment area, and the author will reply within 24 hours after seeing them

If you have any questions about this article, please point it out in the comments. If you like this article, please like, comment, and follow

If there are people around you who have mentioned this field like you, or hope to make progress together with him, please share this article with him

3762 words, 10 parents, 5 children


quick page turning

Previous

Other high-quality blogger articles:

Hexo tutorial, just read this one - How to series

Apple WWDC23 Developer Conference - Apple's ambitions

How to write high-quality blogs in CSDN - How to series


This article references articles, books

reference article

For the use of C++ cout, it is enough to read this article-Knowledge

What is the function of << in std::cout <<? - Know almost

What is the essence of endl in C++_c++ endl_xiaofei0859's blog-CSDN Blog

reference books

C++ Primer Plus

Guess you like

Origin blog.csdn.net/cat_bayi/article/details/132132360