Learning log: Talking about the similarities and differences between C language and C++

Table of contents

1. Introduction

Two, the difference 

1. Object-oriented and process-oriented

2. Grammatical Differences - Quoting

3. Keyword aspect

4. In terms of suffixes

5. About const

 6. About the application and release of memory space

7. The new data type bool

8. Function thinking, function overloading...

1. Introduction

I signed up for the Blue Bridge Cup this year. During the learning process, I found that the programming language of many tutorials is C++. Although I have learned C language in my freshman year, I still feel a little unaccustomed to it. I am determined to learn C++ first-hand.

In the first semester of freshman year, I learned the basics of programming - C language, an object-oriented programming language;

In the next semester of my freshman year, I learned object-oriented programming--Java. Only then did I slowly understand the difference between object-oriented and process-oriented!

"Long-awaited name", C++ not only inherits the characteristics of process-oriented programming of C language, but also is an object-oriented programming language with comprehensive functions~~

Two, the difference 

1. Object-oriented and process-oriented

First of all, it is the point mentioned above: C language is a process-oriented programming language, and C++ is an object-oriented programming language, which is more widely used in practice.

2. Grammatical Differences - Quoting

Before learning C++, what I was most concerned about was the difference in syntax, because it was mainly because of obstacles in understanding the code. As for the learning channel, it is mainly through watching the courses of the dark horse programmers of the station, which is very good~

The most impressive thing is the reference of C++. During the process of learning C language in the freshman year, I did not have any knowledge of this aspect.

The following are relevant knowledge points:

(1) Introduction to Citation

The transfer of parameters is essentially a process of assignment. For basic types of data such as char, bool, int, and float, the memory they occupy is usually only a few bytes, and memory copying them is very fast.

Arrays, structures, and objects are collections of a series of data. There is no limit to the amount of data, which may be few or tens of thousands. Frequent memory copying of them may consume a lot of time and slow down the execution efficiency of the program. .

In C language, when passing this kind of aggregation type data, it is recommended to pass pointers. But pointers are cumbersome to use and less readable.

Therefore, C++ has added a more convenient way to transfer aggregate type data than pointers-references.

Like pointers, references can reduce data copying, improve data transfer efficiency, and are more convenient and easy to use than pointers.


(2) Grammatical format of references

type &name = data;
for example:

int a = 10;
int &p = a;

A reference can be regarded as an alias of data, and the data can be found through this alias and the original name, and the operation on the reference is equivalent to the operation on the data.


(3) Citation rules

1. The reference is just an alias and does not occupy memory; it shares the same memory space with the variable it refers to;

2. References are only defined with &, and used as ordinary variables without &;

3. The reference must be initialized when it is created. Once the reference is initialized, the variable pointed to by the reference cannot be changed, similar to a pointer constant.

4. The reference must be associated with a certain legal memory unit, and there is no NULL reference and cannot be used;

(4) Referenced applications

4.1 Common references

The data of the reference space cannot be modified through the alias, and the modified parameter can prevent the misoperation of the reference space in the function;
format:
const data type & alias = data space name;
const data type & alias

4.2 References and arrays

①The type of array element (& alias) [number of elements] = array name;

② Use typedef to alias the type of array element [the number of elements], and then use the new type name to define the reference

4.3 *References act on pointers

Alias ​​the pointer variable, C language uses a secondary pointer to point to a pointer, and then operate on the pointer space, C++ can alias the pointer to operate on the pointer space.


4.4. References as function parameters

//Reference
//Modify the data in the outer space of the function inside the function
//Do not generate a copy of the parameter
//It is easier to use than a pointer

Note: If you use a reference as a parameter and do not want to change the content of the reference space inside the function, you can add const modification in front of the reference.


4.5. Reference as a function return value - generally used for iteration

Note: Do not return a reference to a local variable, because the local variable will be destroyed after the function returns, so the returned reference becomes a "nothing" reference, and the program enters an unknown state.

(5) The nature of the citation

The essence of reference is pointer constant (constant pointer) in C++, typedef& ref = val; //typedef* const ref = &val;

