C + + Programming thought learning Diary 1-----C and C + + some differences

Source: Internet
Author: User

recently in the C + + programming idea of this book, so I intend to see what I saw every day to record some of the things.

It is well known that C + + is developed from the C language, there are many similarities between them, and there are many different places. If you want to ask the difference between C and C + +, many people will say that C is a process-oriented language, and C + + is object-oriented. There are class in C + +, templates and so on. But there are many places in C + + and C where usage is basically the same, but what are the same things that are different?

1. Functions without parameters

If I ask a question like this: int func (), what is the difference between C and C + + in this function, what does it say? For the meaning of this function, everyone will say that it is a function with no parameters and a return value of int. In C + +, of course, there's no problem with that. But there will be a little bit of a difference in the C language. The meaning of the C language for functions like this is that the return value is int, a function with an indeterminate parameter. A bit similar to ... Variable parameters. If declared as int func (void), there is no ambiguity, and in C and C + + it represents a function with a return value of int, with no arguments.

2. Casting

For casts, everyone is familiar with, for example:

int a = 10;

Double b = (double) A;

Above is the forced type conversion in the C language.

In C + +, in order to be compatible with the C language, while preserving the above conversion method, the new one is added a similar function call conversion mode, for example:

int a = 10;

Double b = double (a);

The difference between these two conversions is that one is to enclose the type in parentheses, and the other is to enclose the variable in parentheses.

In C + +, of course, a display conversion syntax has been introduced: static_cast, Const_cast, reinterpret_cast, dynamic_cast.

Statement of the 3.struct

struct, struct definition everyone will, for example:

struct ST_XXX

{

int A;

float B;

};

However, after defining a struct in C, it is relatively cumbersome to define an object of a struct, which must precede the struct name with a struct, as follows:

struct ST_XXX ST;

Another way to define this is to use a TypeDef, as shown below:

typedef struct

{

int A;

float B;

}st_xxx;

In this case, when you define an object, you can remove the previous struct, for example:

St_xxx St;

Of course the second way is still more troublesome, because the typedef must be used. Of course this is only in the C language, and in C + + There is no such problem. C + + is like a combination of the above two ways, for example:

struct ST_XXX

{

int A;

float B;

};

St_xxx St;

4. Determination of Authenticity

There is no variable of type bool in C, so the true and false judgment is determined by whether it is zero.

5. Enum type enum

For an enumerated type that has been declared, for example:

Enum Color

{

Red

Green

Blue

};

Color A;

For the statement that executes a++ for variable C, it is possible in the C language. Because the enumeration in the C language is simple, it simply links an integer value to a name. In C + + for enumerations, the enumeration is looked at by a new type, and the type checking of the enumeration is stricter than in C. A two-type conversion is performed when the a++ statement is executed. The first converts the A from the color of an enumeration to an int (which is legal in C + +), and then the A + + operation after the conversion into int. The increment a is then converted to a color type (which is not legal in C + +).

There are some differences between the enumerated types in C and C + +, and you need to pay more attention when using them.

C + + Programming thought learning Diary 1-----C and C + + some differences

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.