1) Foreword
In 1979, Benjani of Bell Labs and others tried to modularize the unix kernel when they tried to analyze the unix kernel, so they extended it on the basis of C language, added the mechanism of classes, and completed a runnable preprocessor called C with classes.
C++98: the first version of C + + standard, which is supported by most compilers. It has been recognized by the international organization for Standardization (ISO) and the American Institute for standardization. It rewrites the C + + standard library in the form of template and introduces STL (Standard Template Library)
C++11: many features have been added to make C + + more like a new language, such as regular expression, range based for loop, auto keyword, new container, list initialization, standard thread library, etc
C++14: the extension of C++11 is mainly to repair the loopholes and improvements in C++11, such as generic lambda expressions, auto return value type derivation, binary literal constants, etc
In addition, there are C++1.0, 2.0, 03, 05, 17, 20, etc., but they are not so important
2) C + + keywords
There are 63 keywords in C + +, which is equivalent to doubling compared with 32 keywords in C (compatible with C keywords)
asm | do | if | return | try | continue |
auto | double | inline | short | typedef | for |
bool | dynamic_cast | int | signed | typeid | public |
break | else | long | sizeof | typename | throw |
case | enum | mutable | static | union | wchar_t |
catch | explicit | namespace | static_cast | unsigned | default |
char | export | new | struct | using | friend |
class | extern | operator | switch | virtual | register |
const | false | private | template | void | true |
const_cast | float | protected | this | volatile | while |
delete | goto | reinterpret_cast |
3) namespace
Many of the same variable, function and class names will exist in the global scope, which may lead to many conflicts. The purpose of using namespace is to localize the name of identifier to avoid naming conflict or name pollution. The emergence of namespace keyword is aimed at this problem
1. Use of namespace
The following is the general namespace usage
namespace My_Space // My_Space is the name of the namespace { // Namespace, you can define either variables or functions int a; int Add(int left, int right) { return left + right; } }
Note 1: namespaces can be nested
namespace My_Space1 { int a; int b; int Add(int left, int right) { return left + right; } namespace My_Space2//nesting { int c; int d; int Sub(int left, int right) { return left - right; } } }
Note 2: multiple namespaces with the same name are allowed in the same project, and the compiler will finally synthesize them into the same namespace
namespace My_Space // My_Space is the name of the namespace { // Namespace, you can define either variables or functions int a; int Add(int left, int right) { return left + right; } } namespace My_Space // My_Space is the name of the namespace { // Namespace, you can define either variables or functions int b; int Mul(int left, int right) { return left * right; } }
amount to
namespace My_Space // My_Space is the name of the namespace { //upper int a; int Add(int left, int right) { return left + right; } //lower int b; int Mul(int left, int right) { return left * right; } }
2. Usage of namespace
1. Add namespace name and scope qualifier '::'
int main() { printf("%d\n", My_Space::a); return 0; }
2. Use using to introduce members in the namespace
using My_Space::b; int main() { printf("%d\n", b); return 0; }
3. Use the using namespace namespace name to import
using namespce My_Space; int main() { printf("%d\n", My_Space::a);//You can printf("%d\n", b);//It can also be used directly Add(10, 20); return 0; }
4) cout and cin
be careful:
When using cout standard output (console) and cin standard input (keyboard), the < iostream > header file and std standard namespace must be included
Explanation for the absence of '. h' in C + +:
Early standard libraries implemented all functions in the global domain and declared in the header file of. h suffix. Later, in order to distinguish from the C header file and correctly use the namespace, the C + + header file is not provided with. h. The old compiler (vc 6.0) also supports iostream.h format, which is no longer supported by subsequent compilers, so the method of < iostream > + std should be used
Basic usage (rough understanding)
cout is equivalent to printf
cin is equivalent to scanf
endl is equivalent to newline '\ n'
#include <iostream> using namespace std; int main() { int a; double b; char c; cin>>a; cin>>b>>c; cout<<a<<endl; cout<<b<<" "<<c<<endl; //Unlike printf, the output stream symbol < < must be used to connect here return 0; }
5) Default parameters
Definition: parameters can be passed or not when calling a function. If not, the actual parameters specified by the function are used, as shown in the example:
void TestFunc(int a = 0) { cout<<a<<endl; } int main() { TestFunc(); // When no parameter is passed, the default value of the parameter is used TestFunc(10); // When passing parameters, use the specified arguments }
1. All default parameters
void TestFunc(int a = 10, int b = 20, int c = 30);
2. Semi default parameters
void TestFunc(int a, int b = 10, int c = 20);
be careful:
- Semi default parameters must be given from right to left, and cannot be given at intervals
- Default parameters cannot appear in both function declarations and definitions
(if the declaration and definition locations appear at the same time, and the values provided by the two locations happen to be different, the compiler cannot determine which default value to use) - The default value must be a constant or a global variable
- C language not supported (compiler not supported)
6) Function overloading
Definition: function overloading is a special case of functions. C + + allows to declare several functions with the same name with similar functions in the same scope. The formal parameter list (number of parameters or type or order) of these functions with the same name must be different. It is commonly used to deal with the problems of similar functions and different data types
The following are function overloads
int Add(int left, int right) { return left+right; } double Add(double left, double right) { return left+right; } long Add(long left, long right) { return left+right; } int main() { Add(10, 20); Add(10.0, 20.0); Add(10L, 20L); return 0; }
The following are not function overloads
short Add(short left, short right) { return left+right; } int Add(short left, short right) { return left+right; }
1. Why does C + + support function overloading and C language does not support function overloading?
The operation of the program needs to go through: preprocessing, compilation, assembly and linking
reference resources: C language -- program compilation (preprocessing)
Under Linux: (suppose a.cpp calls the Add function defined in b.cpp).
In the link phase, when the linker sees that a.o (binary file) calls Add, but there is no address of Add, it will find the address of Add in the symbol table of B.O (binary file) and link it together
The assembly instruction call is usually used when calling a function
See: Creation and destruction of function stack frames (detailed)
For example: 80489bc: E8 73 FF call <? > (question mark is the function signature)
Each compiler has its own function name modification rules
gcc compiler:
Using the objdump tool, type the objdump -S source file
The function signature is still Add
g + + compiler:
The same operation shows that the function signature becomes<_ Z3addii > instead of the original function name Add
Here we can conclude:
Under linux, after g + + compilation, the modification of the function name changes. The compiler adds the function parameter type information to the modified name to form the function signature
Back to the link phase
If there are two Add functions in b.cpp, the gcc compiler will find that there are two function signatures with duplicate names in the symbol table, so it will directly report an error because it does not know which address to take out. But g + + makes it clear that the signatures of two functions with the same function name are different, so you can easily get the target address
In this way, it can also explain that the above is not a function overload (independent of the return value)
Under Windows:
For C language