A brief analysis of C # and C + + related concepts _c language

Source: Internet
Author: User
Tags abstract multiple inheritance in c

1, in C #, class variables for reference types, in C + + class variables for the value type, such as MyClass MC, in C + +, MC is a value type, members stored in the program stack area, in C #, MC is reference type, point to the members of the heap.

2, switch statements, C # in the case after the root string, such as "ABC", but in C + + but not, C + + case after only with numbers, characters, and enumerations.

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

4, C # in the keyword sealed can be declared as sealed class, that is, can not be inherited as a parent class, in C + + can be declared by the constructor as private, so that it can not be inherited as a parent class sent out classes, and can not be instantiated, However, the constructor can be invoked by the static function of the class to produce the instance.

5. C # supports user-defined type conversions, which define implicit conversions through keyword implicit, and define explicit conversions by keyword explicit. In C + +, the purpose of implicit conversions can be achieved by overloading the = operator.

6. In C #, operator overloading must define operator overloading functions as members of a class and must be decorated with the static and public keywords, and for unary operator overloading, a parameter is required, and two arguments are required for the two-dollar operator overload. In C + +, operator overloading, with a default suppressed parameter this, the unary operator overload requires no arguments, and two dollars requires only one argument. and C # cannot overload the = number operator.

7. C # 's Catch statement has a general catch statement with no arguments, which is equivalent to a catch in C + + (...). )。

8. C # uses the keyword abstract to declare a class as an abstract class, and an abstract class cannot be instantiated. The same cannot be instantiated in C + + by declaring a pure virtual function to make a class an abstract class.

9, for the structure type, namely uses the keyword struct definition data structure, in C + + it and class usage and function are similar, only uses the class to define the default member's access permission is private, but stract the default member's access permission is public. The variables of class in C # are reference types, whereas struct-defined variables are value types, and members have the same access rights as class. Note that structs are not allowed in C # to have destructors.

10. For structural types, predefined parameterless constructors exist for each structure and cannot be deleted or redefined, and constructors can be overloaded, but with parameters.

11. In C #, an array is an object, inherits from System.Array, has a certain method of operation, and the data is a reference type, referenced on the stack or heap, and 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, and the array itself can be in the stack or in the heap. C # does not support dynamic arrays, and C + + can dynamically allocate memory through new to implement dynamic arrays.

12, in C #, there is a data type called interface, it is a set of functions of the Declaration does not implement the reference type of functions, can not be instantiated, only classes can implement the interface, implementation interface must implement all declared functions in the interface. In C + + although there is no such default data type, but in C + + You can create an interface type, that is, the function inside all declare pure virtual functions, does not contain data members, to achieve the same function.

13, a class in C # can implement multiple interfaces, each interface must be ', ' delimited, and if there is a base class, the base class must be written at the front, and if multiple interfaces have multiple interface members of the same signature and return type, the class can implement a single member to satisfy all the interfaces that contain duplicate members. This is the default method in C #, and you can use the dot operator to specify the interface to which the implementation's members belong. This is similar to the ambiguity problem generated by multiple inheritance in C + +, which does not provide this default mechanism and is relatively free in C + +, and you can use the domain operator to indicate which base class member you are calling, or which base class you are implementing. If you only want your derived class to have only one implementation for a function or member of the same signature, you can use the method of virtual inheritance to implement it.

14, in C # class can only single inheritance can not achieve multiple inheritance, while for interfaces, interfaces can implement single inheritance and multiple inheritance, and the class that implements it must provide implementations for all interface members in the interface inheritance system.

15, in C # is called boxing and unboxing of the conversion, why there are these two transformations, because the variable data in C # is stored in the heap (reference type), while others are stored in the stack (value type), when you want to assign the value type to the reference type, the value type is boxed, the other is the need to remove the box. And the unboxing is an explicit conversion. After boxing, the value that the reference type holds in the heap is a copy of the original value type. This problem does not exist in C + + because the variable data is stored on the stack by default, and if you want to save it in the heap, you need to use a pointer, and to assign a value, you need to P->data = data or (*p). Data = data. The effect of an implicit conversion can also be achieved by overloading the ' = ' operator.

16. In C # 's generic programming, you can use the keyword where to constrain the type parameters, such as Class Myclass<t1, T2, t3> where t2:cls2 where t3:in3. Only the CLS2 type or its derived type can be the T2 argument, and only the In3 interface type can be the T3 argument. This is similar to template specificity and template bias in generic programming in C + +, where similar functions can be implemented in C + +, class Myclass<t1, CLS, in3>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.