A brief analysis of the comparison between c sharp and C++

  • 2020-04-02 01:28:44
  • OfStack

1. In C#, the variable of class is the reference type; in C++, the variable of class is the value type, such as myclass MC. In C++, the MC is the value type and the members are stored in the stack area of the program.

2. Switch statement. In C#, strings can be root after case, such as case "ABC", but not in C++.

3. In C#, there is no private inheritance or protection inheritance in C++, only public inheritance.

4, c # can be sealed by keywords in a class declaration for sealed class, which can't be as sent class that is inherited from the parent class in c + + by the constructor is declared private, let it be as the parent class are inherited from class, and at the same time cannot be instantiated, but can be by static function calls the constructor to create an instance of a class.

5. C# supports user-defined type conversions, implicit conversions can be defined with the keyword implicit, and explicit conversions can be defined with the keyword explicit. In C++, implicit conversion is achieved by overloading the = operator.

6. In C#, operator overloading must define the operator overloading function as a member of the class, and it must be decorated with static and public keywords. Moreover, for unary operator overloading, one parameter is required, while for binary operator overloading, two parameters are required. In C++, operator overloading, with a default implicit parameter this, unary operator overloading requires no parameter, and binary requires only one parameter. And the = sign operator cannot be overridden in C#.

7. C# 's catch statement has a general catch statement without any parameters, which is equivalent to C++' s catch (...). .

C# USES the keyword abstract to declare a class as abstract. Abstract classes cannot be instantiated. Making a class abstract by declaring a pure virtual function in C++ is also impossible to instantiate.

9. For structure type, that is, the data structure defined by the keyword struct. In C++, it is similar to the usage and function of class. While the class variable in C# is a reference type, the struct defines a variable as a value type, and members have the same access rights as the class. Note that structures in C# are not allowed to have destructors.

10. For structure types, predefined no-argument constructors exist for each structure and cannot be deleted or redefined.

11. In C#, an Array is an object, inherited from system.array, with certain operation methods, and the data is a reference type, which is referred to on the stack or heap, while the Array object itself is in the heap. An array in C++ is just a collection of objects of data or classes. The array itself is not an object, but the elements in the array can be objects. The array itself can be on the stack or in the heap. C# does not support dynamic arrays, while C++ can dynamically allocate memory through new to implement dynamic arrays.

12, there is a data type called interface in C#, it is a set of function declaration does not implement the function reference type, can not instantiate, only the class can implement the interface, the implementation interface must implement all the declared functions in the interface. Although there is no such default data type in C++, but in C++ you can create an interface type, which is to declare all the functions inside the pure virtual function, without data members, to achieve the same function.

13, in C#, a class can implement multiple interfaces, each interface must be separated by ', ', if there is a base class, the base class must be written in the first place, if there are more than one interface with the same signature and return type of interface members, the class can implement a single member to satisfy all the interfaces containing duplicate members. This is the default in C#, and you can also use the dot operator to specify the interface to which the implemented member belongs. This is similar to the ambiguity problem caused by multiple inheritance in C++, which does not provide such a default mechanism. In C++, it is relatively free, and you can use the domain operator to indicate which base class member you are calling or which base class member you are implementing. If you only want your derived class to have one implementation of the same signed function or member, you can use virtual inheritance.

14. In C#, the class can only be single inheritance but not multiple inheritance, while for the interface, the interface can be single inheritance and multiple inheritance, and the class that implements it must provide implementation to all interface members in the interface inheritance system.

15, in C# there are called boxing and unboxing conversion, why there are two kinds of conversion, because the variable data in C# is stored in the heap (reference type), some is stored in the stack (value type), when you want to assign the value type to the reference type, you need to box the value type, otherwise you need to unbox. And unboxing is an explicit conversion. After boxing, the value of the reference type in the heap is a copy of the original value type. In C++ there is no such problem, because variable data is stored on the stack by default, Pointers are required for storage in the heap, and p- is required for assignment > Data = data or (*p). Data = data. Implicit conversions can also be achieved by overloading the '=' operator.

16. In C# generic programming, the keyword where can be used to constrain type parameters, such as class myclass < T1, T2, T3 > Where T2:cls2 where T3:In3. Only cls2 type or its derived type can be an argument of T2, and only In3 interface type can be an argument of T3. This is similar to template specialization and template partial specialization in generic programming in C++, where similar functionality can be achieved in such a form, class myclass < T1, CLS, In3 > .

Related articles: