Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between C and C++?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what's the difference between C and C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

C + + is C with classes.

As early as when C was popular and there was no C + +, Bjarne Stroustrup chose C, extended it with classes from the Simula67 language, and created C + +. These classes open up a whole new world for object-oriented paradigms.

Initially, there was no dedicated C + compiler. When you write C +, the compiler converts it to C (C that is difficult for humans to understand, but C still works), and then compiles C. However, C + + compilers do appear, and new features appear.

Some of the differences between C and C + are:

In C, for input or output, we use functions such as gets (), puts (), scanf (), printf () and so on. The expected values of these functions have specific data types, or for scanf () or printf (), we must specify the data types that we want the function to handle. In C +, we can sometimes use these features. However, people generally prefer to use cin and cout objects. Note that these are the objects of the class, and when we perform I / O, they themselves determine the data type of the variable.

C does not support function overloading. You cannot have multiple features with the same name. Therefore, suppose you have a function that calculates the square of integers and a function that calculates the square of floating-point numbers, you must name them separately. In C + +, give them the same name, and the language determines which function to use based on the type of arguments passed to the function.

C does not support exception handling. You should avoid runtime errors caused by unexpected event turning points. In C +, you can throw exceptions and handle them accordingly. This is very useful when working on large projects.

In addition to function overloading, C + supports operator overloading that does not exist in C. Have you ever thought of concatenating two strings with the + operator? Well, although C is unlucky, it can be done with C +. Of course, classes make this possible, and you must use std:: string instead of regular strings, but std:: string is much better in order to be rational.

The previous point leads us to std:: string. This is a (surprise class) class for strings. Easier to use and read than C-style strings.

For dynamic memory allocation, C uses malloc () and free (), but C + + has better new and delete operators.

Namespaces are becoming more and more popular, especially in application development, and C + does support namespaces that are missing in the C language.

One of the annoying features of C is that you need to declare all variables at the beginning of their scope, after the left parenthesis of all variables or functions. In C +, define a variable anywhere, preferably closest to where it is used.

In C, global variables can have multiple declarations. C + + is stricter and is not allowed to do so.

After all, C + + is object-oriented. Things like abstraction, inheritance, polymorphism and so on cannot be done in C. It brings you a whole new paradigm. If you like, please use it. If not, you can use it (but many of the features of C + + are built around classes).

Have you ever thought of creating similar features that differ only in the types of data you are dealing with? You can do this through function overloading, but this is usually used for functions with the same name but different behavior. What if you want to do exactly the same thing but for different data types? It doesn't make much sense to write multiple functions for this. Also, function overloading does not support returning only functions of different types. The template is right here. You can create generic functions and classes and use them with any data type. C + + has, C does not.

C + + has STL, which is a very useful list of templated classes (so you can use them for various data types). You have prepared algorithms, data structures, iterators, etc. Maybe every C + programmer will praise the C encoder.

This is the end of the content of "what's the difference between C and C++". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report