Detailed Explanation of the Differences Between C and C++

Detailed Explanation of the Differences Between C and C++

1

From C Language to C++

In 1980, Dr. Bjarne Stroustrup began creating a new language that could incorporate object-oriented programming features. At that time, object-oriented programming was a novel concept. Dr. Stroustrup did not design a new language from scratch but modified the C language, which resulted in C++.C++ is a superset of C, meaning that C++ is compatible with the C language. C++ adds many features and concepts on top of C, implemented through keywords. However, this article will not focus on the differences between C++ and C; instead, it will observe the issues between the two languages from the perspective of data structures and explain the improvements in C++. From this point forward, we will implement data structures using C++ features and concepts based on C, which will facilitate our program design.

2

Code Changes

Here are several obvious changes that we will frequently use, which can be seen in the C++ tutorials on the C language website. If you have already learned C++, you may skip this section. If you are still somewhat unclear about C++, it is recommended that you refer to C++ learning materials while reading this article; the two studies do not conflict but rather complement each other.

Header File Inclusion

C language includes header files using the format include name.h. In contrast, C++ removes the .h extension (although using .h in most compilers will not cause an error). Instead, it is changed to directly include the name. It is important to note that when using the C language standard in C++, a ‘C’ must be added before the library to indicate that this library comes from the C language.

Input and Output

The input and output in C++ differ from the type-specified input and output in C. C++ adopts a “stream” approach for input and output design, which greatly simplifies our design, but this approach is indeed slower. This chapter will later introduce optimizations for input and output, revisiting this content.

3

Comparison

The most prominent aspect of C++ compared to C is the addition of the concept of classes, which leads to encapsulation, inheritance, overloading, and polymorphism. The concept of encapsulation is somewhat similar to structures in C, but structures cannot have member functions, cannot define variable access permissions, and cannot be inherited. Inheritance allows a class to inherit from its parent class. For example, if you define a class as Animal, you can define subclasses such as Dog, Pig, etc. Overloading is most easily understood as operator overloading. For instance, in C, the – operator cannot directly perform operations like (2008.5.1 – 2006.1.2), but in C++, operator overloading allows such date operations to be performed directly. Polymorphism is more difficult to summarize and involves the concept of virtual functions. In simple terms, it allows the selection of the appropriate member function implementation based on the class.

Detailed Comparison of Differences

(1) Default Function ValuesC Language:The C89 standard does not support default function values.This operation is not feasible under C89, and writing it this way in C will result in an error.C++:C++ supports default function values, which must be assigned from right to left.For example: int FUN(int a = 10);This means that when called without parameters, a is automatically assigned an initial value of 10.Initial values must be assigned starting from the right side of the parameter list.(2) Inline FunctionsC Language:Inline functions are not present in the C89 standard. They are similar to macros, but macros are expanded at the pre-compilation stage and do not perform type checking.C++:The difference between inline functions and regular functions in C++ is that inline functions do not create a stack frame for rollback. Generally, we write inline functions in header files, and they are processed at the compilation stage. Inline functions require type checking and do not create a stack frame for rollback. They are usually written in header files.(3) Function OverloadingC Language:C does not support function overloading because the rules for generating function symbols in C are based on names.C++:C++ determines overloading based on function name, number of parameters, and parameter types, which is known as static polymorphism. Overloading must occur within the same scope, and the return value cannot be used as a basis for function overloading.(4) ConstC Language:In C, variables modified by const are not constants; they are called constant variables or read-only variables, and these constant variables cannot be used as array indices.C++:In C++, variables modified by const can be used as array indices, becoming true constants. They must be initialized at the time of definition and can be used as array indices. They can also be modified through pointers.(5) ReferencesIn C Language: Brothers: pointers pass by address.Pointers can be nulland can change their pointing to other objects.The size of a pointer is 4 bytes.In C++: it is an alias for the original variable.References are essentially pointers, and when used, they are dereferenced directly. They can be used with const to reference a literal.References cannot be nulland cannot change their pointing.The size of a reference is the size of the variable it points to, as a reference is merely an alias.(6) Malloc, Free && New, DeleteC Language:malloc() and free() are standard library functions in C for dynamically allocating and freeing memory.C++:new and delete are C++ operators and keywords. Under the hood, new and delete still call malloc and free.malloc requires the size before allocating memory and also requires type casting.int *p1 = (int *)malloc(sizeof(int));new does not require this because it can infer the type from the given type and can also assign an initial value simultaneously.int *p2 = new int;//int *p3 = new int(10);malloc is unsafe and requires manual type casting.new does not require type casting.free only releases space.delete first calls the destructor and then releases space (if needed).new first calls the constructor and then allocates space (if needed).malloc returns 0 on failure.malloc allocates in the heap.new can call malloc().new throws a bad_alloc exception on failure.new allocates in the free store.malloc cannot call new.(7) ScopeC Language:There are only two scopes: local and global.C does not have namespaces.C++:In C++, there are three scopes: local scope, class scope, and namespace scope.(8) C Language is Procedural, while C++ is Object-OrientedC Language:Procedural programming involves analyzing the steps to solve a problem and implementing these steps one by one, which can be called sequentially when needed.C is mainly used in embedded fields, driver development, and other areas that directly interact with hardware.C++:Object-oriented programming decomposes problems into various objects. The purpose of establishing objects is not to complete a step but to describe the behavior of a certain entity in the entire problem-solving process.C++ can be used for application layer development, user interface development, and other areas that interact with the operating system.(9) Input and Outputcout represents the output stream in C++.cin represents the input stream in C++.Both are defined in the header file “iostream”.“cout” must be used with “<<” which serves as an insertion operator.In a single statement, “<<” can be used multiple times to output several data.(10) Different Return ValuesC Language:In C, if a function does not specify a return type, it defaults to int and returns a random number, usually 0XCCCCCCCC.C++:In C++, if a function does not return a value, it must be specified as void; otherwise, the compilation will fail.

Detailed Explanation of the Differences Between C and C++

Detailed Explanation of the Differences Between C and C++

Screenshots of Some E-books

Detailed Explanation of the Differences Between C and C++

【Complete Set of Hardware Learning Materials Collection】

Detailed Explanation of the Differences Between C and C++

Leave a Comment