Transferred from: http://www.cnblogs.com/qlwy/archive/2011/08/25/2153584.html
Public, private, and protected inheritance (protected) are three common ways to inherit.
1. Public inheritance
Public inheritance is characterized by the public and protected members of the base class as members of the derived class, which remain in their original state, while the private members of the base class are still private and cannot be accessed by subclasses of the derived class.
2. Private inheritance
Private inheritance is characterized by the public and protected members of the base class as private members of the derived class and cannot be accessed by subclasses of the derived class.
3. Protection Inheritance (protected)
The feature of protection inheritance is that all public and protected members of the base class become protected members of the derived class and can only be accessed by its derived class member functions or friends, and the private members of the base class are still private.
The following is a list of the base class attributes and derived class attributes of three different inheritance methods.
|
Public |
Protected |
Private |
Common inheritance |
Public |
Protected |
Not visible |
Private inheritance |
Private |
Private |
Not visible |
Protect inheritance |
Protected |
Protected |
Not visible |
In: 1) A base class member is a member of a derived class: the members that are shared and protected are visible, and private members are invisible.
2) The base class member is to the object of the derived class: To see what type of member of the base class becomes in the derived class. For example, when a private inheritance occurs, both common and private members of the base class become private members of the derived class, so that common members and private members of the base class are invisible to the objects in the derived class.
To further understand the differences in the visibility of the three different types of inheritance in their members, the following is discussed from three different perspectives.
For the public inheritance method
(1) The visibility of the base class members to their objects:
Public members are visible, others are not visible. Here the protection members are identical to the private members.
(2) The visibility of a base class member on a derived class:
Public and protected members are visible, and private members are not visible. The protection of Members here is the same as public members.
(3) The visibility of a base class member on a derived class object:
Public members are visible and other members are not visible.
Therefore, when the public inherits, the objects of the derived class can access the public members in the base class, and the member functions of the derived class can access the public and protected members in the base class. Here, it is important to distinguish between objects of derived classes and member functions in derived classes that have different access to the base class.
For private inheritance methods
(1) The visibility of the base class members to their objects:
Public members are visible and other members are not visible.
(2) The visibility of a base class member on a derived class:
Public and protected members are visible, and private members are invisible.
(3) The visibility of a base class member on a derived class object:
All the members are invisible.
Therefore, in private inheritance, members of the base class can only be accessed by directly derived classes, and cannot be inherited further down.
For protection of inheritance methods
This inherits in the same way as private inheritance. The difference between the two is that the members of the derived class have different visibility to the base class members.
The visibility referred to above is also accessibility.
There is another way of saying about accessibility. In this rule, objects that are called derived classes are accessed horizontally for the base class, which is called a derived class's access to the base class as a vertical access.
Take A look at this example :
#include<iostream>
using
namespace
std;
//////////////////////////////////////////////////////////////////////////
class
A
//父类
{
private
:
int
privatedateA;
protected
:
int
protecteddateA;
public
:
int
publicdateA;
};
//////////////////////////////////////////////////////////////////////////
class
B :
public
A
//基类A的派生类B(共有继承)
{
public
:
void
funct()
{
int
b;
b=privatedateA;
//error:基类中私有成员在派生类中是不可见的
b=protecteddateA;
//ok:基类的保护成员在派生类中为保护成员
b=publicdateA;
//ok:基类的公共成员在派生类中为公共成员
}
};
//////////////////////////////////////////////////////////////////////////
class
C :
private
A
//基类A的派生类C(私有继承)
{
public
:
void
funct()
{
int
c;
c=privatedateA;
//error:基类中私有成员在派生类中是不可见的
c=protecteddateA;
//ok:基类的保护成员在派生类中为私有成员
c=publicdateA;
//ok:基类的公共成员在派生类中为私有成员
}
};
//////////////////////////////////////////////////////////////////////////
class
D :
protected
A
//基类A的派生类D(保护继承)
{
public
:
void
funct()
{
int
d;
d=privatedateA;
//error:基类中私有成员在派生类中是不可见的
d=protecteddateA;
//ok:基类的保护成员在派生类中为保护成员
d=publicdateA;
//ok:基类的公共成员在派生类中为保护成员
}
};
//////////////////////////////////////////////////////////////////////////
int
main()
{
int
a;
B objB;
a=objB.privatedateA;
//error:基类中私有成员在派生类中是不可见的,对对象不可见
a=objB.protecteddateA;
//error:基类的保护成员在派生类中为保护成员,对对象不可见
a=objB.publicdateA;
//ok:基类的公共成员在派生类中为公共成员,对对象可见
C objC;
a=objC.privatedateA;
//error:基类中私有成员在派生类中是不可见的,对对象不可见
a=objC.protecteddateA;
//error:基类的保护成员在派生类中为私有成员,对对象不可见
a=objC.publicdateA;
//error:基类的公共成员在派生类中为私有成员,对对象不可见
D objD;
a=objD.privatedateA;
//error:基类中私有成员在派生类中是不可见的,对对象不可见
a=objD.protecteddateA;
//error:基类的保护成员在派生类中为保护成员,对对象不可见
a=objD.publicdateA;
//error:基类的公共成员在派生类中为保护成员,对对象不可见
return
0;
}
|
C + + Inheritance