Upgrade from C to C ++ to c ++
I. Relationship between C and C ++
1. C ++ inherits all the features of C Language
2. C ++ provides more syntaxes and features based on C.
3. The objective of C ++ is to unify the running efficiency and development efficiency.
C-> C Language
+-> Object-oriented support
+-> Type enhancement and function Enhancement
II. C ++ emphasizes the "practicability" of the language. All variables can be defined as needed.
In C, it is legal to repeatedly define multiple global variables with the same name.
In C ++, multiple global variables with the same name cannot be defined.
Multiple global variables with the same name in C will be connected to the same address space in the global data zone.
C ++ directly rejects this ambiguity.
3. C ++ enhancements to C
1. Processing of const Constants by C ++ Compiler
A. When a constant declaration is encountered, a constant is placed in the symbol table.
B. If constants are found during compilation, they are directly replaced with values in the symbol table.
C. If the extern or & operator is used for const during compilation, the storage space is allocated to the corresponding constant.
Const constants in C language are read-only constants and have their own storage space.
Const constant in C ++
Storage space may be allocated.
# When the const constant is global and needs to be used in other files
# When the & operator is used to obtain the address of the const constant
#include <stdio.h>int main(int argc, char *argv[]){ const int c = 0; int* p = (int*)&c; printf("Begin...\n"); *p = 5; printf("c = %d\n", c); printf("End...\n"); printf("Press enter to continue ..."); getchar(); return 0;}
The output is C = 0;
2. const summary in C ++
Const constants in a and C ++ are similar to macro definitions.
Const int c = 5; approximately equal to # define c 5
Const constants in B. C ++ are different from macro definitions.
Const constants are processed by the compiler and provide type checks and scope checks.
Macro definition is processed by a pre-processor and replaced simply by text.
#include <stdio.h>void f(){ #define a 3 const int b = 4;}void g(){ printf("a = %d\n", a); //printf("b = %d\n", b);}int main(int argc, char *argv[]){ f(); g(); printf("Press enter to continue ..."); getchar(); return 0;}
Output: a = 3; B returns an error.
3. Enhanced struct type
A. the struct in C Language defines a set of variables. The C compiler does not regard this as a new type.
Struct in B and C ++ is a new type definition declaration.
Struct Student
(
Const char * name;
Int age;
)
Student s1;
Student s2;
4. Changes in the register keyword
A. The register keyword requests the "compiler" to store local variables in registers.
Register variable address cannot be obtained in C Language
B. The register keyword is still supported in C ++.
# The C ++ compiler has its own optimization method. It may also be optimized without using register.
# The address of the register variable can be obtained in C ++.
When the C ++ compiler finds that the address of the register variable is required in the program, the register Declaration of the variable becomes invalid (because the C ++ compiler automatically optimizes the variable ).
Early C language compilers do not optimize the code, so the register variable is a good supplement.
5. All variables and functions in C ++ must be of the C type.
A. The default type in C language is invalid in C ++.
F (I)
{
Printf ("I = % d \ n", I );
}
G ()
{
Return 5;
}
B. The function in C language contains a non-argument function.
Void f (void)
The function in C ++ is a nonargument function.
Both void f () and void f (void) are supported.
4. Summary
1. C ++ emphasizes practicality and can declare variables anywhere
2. register in C ++ is only a backward compatible function, and the C ++ compiler can perform better variable optimization.
3. const in C ++ is a real constant instead of a read-only constant.
4. C ++ emphasizes the type, and any program element must display the specified type.