C ++ inheritance details: Public inheritance, private inheritance, and protected inheritance

Source: Internet
Author: User

Public, private, and protected are common inheritance methods.

1. Public inheritance)

Public inheritance is characterized by the original state of both the public members of the base class and the protected members as the members of the derived class, while the private members of the base class are still private, it cannot be accessed by the subclass of this derived class.

2. Private)

Private inheritance features that the public and protected members of the base class are both private members of the derived class and cannot be accessed by the subclass of the derived class.

3. Protected)

The protection inheritance feature is that all the public and protected members of the base class become the protected members of the derived class and can only be accessed by the member functions or friends of the derived class, the Private Members of the base class are still private.

The following lists the basic class features and derived class features of three different inheritance methods.

PublicProtectedPrivate
Total inheritancePublicProtectedInvisible
Private inheritancePrivatePrivateInvisible
Protection inheritanceProtectedProtectedInvisible

In: 1) The base class members are visible to the common and protected members and Private Members are invisible to the derived classes.

2) base class members for the objects of the derived class: It depends on the type of members of the base class in the derived class. For example, in private inheritance, both the common and Private Members of the base class become private members in the derived class, therefore, for objects in a derived class, the common and Private Members of the base class are invisible.

To further understand the differences between the three inheritance methods in terms of Member visibility, we will discuss them from three different perspectives.

For public inheritance

(1) Visibility of base class members on their objects:

Public members are visible, while others are invisible. Here, the protection of members is the same as that of private members.

(2) Visibility of base class members on Derived classes:

Public and protected members are visible, while private members are invisible. The members are protected in the same way as public members.

(3) Visibility of base class members on derived class objects:

Public members are visible, while other members are invisible.

Therefore, in public inheritance, the object of the derived class can access the public members in the base class; the member functions of the derived class can access the public members and protect members in the base class. Here, you must distinguish between the object of the derived class and the member functions in the derived class to access the base class.

For private inheritance Methods

(1) Visibility of base class members on their objects:

Public members are visible, while other members are invisible.

(2) Visibility of base class members on Derived classes:

Public and protected members are visible, while private members are invisible.

(3) Visibility of base class members on derived class objects:

All members are invisible.

Therefore, in private inheritance, the base class members can only be accessed by direct Derived classes, but cannot be inherited.

For the protection Inheritance Method

This inheritance method is the same as the private inheritance method. The difference between the two lies in that they have different visibility on the base class members for the members of the derived class.

The visibility mentioned above is accessibility.

There is another saying about accessibility. In this rule, the object of the derived class uses horizontal access to the base class, and the access of the derived class to the base class is vertical access.

Take a look at this example:

# Include <iostream>
Using Namespace STD;
//////////////////////////////////////// //////////////////////////////// //
Class A // Parent class
{
Private :
Int Privatedatea;
Protected :
Int Protecteddatea;
Public :
Int Publicdatea;
};
//////////////////////////////////////// //////////////////////////////// //
Class B: Public A // Derived class B of base class A (inherited in total)
{
Public :
Void Funct ()
{
Int B;
B = privatedatea; // Error: Private Members in the base class are invisible in the derived class.
B = protecteddatea; // OK: the protected member of the base class is the protected member in the derived class.
B = publicdatea; // OK: The Public Member of the base class is a public member in the derived class.
}
};
//////////////////////////////////////// //////////////////////////////// //
Class C: Private A // C (private inheritance), a derived class of Base Class)
{
Public :
Void Funct ()
{
Int C;
C = privatedatea;// Error: Private Members in the base class are invisible in the derived class.
C = protecteddatea; // OK: the protected members of the base class are private members in the derived class.
C = publicdatea; // OK: The public members of the base class are private members in the derived class.
}
};
//////////////////////////////////////// //////////////////////////////// //
Class D: Protected A // Derived class D of base class A (Protection inheritance)
{
Public :
Void Funct ()
{
Int D;
D = privatedatea; // Error: Private Members in the base class are invisible in the derived class.
D = protecteddatea; // OK: the protected member of the base class is the protected member in the derived class.
D = publicdatea; // OK: The public members of the base class are protected members in the derived class.
}
};
//////////////////////////////////////// //////////////////////////////// //
Int Main ()
{
Int A;

B objb;
A = objb. privatedatea;// Error: Private Members in the base class are invisible to the derived class and invisible to the object.
A = objb. protecteddatea; // Error: the protected member of the base class is a protected member in the derived class and is invisible to the object.
A = objb. publicdatea; // OK: The Public Member of the base class is a public member in the derived class and is visible to the object.

C objc;
A = objc. privatedatea; // Error: Private Members in the base class are invisible to the derived class and invisible to the object.
A = objc. protecteddatea;// Error: the protected member of the base class is a private member in the derived class and is invisible to the object.
A = objc. publicdatea; // Error: The Public Member of the base class is a private member in the derived class and is invisible to the object.

D objd;
A = objd. privatedatea; // Error: Private Members in the base class are invisible to the derived class and invisible to the object.
A = objd. protecteddatea; // Error: the protected member of the base class is a protected member in the derived class and is invisible to the object.
A = objd. publicdatea;// Error: The public members of the base class are protected members in the derived class and are invisible to objects.

Return 0 ;
}

Address: http://www.cnblogs.com/qlwy/archive/2011/08/25/2153584.html

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.