私有继承(private):
protected 成员的特点与作用:
#include<iostream>
using namespace std;
class A
{
public:
void SetA(int x) { this->x = x; }
private:
int x;
};
class B
{
public:
void SetB(int x) { this->y = y; }
private:
int y;
};
class C
{
protected:
void SetC(int x) { this->y = y; }
private:
int y;
};
class ABC: public A, protected B
{
public:
void SetABC(int x, int y, int z)
{
SetA(x);
SetB(y);
this->z = z;
}
private:
int z;
};
class ABC2: private ABC
{
public:
void SetABC2(int x) { SetB(x); }
};
int main(void)
{
ABC s1;
s1.SetA(5);
//s1.SetB(6); 非法!因为类B是保护继承,所以通过派生类的对象:不能直接访问从基类继承的任何成员
s1.SetABC(2, 5, 6);
}