C + + access control and inheritance

Source: Internet
Author: User

Recently, the decision to seriously begin to record their own way of learning.

Access control for C + + classes is public, private, and protected.

The first is to divide the simple base class and the derived class case, not very easy to confuse with multilevel inheritance when the access control. I did not understand the original, it is precisely because there are no separate two cases of treatment.

In the case of simple base classes and derived classes, their access control can be summarized briefly:

Public:1, member function 2, class object 3, friend 4, subclass member function

Private:1, member function 2, friend

Protected:1, member function 2, friend 3, subclass member function

These are basically clear, but the previous confusion is that inheritance also has public, private and protected. This is actually a subclass of the derived class that does not affect the access control of the original base class and derived classes.

The general situation is:

Assuming public inheritance, the access control of public, private, and protected of the original base class does not change the subclass of the derived class of this base class.

In the case of protected inheritance, the public of the original base class is reduced to protected and is also a subclass of the derived class of the base class.

Private inheritance, the original base class public, protected are reduced to private, and also to the base class of the subclass of the derived class function.

The base class can be likened to a faucet, and the public of the derived class is inherited as the original faucet size of the tube (short pipe), protected inherited as the middle tube, private inheritance for the small tube, when the faucet water through the water pipe is, the original faucet of the waters narrowed, so just for the derived class sub-class function. The following code explains:

   classBase
{
Public:
intA1;
VirtualvoidTest ()=0;
protected:
intA2;
Private: 
intA3;
};
//------------------------------------------------------------------------------ 
classProtected_class_1:protectedBase//Protection inheritance
{
Public:
voidTest ()
{
A1=1;//A1 was transformed here into protected .
A2=2;//a2 was transformed here into protected .
//a3=3;//error, derived class cannot access private member of base class
}
};
classProtected_class_2: PublicProtected_class_1//Inherit Protected_class_1 class in public mode
{
Public:
voidTest ()
{
A1=1;//A1 Here still remains for A1 here to be transformed into protected
A2=2;//A2 Here still remains for A1 here to be transformed into protected
//a3=3;//Error, because the base class member is private, even the parent class is a protected inheritance and cannot change the control type of the base class member
}
};
//------------------------------------------------------------------------------ 
classPrivate_class_1:PrivateBase//Private inheritance
{
Public:
voidTest ()
{
A1=1;//A1 was converted here to private .
A2=2;//a2 was converted here to private .
//a3=3;//error, base class Private member is inaccessible
}
};
classPrivate_class_2: PublicPrivate_class_1//Inherit Private_class_1 class in public mode
{
Public:
voidTest ()
{
//a1=1;//Error, because the base class Private_class_1 is a private inheritance, A1 has been converted to private
//a2=2;//Error, because the base class Private_class_1 is a private inheritance, A1 has been converted to private
//a3=3;//Error, because the base class member is private, the Private_class_1 class is also a private inheritance
}
};
//------------------------------------------------------------------------------ 
classPublic_class_1: PublicBase//Common inheritance differs from other forms of inheritance, and inherited members do not change the way they control
{
Public:
voidTest ()
{
A1=1;//A1 still remain public
A2=2;//a2 still remains protected
//a3=3;//error, derived class cannot manipulate private members of base class
}
};
classPublic_class_2: PublicPublic_class_1//Inherit Public_class_1 class in public mode
{
Public:
voidTest ()
{
A1=1;//A1 still remain public
A2=2;//a2 still remains protected
//a3=3;//Error, because the base class member is a private member, even the parent class is a public inheritance and cannot change the control type of the base class member
}
};

  

C + + access control and inheritance

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.