C ++ vs c # (9): Inheritance

Source: Internet
Author: User

// ================================================ ====================================
// TITLE:
// C ++ vs c # (9): Inheritance
// AUTHOR:
// Norains
// DATE:
// Thursday 17-jaruary-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================


1. Inheritance

C ++ and C # both have classes, so naturally, the existence of inheritance is quite simple. However, there are many differences in inheritance between the two. The most obvious difference is that C ++ can inherit multiple classes. That is to say, C ++ can have multiple parent classes, while C # has only one. In other words, the following code can be compiled smoothly in C ++, but in C # It will be ruthlessly complained by the compiler:
View plaincopy to clipboardprint?
Class CMyBase_A
{
...
};

Class CMyBase_ B
{
...
};

// CMyClass inherits both CMyBase_A and CMyBase_ B. This Code cannot be passed in C #
Class CMyClass:
Public CMyBase_A,
Public CMyBase_ B
{
...;
};

Class CMyBase_A
{
...
};

Class CMyBase_ B
{
...
};

// CMyClass inherits both CMyBase_A and CMyBase_ B. This Code cannot be passed in C #
Class CMyClass:
Public CMyBase_A,
Public CMyBase_ B
{
...;
};


Does it mean that C # can only inherit from one parent class, but cannot have more extensions? C # has two more than C ++, and naturally won't make such a stupid mistake, because it also has something called an interface. Unlike only one parent class, there can be multiple interfaces. The interface keyword is interface, which is similar to the class and can be modified using public or internal. The difference is that abstract or sealed cannot be used, because the two keywords have no meaning for the interface, because the interface must not be instantiated and must be inherited.

If you change the code above as an interface, the code will become more like this in C:
View plaincopy to clipboardprint?
Class CMyBase
{
...
};

Interface IMyBase_A
{
...
};

Interface IMyBase_ B
{
...
};

// One parent class and multiple interfaces
Class CMyClass:
CMyBase,
IMyBase_A,
IMyBase_ B
{
...
};
Class CMyBase
{
...
};

Interface IMyBase_A
{
...
};

Interface IMyBase_ B
{
...
};

// One parent class and multiple interfaces
Class CMyClass:
CMyBase,
IMyBase_A,
IMyBase_ B
{
...
};

 
  
What makes it easy for C ++ programmers to ignore here is that the order of C # interfaces is strictly defined, and interfaces must be placed behind classes. That is to say, the following code is incorrect:
View plaincopy to clipboardprint?
// The interface is placed before the class and the Code cannot be compiled
Class CMyClass:
IMyBase_A,
CMyBase,
IMyBase_ B
{
...
};
// The interface is placed before the class and the Code cannot be compiled
Class CMyClass:
IMyBase_A,
CMyBase,
IMyBase_ B
{
...
};
  
  
C # Another small problem that is easy to ignore in inheritance is that the derived class cannot have higher access permissions than the parent class, as shown in the following code:
View plaincopy to clipboardprint?
// The parent class is internal.
Internal class CMyBase
{
...
}

// The derived class is public and cannot be compiled.
Public class CMyClass:
CMyBase
{
...
};
// The parent class is internal.
Internal class CMyBase
{
...
}

// The derived class is public and cannot be compiled.
Public class CMyClass:
CMyBase
{
...
};
 
  
In this Code, CMyBase of the parent class is modified by internal, while CMyClass of the derived class is modified by public, because the accessible permission of public is much higher than internal, this Code violates the rules that the access permission of a derived class is lower than that of the parent class.

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.