What is C ++? What is the relationship with C language?

xiaoxiao2021-03-06  73

Simply put, C is C plus, plus. That is, it is a super-collection of C language. As a supercharge means that when a good ANSI STANDARD C language program is submitted to a C language compiler, it can be correctly compiled. Typically, *. C file is considered to be C compilation mode, and * .cpp is considered to be C compilation mode.

If you are an experienced C programmer, don't be intimidated by the characteristics of C , because the C language generates the original intention to make C programmers gradually transition to object-oriented programming methods. If you are just a primary C programmer, or you don't have much programming experience, then you need more effort.

C features

First, the C distinguishes that some of the characteristics of C are unrelated to object-oriented objects. (Or said that these features are only more convenient to make C language.) Let's start learning from these features.

New comment style

C defines a new annotation style, i.e., two oblique lines (//) represent a line of comments. Previously, the C program must use / * --- * / to expire. Now you have a better choice. Enjoy this feature!

Variable scope

C has another feature that provides (almost) the ability to declare and use a variable in any location of a function. You don't have to think too much. As the code segment below: #include "iostream.h" // First we define a global variable. INT TEMP; INT Main (int Argc, char * argv []) {// Now we will define an internal variable of the same name. INT TEMP; // You can use this variable below. TEMP = 10; // use global variables. :: temp = 6; // Display the result on the screen. COUT << Temp << "/ n" << :: temp << "/ n"; return 0;} After running, 10, wrap, 6, and wrap are output on the screen. Description :: It is the variable domain operator, :: TEMP represents TEMP in the global variable.

Function overload

The function overload provides a definition and powerful function of the variable. In the C language, if we need a function of two integer maximum, you can write: int MAX_INT (int, int) If we need a function of two floating point maximum, we have defined: float MAX_FLOAT (FLOAT, FLOAT). What kind of type is required? You can imagine how trouble. Now, C allows the same name function, as long as the parameter type or number of parameters you define, the compiler will automatically link. Such as: int MAX (int, int), float max (float, float) ... This feature is quite useful for people who are not very good.

Operator overload

C allows you to overload a operator such as , =, <<, >>, = to accommodate different needs. This feature is very necessary for object-oriented. (Two string objects plus more than 2 3 = 5 plus it ?!) In the previous example, we used COUT << Temp << "/ n" << :: temp << "/ n "; where COUT is a screen object defined in iostream.h," << "has been overloaded in this header file, so that it supports common data types such as integer, floating point, characters, strings. Output, so we can simply use cout << temp to output a variable without indicating type. This feature allows us to lazy. Default parameters

In the C language, the function call must be explicitly transmitted according to the parameter table. C allows the use of default parameters. If a function declares int default (int NUM = 10), we can write DEFAULT () directly when we call, and the compiler will automatically think that we accept Num = 10 parameters. This feature is a lot of parameters, and most of us generally do not need special settings that are very convenient. Note that for a plurality of default parameters, if a parameter is default, the parameters behind them must be default. For design functions, such as void AFUNCTION (int A = 10, char b, int C = 3) is not so smart. In fact, A is not default here. (I want to think about this.) Change to Void Afunction (Char B, INT A = 10, INT C = 3) is fine. After the modification, for the call function, if you want to accept A's default, you must accept the default value of C. In other words, if you want to give C-pass parameters, you must also pass parameters.

Transfer according to the reference

Some features of the C language sometimes not ideal. For example, when it passes the parameters to the function, copy the variable to a copy, then send the copy to the function. On the one hand, for larger types of variables (such as structures or objects), such transmission parameters are clearly efficient. On the other hand, you can only modify the copy inside the function, and the variable itself cannot be changed. In C language, the method of solving this problem is to use a pointer, which is transmitted according to address. The C inherits this feature (the pointer is one of the essence of C / C ), on the other hand, it introduces the concept of "reference". For example: void fn (int & nanothervar) {nanothervar = 10;} int main () {int Navar = 5; // call function. Fn (navar); // Now, Navar's value is 10. } C uses "&" to represent the reference variable (the address of the transfer variable is not a variable value). In this way, the terrible pointer variable is completely avoided. For those who are afraid of pointers, it is really a gospel. However, due to the importance of pointers, such as Windows, UNIX, Linux, the programming interface (API), which is written based on C language, can only use pointers) "reference", can not be brought like other characteristics The person is eye-catching.

Inline function

转载请注明原文地址:https://www.9cbs.com/read-91571.html

New Post(0)