Differences between C ++ and C

Source: Internet
Author: User

CA language is a structured language. It has a clear hierarchy, which facilitates the organization of programs in a modular manner and facilitates debugging and maintenance. To a large extent, standardsC ++It is the superset of Standard C. In fact, all C programs are also C ++ programs. However, there are a few differences between the two. The most important differences are briefly described below.
 

In C ++, local variables can be declared anywhere in a program block. In C, local variables must be at the beginning of the program block, that is, it is declared before all "operation" statements. Please note that this restriction is canceled in the C99 standard.
In C, the function declared as follows does not explain the function variable;
Int func ();
That is to say, if no variable element is specified in the struct orphan behind the function, it means that no declaration is made on the variable element of the function in C. The variable element of the function may or may not be changed. However, in C ++, such a function declaration means that the function has no variable, that is, in C ++, the following two function declarations have the same effect:
Int func ();
Int func (void );

In C ++, the void in the Variable list is optional. many C ++ programmers use them to indicate that the function does not have any variable, so that others can understand the program. however, technically, void is not required.
 

In C ++, all functions must be designed as prototypes, but this is only an option in C. programming experience shows that the prototype design method should also be used for functions in the program. there is an important and subtle difference between C and C ++, that is, the character constant is automatically processed as an integer in C, but not in C ++.
 

In C, although it is not possible to declare a global variable multiple times, it is not an error. In C ++, multiple declarations of the same global variable may cause an error.
 

In C, an identifier can contain at least 31 valid characters.

In C ++, all the characters of an identifier are valid. however, from a practical perspective, long identifiers are not very useful, not only difficult to remember, but also increase the possibility of incorrect typing.
In C, it is not common to call the main () function in a program, but this approach is acceptable. In C ++, this approach is not allowed.
In C, the register address cannot be obtained. In C ++, the address can be obtained.
In C, if no type name is specified in the type declaration statement, the type is assumed to be int. This implicit transformation is not allowed in C99 and C ++.

# Include
Using namespace std;
Why is there no. h suffix?

The iostream name has no extension. h because Is one of the new header files defined by the Standard C ++, and the new C ++ header files no longer use the. h extension.
The second line tells the Compilation Program to use the std namespace. The namespace is recently added to C ++. a namespace creates a declaration range, and various program elements can be placed in this range. namespace is used to help organize the structure of large programs. this using statement notifies the Compilation Program that the programmer needs to use the std namespace, which is the namespace used by the Standard C ++ library declaration. by using the sdt namespace, the program can simplify access to the standard library. normal C Programs only use the C subset and do not need namespaces, because C library functions are available in the default global namespace.

The difference between classes and structs: classes and structs are correlated.

The structure is part of C, and C ++ inherits the structure from C. In terms of syntax, classes and structures are very similar. In terms of relationship, the two are also very close, in C ++, the role of the structure is extended, making the structure an alternative to the class. in fact, the only difference between a class and a structure is that by default, all the members of the structure are public, and all the members of the class are private. in addition, classes and structures are equivalent, that is, a structure defines the type of a class.
C ++ also contains the two equivalent keywords struct and class Based on three reasons. first, strengthen the structure capability. in C, the structure provides a Data grouping method, so it is a small improvement to include a member function in the structure. second, because classes and structures are correlated, it is easier to port all existing C code to C ++. third, because of the equivalence between classes and structures, two different keywords are provided to allow the class definition to develop freely. To maintain the compatibility between C ++ and C, the structure definition must always end with its C definition.
Even if the structure can be used to replace the class in some places, do not do this. For clarity, the class keyword should be used in the place where the class is used, and the struct keyword should be used in the place where the C structure is used.

The class and the Union are interrelated.

Union can also be used to define classes. in C ++, the Union includes member functions, variables, and constructor and destructor. the C ++ Union retains all features of the C Union. The most important feature is that all data elements share the same memory address. similar to the structure, federated members are also public by default and fully compatible with C. like the structure, the joint declaration in C ++ defines a special class, which means that the class encapsulation principle is maintained.
 

The combination of C ++ has several restrictions that must be observed. first, the Union cannot inherit any other types of classes. second, the Union cannot be a base class or contain virtual function members. static variables cannot be associated members. the Union cannot use referenced members, and it cannot have any objects that serve as the member's overload assignment operator. third, if an object contains a clear constructor or destructor, the object cannot become a member of the Union.
 

C ++ has a special union called anonymous union. the anonymous Union does not have a type name or declare any variables, but only tells the Compilation Program that its member variables share a memory address. however, the variable itself can be directly referenced without the need to use the regular dot operator syntax.
 

The preceding Union restrictions apply to anonymous union, except the following two restrictions. First, anonymous Union can only contain data and cannot contain member functions, it cannot contain private or protected elements. Second, the global anonymous union must be declared as static.

Through the introduction in this article, we can understand that C is the basis of C ++, and C ++ and C are compatible in many aspects. Therefore, if you have mastered the C language, and then learn C ++, you can use a familiar syntax to learn the object-oriented language, so as to get twice the result with half the effort.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.