The difference between the number of C + + and C

Source: Internet
Author: User

The C + + language is an extension of the language. So people familiar with the C language will find that the No. 01 to 18th chapter of the book is basically about the content of C language. C + +, on the one hand, modified the syntax of the language and added some new concepts.

The new concepts in C + + are: bool type, reference (Chapter 14), Class (19~24), template (25~26), exception (chapter 27).

C + + has different syntax points for c: Variable dispersion definition, function name overloading, struct syntax. This chapter focuses on a comparative analysis of these changing grammatical points.

This post is selected from the study guide for C + +, appendix 14

 Rights statement: The author owns all the rights of the book. The author authorizes anyone to freely reprint the content posted on this website, but must comply with the following restrictions when reproduced: ① reprint must be reproduced in full, no modification, must contain"Rights Statement"and the"Official Address"② Limited to network reprint, that is, the final results are published on the network. Any non-compliance with the above two of the reprint act as a tort. Except as permitted by me, no person shall use the content of this website for any other purpose.
Official Address: http://www.afanihao.cn/  leave a message, please .http://www.afanihao.cn/kbase/

1.1 Adding C files to the project

When a file is suffixed with. C, the compiler compiles the file in the syntax of the C language, and when the file is suffixed with. cpp, the compiler compiles it in C + + 's syntax standard. Therefore, the suffix name of the file has this Convention, can not be arbitrarily changed.

When adding a file, the suffix of the input file name is. C, which must be followed by the C language syntax when editing this file, as shown in

1.2 Variable dispersion definitions

This is briefly introduced in the 8th chapter of the introduction of functions.

In the C language, all local variables must be defined at the very front of a function or compound statement, simply to say that the variable definition statement is placed before. Otherwise, we will report a grammatical error.

In the following MAIN.C (c) code, the local variable, a, B, is defined in the front of the function body and thus conforms to the syntax requirements of the C language.

MAIN.C//////////#include <stdio.h>void main () {      int A;  Variable definition      int b;  Variable definition       a = ten;      printf ("a=%d \ n", a);       b = one;      printf ("b=%d \ n", b);}

In the following MAIN.C (c) Code, the definition of the local variable C is not in front, does not conform to the C language syntax requirements, and therefore compiles an error,

MAIN.C//////////#include <stdio.h>void main () {      int A;  Variable definition      int b;  Variable definition       a = ten;      printf ("a=%d \ n", a);       b = one;      printf ("b=%d \ n", b);       int C; The variable definition statement cannot be placed here      C = a + B;      printf ("c=%d\n", c);}

Obviously, this is very inconvenient for programmers. The C + + language removes this limitation, allowing programmers to define variables anytime, anywhere.

1.3 Function Name overloading

C + + allows multiple functions to use the same function, which is called the function name overload. And this is absolutely impossible in C language, in C, the global function name can not be duplicated. For example, in the following main.c file, there are two global function do_something with the same name, causing the compiler to error,

Main.c//////////#include <stdio.h> void do_something (int x, int y) {      printf ("int, int \ n");} void do_something (double x, double y) {      printf ("double, double \ n");} void Main () {   printf ("Test");}

The essence of this problem is that in C language, a function name uniquely distinguishes a global function . In other words, the function name is the unique ID of the function, and it is obvious that the ID is not conflicting.

This led to the C language, the name of the function is also a more troublesome thing, for function-like function, because the same name can not be, so have to constantly add some suffixes to show the difference, for example,

Do_something_int_int ()

Do_something_double_double ()

If you read some C code in the future, I believe you will feel it.

C + + 's improvement for this issue is to uniquely differentiate functions by function name + argument list . Therefore, in C + +, the function name is allowed to be the same, as long as the argument list is different and does not conflict.

1.4 struct definition

In the C language, the definition of a struct type must be prefixed with a struct,

In C + +, structs can use their type names directly to define

In contrast, the syntax for C + + should be more concise. So in the C language when writing code, always add a struct prefix will make people feel impatient, so C programmers are usually so to define the struct. In the following code, a typedef is used to define a type of object_t,

In the world of C, the definition of a struct is a common pattern, which is to write less a struct prefix when used. (See how the C programmer has no resistance).

When you need to define a linked list, C programmers have two ways of writing,

struct  object{     int id;     struct Object * NEXT;  }; or typedef struct  _tag_object{     int id;     struct _tag_object * NEXT;} object_t;

Similarly, C programmers will choose the second definition method, the only purpose is to write less a keyword struct when using.

1.5 a struct in C + +

In fact, C + + provides a comprehensive upgrade of structs so that C + + structs are almost entirely equivalent to class syntax. All syntax supported by class, including member functions, Public/private, inheritance, and so on, structs are all supported and the meaning is unchanged. That is, thestruct is synonymous with class (slightly different, as mentioned later).

The only small difference: for a struct, if you do not declare any modifiers (public/protected/private), all members are public by default. This is the opposite of class, Class is private by default.

The difference between the number of C + + and C

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.