You can say c ++ language c based on the expansion of a number of learning after learning to play c language c ++ will find easier but c ++ is also superior to c where c ++ and c language are all native compiled language, directly compiled to native compiled code, run particularly fast.
c ++ OPP object-oriented language different from the java python PHP script (interpretive) language which must have a script interpreter, running on the local operating system
Therefore, the difference between c and c ++ The following summary
1) c ++ support with default parameters
1 functions to a default parameter value, from right to left to void fun (int a, int b = 10) ✅ void fun (int a = 10, int b) ❌
2 to the function parameter may be defined at a time when the declared
And a function without the function of a default value with a default value of 3 with differences in the efficiency of call
int sum(int a = 10, int b = 20); int sum(int a, int b) { return a + b; } int main() { sum(10,10); 00FE142E push 0Ah 00FE1430 push 0Ah 00FE1432 call sum(0FE105Fh) 00FE1437 add esp, 8 sum(); 00FE143A push 14h 00FE143C push 0Ah 00FE143E call sum(0FE105Fh) 00FE1443 add esp, 8 int a = 10; int b = 10; sum(a, b); 00FE1446 mov dword ptr[a], 0Ah 00FE144D mov dword ptr[b], 0Ah 00FE1454 mov eax, dword ptr[b] 00FE1457 push eax 00FE1458 mov ecx, dword ptr[a] 00FE145B push ecx 00FE145C call sum(0FE105Fh) 00FE1461 add esp, 8
Substantially sum (10,20); and SUM (); there is no difference or a given parameter default values are used for a push operation, the CPU calls instruction PUSH
The int a = 10, b = 20; sum (a, b); efficiency slower than the above, the memory will take a go, the value of b increases mov instruction
2) c ++ function overloading support
1 c language support overloading Why not?
Since c language compiler symbol generating function, and therefore only defined in terms of generating a plurality of function names can not be the same as the function name of the function
E.g. void fun () _fun
While c ++ compiler generated function symbol is generated in accordance with the function name and a function parameter, the name of the same set of functions different parameter list (number of parameters, different types of parameters) can be configured overloaded function
bool compare(int a, int b) bool compare(double a, double b) bool compare(const char* a, const char* b)
2 call overloaded function to determine at what time, compile phase or stage? ? ?
Determining a logical address overloaded function call instruction requires functions at compile
3 appreciated polymorphism
Static (compile-time) polymorphism: function overloading, template
Dynamic (runtime) Polymorphism: virtual function
Therefore, the function symbol will need to use a unified naming function in c ++ c consideration
First call c in c ++ interface should be put under the c interface extern "c"
C ++ code to call in c cumbersome interfaces To meet the c language put all extern "c" {} in
In the multi-file project, the project will be c function declaration file all declarations to #ifdef __cplusplus expand this macro, you can give other c / c ++ project uses
3) the difference between the dynamic application space malloc / free c ++ new / delete
1 malloc is a C library function and the new operator is
2 malloc memory is byte application, when using the type of the return value to be strong new application revolutions to specify the type of memory, so the value returned turn requiring a strong
3 malloc memory not only responsible for application initialization: The new application can be completed with memory and initialization
4 malloc allocate memory failed to return nullptr pointer; and the new application failed to throw an exception, only by capturing
5 new underlying fact is by malloc to allocate memory, but optimized with malloc
The difference between free and delete the
malloc open two-dimensional array of new open two-dimensional array
#include<iostream> #include<stdlib.h> #include<string.h> #include<malloc.h> using namespace std; int main() { int **p=(int **)malloc(sizeof(int*)*3); int i=0; for(;i<3;i++){ p[i]=(int *)malloc(sizeof(int)*4); } int **s=new int*[3]; for(i=0;i<3;i++){ s[i]=new int[4](); } for(i=0;i<3;i++){ free(p[i]); } free(p); for(i=0;i<3;i++){ delete[]s[i]; } delete []s; cout<<endl; return 0; }
1 delete to free up space and release calls the destructor; immediate release and free
2delete operator is free and is a library function