The reference is just a simple encapsulation of the pointer, and its bottom layer is still implemented through the pointer. The memory occupied by the reference is the same as the memory length occupied by the pointer, which is 4 bytes in the 32-bit environment and 8 bytes in the 64-bit environment. bytes, the reason why the address of the reference cannot be obtained is because the compiler performs an internal conversion. 

Summary: It is not that the variable p does not occupy memory, but that the compiler does not allow it to obtain its address. 


(6) The difference between references and pointers

1. References are more convenient and readable than pointers;

2. References do not occupy memory, pointers occupy memory;

3. The referenced value cannot be NULL, and the value of the pointer can be NULL;

4. The meanings of the self-increment (++) and self-decrement (--) operations of pointers and references are different. Use ++ for pointers to point to the next piece of data, use ++ for references to add 1 to the data it refers to; self-decrement (--) is similar.


3. Keyword aspects

After reviewing relevant textbooks, it was found that

(1) C language has 32 keywords (2) C++ has 63 keywords

4. In terms of suffixes

When I was doing data structure experiments last semester, I was already using C++ files for convenience

(1) C language: .c 

(2)C++:  .cpp

5. About const

Variables modified by const in C language are not constants, called constant variables or read-only variables. This constant variable cannot be used as an array subscript. However, variables modified by const in C++ can be used as array subscripts and become real constants. This is the extension of C++ to const.

const in C language: After being modified, it cannot be used as an lvalue, and it can not be initialized, but there is no chance to reinitialize afterwards. It cannot be used as the subscript of the array, but can be modified through the pointer. Simply put, the difference between it and ordinary variables is that it cannot be an lvalue. Everywhere else is the same.

const in C++: real constants. It must be initialized when it is defined, and can be used as the subscript of the array. The compilation rule of const in C++ is replacement (much like a macro), so it is regarded as a real constant. It can also be modified by pointer. It should be noted that C++ pointers may degenerate into C language pointers.

When const generates symbols, it is a local symbol. That is, it is only visible in this document. If you have to use it in other files, declare in the file header: extern cosnt int data = 10; the generated symbol is a global symbol.

Summary: const in C is called a read-only variable, but it cannot be an lvalue variable; const in C++ is a real constant, but it may degenerate into a constant in C language, and local symbols are generated by default.

 6. About the application and release of memory space

malloc() and free() are functions in the standard library that dynamically apply for memory and release memory in C language. And new and delete are C++ operators and keywords. The bottom layer of new and delete actually calls malloc and free. They differ in the following ways:

1), malloc and free are functions, new and delete are operators.

2), malloc needs the size before allocating memory, new does not.

When malloc needs to specify the size, it also needs type conversion. There is no need to specify the size when new because it can be judged from the given type, and the initial value can also be assigned at the same time.

3) Malloc is not safe and requires manual type conversion, while new does not require type conversion.

See the previous article for details.

4), free only releases the space, delete calls the destructor first and then releases the space (if necessary).

Corresponding to item 5, if a complex type is used, first destruct and then call operator delete to reclaim the memory.

5), new is to call the constructor first and then apply for space (if necessary).

Corresponding to item ④, when we call new (for example, int *p2 = new int; this code), the implementation of the underlying code is: first push 4 bytes (size of int type), and then call operator new function allocation out of memory. Since our code does not involve complex types (such as class types), there is no constructor call.

6) The processing method is different when the memory is insufficient (development failure).

If malloc fails, it returns 0, and if new fails, a bad_alloc exception is thrown.

7), new and malloc open up memory locations are different.

malloc is allocated in the heap area, and new is allocated in the free storage area.

8), new can call malloc(), but malloc cannot call new.

new is implemented with malloc(), and new is unique to C++, and of course malloc cannot be called.

In short, there is a close relationship between the differences

7. The new data type bool

The occupied memory is one byte. If the computer is non-zero, it means it is established. If it is only 0 or the pointer is empty, it means it is not established. Generally, it is used as the return value of the function, or the normal output of the switch is 0 and 1. The C++ proprietary assignment method, false and true

8. Function thinking, function overloading...

Learning is not over yet, sharing will continue~

Guess you like

Origin blog.csdn.net/m0_64198455/article/details/128583485