Differences between C ++ and C # inheritance mechanisms

Source: Internet
Author: User

C ++ has the following inheritance methods:

Public, private, and protected (they directly affect the members of the derived class and the access rules of their objects to the base class members ).

(1) Public (Public inheritance): When inheriting, the private member in the base class is hidden, and the attributes of other members remain unchanged. The private member is inherited as a member of the derived class. Members of so derived classes can only access public/protected members in the base class, but not private members. Objects of derived classes can only access public members in the base class.

(2) Private (private inheritance): When the basic class is inherited, the private member is hidden, and other member attributes are changed to private, and inherited as a member of the derived class. A member of a derived class can only access the public/protected member of the base class, but cannot access the private member. The object of a derived class cannot access any member of the base class.

(3) protected: When inheriting, the private member in the base class is hidden, and the attributes of other Members are changed to protected, which is inherited as the member of the derived class. A member of a derived class can only access the public/protected member of the base class, but cannot access the private member. The object of a derived class cannot access any member of the base class.

However, the inheritance of C # can only be achieved through the permission control of member variables.

In C ++, if a variable uses different inheritance methods, its accessibility will change greatly during the inheritance process.

For example

Class A {public int A1 () {}; protected int A2 () {}; private int A3 () {};} class B1: Protected A {A1; // accessible to your own preotected A2; // accessible to your own preotected // A3; Inaccessible} Class B2: Private A {A1; // accessible, change to private A2; // accessible, private // A3; Inaccessible} Class C1: protected B1 {A1; // It is a protected member of B1, inherited to become its protected member A2; // It is the protected member of B1, inherited to become its protected member} Class C2: protected B2 {// A1; it is a private member of B2 and can no longer be inherited/A2; it is a private member of B2 and can no longer be inherited}

Therefore, a member variable that can be inherited may be lost and cannot be inherited in different inheritance methods. In C #, there is no concept of inheritance. If a member variable can be inherited, it will be inherited by the derived class.

Just like the family's family, C ++ may not pass on to the next generation because of a generation, so that future generations will not be able to get it. In C #, this family is always passed (unless other human factors occur during the transfer ).

Of course, the research in this article is limited to the inherited accessibility.

(Some brief analysis will be made on the member modifiers of C # in subsequent articles)

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.