Re-learning C++ Notes (1) Basic Introduction

C++ incorporates 3 different programming methods:

  • Procedural language represented by C language
  • C++ is an object-oriented language (OOP, Object Oriented Programming) represented by classes added on the basis of C language
  • C ++ template support for generic programming (Generic Programming)

1. C language

In the early 1970s, Dennis Ritchie of Bell Labs worked on developing the UNIX operating system. In order to accomplish this work, Ritchie needs a language, it must be concise, can generate concise and fast programs, and can effectively control the hardware. Traditionally, programmers use assembly language to meet these needs. Assembly language relies on the internal machine language of the computer. It is a low-level language (low-level), that is, directly operating hardware. Once the hardware is replaced, a different assembly language is required to rewrite the program . So Ritchie developed the C language on the basis of the old language.

C language is a structured language. It contains for loops, while loops, and if else statements. Its principle is top-down. It encourages programmers to develop program units (functions) to represent each Task module.

2. Object-oriented programming

Although the concept of structured programming improves the clarity and reliability of the program, and makes it easy to maintain, it still faces challenges when writing large programs, so OOP came into being. Procedural programming emphasizes algorithms, while OOP emphasizes data .

The OOP programming method first designs the class. In C++, a class is a specification, which describes this new data format, and an object is a data structure constructed according to this specification.

3. C++ and generic programming

Generic Programming is another programming mode supported by C++. It has the same goal as OOP, which is to make it easier to reuse code and abstract common concepts. However, OOP emphasizes programming data, while generic programming emphasizes independence from specific data types. Generic programming needs to extend the language so that you can write only a generic function (rather than a specific type of function) and use it for various actual types. C++ templates provide a mechanism to accomplish this task .

4. The origin of C++

Like the C language, C++ was born at Bell Labs, where Bjarne Stroustrup developed this language in the 1980s. He is the author of the authoritative reference manuals "The C++ Programming Language" and "The design and Evolution of C++".

  • ANSI/ISO has developed an international standard ISO/IEC 14882:1998 after years of hard work, which is called C++98. The standard also extends the existing C++ features, adding exceptions, runtime type identification (RTTI), templates, and standard template library (STL) .
  • In 2003, the C++ standard (ISO/IEC 14882:2003) was released, and this version was called C++03. C++03 does not change language features, so we use C++98 to mean C++98/C++2003.
  • The ISO Standards Committee approved the new standard ISO/IEC 14882:2011 in August 2001, which is called C++11. It adds some new features. This version was also called C++0x.

5. Programming related

5.1 Programming steps

源代码
编译器
目标代码
链接程序
可执行程序
启动代码
库代码

Linking refers to combining the target code with the target code of the function used and some startup codes to generate the running version of the program.

5.2 Extension of source code files

C++ implementation Source code extension
UNIX C、cc、cxx、c
GNU C ++ C、cc、cxx、cpp、c++
Microsoft Visual C++ cpp、cxx、cc

5.3 Unix compilation and linking

UNIX uses CC to compile programs, but it has not been updated since 1993. Today's UNIX computers may not have a compiler, a dedicated compiler, or a third-party compiler, such as GNU g++. Its compilation command is:

CC my.C precious.C

After it is compiled, it will generate the .o file, and then the compiler will automatically pass the object file to the system linker to generate the executable file a.out.

If we only modify the my.C file, we can also use the following commands:

CC my.C precious.o

5.4 Linux compilation and linking

The most commonly used compiler in Linux system is g++, which comes from the GNU C++ compiler of Free Software Foundation. Most versions of Linux include this compiler, but it is not necessarily installed. The g++ compiler works very much like a standard UNIX compiler. E.g.

g++ my.cxx precious.cxx

This will generate an executable file named a.out and two object code files my.o and precious.o. If the my.cxx is modified next. You can use the following command to compile.

g++ my.cxx precious.o

5.5 windows command line compiler

Both Cygwin and MinGW include the compiler GNU C++, which can be downloaded for free. The compiler they use is called g++. To use the g++ compiler, you first need to open a command prompt window. When you start the programs Cygwin and MinGW, they will automatically open a command prompt window for you. Its compilation command is as follows:

g++ great.cpp

If the compilation is successful, the executable file name is a.exe

6. Other basic introduction

6.1 Notes

//单行注释,C++风格注释

/*
多行注释
C风格注释
*/

6.2 Header file name

Take the following header file as an example. The function of the header file is to replace the code line #include<iostream> in the program with the content of the iostream file.

#include<iostream>

Header file naming convention:

Header file type Agreement Instance Description
C++ old style End with .h iostream.h C++ programs can be used
C old style End with .h math.h C and C++ programs can be used
C++ new style No extension iostream C++ program can be used, use namsspace std
Converted C Prefixed with C, no extension cmath C++ programs can be used, and features that are not C can be used, such as namespace std

Note that if you use iostream instead of iostream.h, you should use the following namespace compilation directives to make the definitions in iostream available to the program:

using namespace std

The header file can also use some special extensions (such as .hpp or .hxx)

6.3 Keywords

Insert picture description here


Overview Catalog
Previous: None
Next: (2) Processing Data


Article reference: "C++ Primer Plus Sixth Edition"

Guess you like

Origin blog.csdn.net/QLeelq/article/details/112258203