C++ - the difference between C language and C++

Preface

I wonder if students who have studied C language and C++ have ever had such questions - what are the essential differences between these two languages ​​and what are the differences? Anyway, when I finished learning C language and then learned C++, I had this question. It seems that there is no difference at all in the header files, some input and output statements, and most importantly the syntax format in C language. In C++ All can be used normally. The biggest difference I hear between them is this sentence: C language is a process-oriented programming language, and C++ is an object-oriented programming language. I didn't understand the difference between the two at all at the time, and it's only now that I've slowly gained some understanding of the relationship between them.

Start the topic

The difference between C language and C++ header files

At the beginning, I was really confused as to why the header files in C language could be used in C++. I also wanted to verify whether the header files in C++ could be used in C language. I didn't have time to verify it at that time, so I verified it today. C language header files can be used normally in C++, but C++ header files cannot be used in C language.

 

As you can see, when I use the standard input and output header of C++ in a C language file, the compiler reports an error. The error reported is also very simple and easy to understand, that is: there is no such file as iostream, and the compiler terminates.

However, there is no problem at all when calling C language header files in C++ files.

 At this point, I began to think that C language is a subset of C++. What can be used in C language can also be used in C++, but what can be used in C++ cannot be used in C language. This is not a subset. . This is my understanding of the relationship between C language and C++ after verification.

Surprisingly, C language header files can be used in C++, but C++ header files cannot be used in C language. I began to wonder how to distinguish the header files between the two. There seems to be no difference, they are all a string of English letters <>. Until one day, I came across math.h and cmath. I was puzzled at the time. Although the functions in the two header files seemed to be the same, why did they have different names? Later, I finally discovered the problem.

The difference between cmath and math.h: cmath is a library recommended in standard C++, and math.h is an old header file of C language.

It turns out that the early standard library implemented all functions in the global domain and declared them in the header file with the .h suffix. When using it, you only need to include the corresponding header file. In order to distinguish it from the C header file and to use the namespace correctly, it is stipulated The C++ header file does not contain .h; the old compiler also supports the <iostream.h> format, but subsequent compilers no longer support it, so it is recommended to use the <iostream>+std method.

At this point, I know how to distinguish the header files of C language and C++.

Use of namespaces

After distinguishing the differences between header files, I had another question. Why does C++ need to add a using namespace std; while C language can just include it directly? Why do I need to add using namespace std; when I already include the iostream standard input and output files?

This is because in order to avoid the overlap between an identifier we define and an identifier in the standard library, everything in the standard library in C++ (that is, everything in iostream) is placed in the namespace named std. If we only include the relevant header files without expanding the contents in the corresponding space of the standard library, we still cannot use the members of the standard library. This code means that everything in the standard library will be expanded into the global domain. Why use using namespace std; when using #include <iostrem>? Because everything in the file iostream is defined in the namespace std.

At this point, I understand the concept and function of namespaces. Later, when I learned C#, I also defined my own namespace by myself.

The biggest and most essential difference between C language and C++

First of all, C++ is developed on the basis of the C language, so C++ contains all the features of the C language and also introduces some new features. This means that C++ can use all library functions and syntax rules of the C language.

Secondly, C++ is an object-oriented programming language, while C language is a procedural programming language. Object-oriented programming allows developers to organize and manage code using concepts such as classes, objects, inheritance, and polymorphism, which makes C++ more flexible and extensible.

In addition, C++ also introduces some new features, such as templates, exception handling, namespaces, etc. Templates allow developers to write universal code that can operate on different data types; the exception handling mechanism can better handle errors and exceptions in the program; namespaces can help developers organize and manage code and avoid naming conflicts.

In addition, C++ also supports function overloading and operator overloading, which allows developers to define different function or operator behaviors based on different parameter types or operating objects.

In general, C++ is more powerful and flexible than the C language. It provides more features and tools, allowing developers to write complex programs more efficiently. However, for some simple application scenarios, C language may be more concise and efficient. Choosing which language to use depends on your specific needs and project requirements.

Summarize

The above paragraph summarizes the differences in the relationship between C language and C++. In general, C++ is derived from the C language. Although their syntax rules are the same, they are still two different programming languages; and the reason why C++ can use C language header files is because C++ includes C All library functions of the language. The most inaccurate but simplest and clearest thing to say is that C++ adds a lot of new things on the basis of the C language, and also retains all the original old things.

Guess you like

Origin blog.csdn.net/qq_64241302/article/details/132364521
Recommended