如上图那样,C++,英文为c plus plus,它是C的超级,C++支持运行大部分C代码,而反过来C却不可以;
这里例举了它们之间的主要10个不同点(为了不偏离原本的语意,直接上英文了):
1、C follows the procedural programming paradigm while C++ is a multi-paradigm(多范式) language(procedural as well as object oriented)
In case of C, importance is given to the steps or procedure of the program while C++ focuses on the data rather than the process.
Also, it is easier to implement/edit the code in case of C++ for the same reason.
2、In case of C, the data is not secured while the data is secured(hidden) in C++
This difference is due to specific OOP features like Data Hiding which are not present in C.
3、C is a low-level language while C++ is a middle-level language (Relatively, Please see the discussion at the end of the post)
C is regarded as a low-level language(difficult interpretation & less user friendly) while C++ has features of both low-level(concentration on whats going on in the machine hardware) & high-level languages(concentration on the program itself) & hence is regarded as a middle-level language.
Note: This is a relative difference. See updates at end of this post.
4、C uses the top-down approach while C++ uses the bottom-up approach
In case of C, the program is formulated step by step, each step is processed into detail while in C++, the base elements are first formulated which then are linked together to give rise to larger systems.
5、C is function-driven while C++ is object-driven
Functions are the building blocks of a C program while objects are building blocks of a C++ program.
6、C++ supports function overloading while C does not
Overloading means two functions having the same name in the same program. This can be done only in C++ with the help of Polymorphism(an OOP feature)
7、We can use functions inside structures in C++ but not in C.
In case of C++, functions can be used inside a structure while structures cannot contain functions in C.
8、The NAMESPACE feature in C++ is absent in case of C
C++ uses NAMESPACE which avoid name collisions. For instance, two students enrolled in the same university cannot have the same roll number while two students in different universities might have the same roll number. The universities are two different namespace & hence contain the same roll number(identifier) but the same university(one namespace) cannot have two students with the same roll number(identifier)
9、The standard input & output functions differ in the two languages
C uses scanf & printf while C++ uses cin>> & cout<< as their respective input & output functions
10、C++ allows the use of reference variables while C does not
Reference variables allow two variable names to point to the same memory location. We cannot use these variables in C programming.
MORE –
11、C++ supports Exception Handling while C does not.
C does not support it “formally” but it can always be implemented by other methods. Though you don’t have the framework to throw & catch exceptions as in C++.
12、Implicit Assignment from void*
You cannot implicitly assign from a void* to any other type. For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C)
int *x = malloc(sizeof(int) * 10);
but it won't compile in C++. (Try it yourself!)
The explanation from Bjarne Stroustrup himself is that this isn't type safe. What this means is that you can have a void that points to anything at all, and if you then assign the address stored in that void to another pointer of a different type, there isn't any warning at all about it.
Consider the following:
int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;
When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes. Forcing a cast from a void pointer makes the programmer pay attention to these things.
13、Freeing arrays: new[] and delete[]
In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:
int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );
and you always release the memory in the same way:
free( x );
free( x_array );
In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).
int *x = new int;
int *x_array = new int[10];
delete x;
delete[] x_array;
The short explanation is that when you have arrays of objects, delete[] will properly call the destructor for each element of the array, whereas delete will not.
14、You must declare functions before use
Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:
#include <stdio.h>
int main()
{
foo();
return 0;
}
int foo()
{
printf( "Hello world" );
}
15、Structs and Enums
You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this
struct a_struct
{
int x;
};
a_struct struct_instance;
and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance:
struct a_struct struct_instance;
In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs:
typedef struct struct_name
{
/* variables */
} struct_name_t;
Now you can declare a struct with
struct_name_t struct_name_t_instance;
But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a pointer to the struct.
typedef struct struct_name
{
struct struct_name instance;
struct_name_t instance2; /* invalid! The typedef isn't defined yet */
} struct_name_t;
16、C++ has a much larger library
C++ has a much larger library than C, and some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:
% g++ foo.cc
or
% gcc foo.c -lm
17、No Boolean Type
C does not provide a native boolean type. You can simulate it using an enum, though:
typedef enum {FALSE, TRUE} bool;
18、main Doesn't Provide return 0 Automatically
In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:
int main()
{
printf( "Hello, World" );
}
but in C, you must manually add it:
int main()
{
printf( "Hello, World" );
return 0;
}
【参考文献】
1、http://www.cprogramming.com/tutorial/c-vs-c++.html
2、http://www.durofy.com/10-major-differences-between-c-and-c/