C# versus C++ 

In most programming scenarios, C++ can be classified as an extension of C and can execute all the code that was written in C. It provides all the features of object-oriented programming while retaining the functionalities provided by C. There are several features that are common between C# and C++. Just as in C#, we can implement object-oriented programming, exception handling, and type safety in C++. However, there are also certain things that make C# different to C++ and more similar to Java.

Before we look at the differences and similarities between C# and C++, we must understand some key concepts pertaining to object-oriented programming. 

The languages that implement object-oriented programming are classified in two categories:

  • Fully object-oriented languages
  • Pure object-oriented languages

A language is classified as a fully object-oriented programming language if it implements at least the four core pillars of Abstraction, Encapsulation, Polymorphism, and Inheritance.

On the other hand, a language can be defined as a pure object-oriented programming language when, apart from being fully object-oriented programming, it only contains classes and objects. This means that all methods, properties, and attributes declared must be inside a class and also should not have any predefined data types, such as char and int.

In the case of C#, we can have predefined data types. In Chapter 2Understanding Classes, Structures, and Interfaces, we will look into those predefined data types in detail. This makes C# a fully object-oriented language and not a pure object-oriented language.

On the other hand, in the case of C++, we can define methods that are not part of any class. This, too, makes it a fully object-oriented language.

Now, let's look at some of the similarities and differences between C# and C++:

Feature

C#

C++

Object-oriented programming

As described previously, C# is a fully object-oriented language.

Similar to C#, C++ is also a fully object-oriented language.

Memory management

C# has got an inbuilt garbage collector that manages the allocation and deallocation of memory. In Chapter 9, Managing the Object Life Cycle, we will understand memory management in C# in detail.

C++ does not have a built-in garbage collector. Due to this, developers are responsible for handling the allocation and deallocation of memory.

Inheritance

C# does not support multiple inheritance. In Chapter 2, Understanding Classes, Structures, and Interfaces, we will learn what it means; however in simple terms, it means that a class can only inherit from one class at a time.

Compared to C# , C++ allows us to implement multi-level inheritance.

Use of pointers

Although C# allows us to use pointers in our code, we need to declare the code with a snippet of UnSafe. We will look into this in detail in Chapter 8, Creating and Using of Types in C#.

C++ allows us to use pointers anywhere without any implicit declaration in the code.

In the previous two sections, we saw how C# compares to both C and C++. However, there is one important difference that we haven't yet explored. That feature is platform independence and was one of the main reasons C# was introduced by Microsoft. When working with C and C++, we need to compile the code in accordance with the underlying platform features, such as the operating system. 

Suppose we write an application in C or C++ and compile it. During the compilation stage, the compiler translates the code into a native language code that is only compatible with the underlying platform. This basically implies that an application in C++, developed and compiled on a Windows machine, will just be compatible with a Windows machine. If the compiled bits are used on a different system, such as Linux, it will not work there.

This difference is caused due to the varying nature of compilers and their compatibility with underlying operating systems, such as Linux and Windows. These are some of the common compilers in Linux and Windows that are available for C and C++:

  • Linux: GCC, Failsafe C, and SubC
  • Windows: Microsoft Windows SDK, Turbo C++, and SubC

Before C# was developed, this platform dependence issue was a major disadvantage compared to some of the other programming languages, such as Java. In Java, when an application is compiled, it's not directly converted into machine code. Instead, it's converted into an intermediate language known as ByteCode. The ByteCode is platform-independent and can be deployed on different platforms. 

When Microsoft introduced C#, they inculcated the same principle in the language. When an application written in C# is compiled, instead of being converted to the native code compatible with the machine, the application is first translated to an intermediate language commonly known as IL code

After the IL code is generated, the Common Language Runtime (CLR) comes into effect. CLR is a runtime environment that sits in the memory of the underlying machine and converts the IL code to the native code, which is specific to the machine. This process is Just-In-Time (JIT) compilation. In the next section, we will look at the underlying platform of the .NET Framework, which handles all this for a C# application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.190.182