实例:
#include <iostream>
using namespace std;
class parent{
public:
void print(void)
{
//cout << "a=:" << a << " b=:" << b <<" c=:"<< d << endl;
}
int a;
private:
int b;
protected:
int d;
};
#if 0
class child1 :public parent
{
public:
void SetVar(int _a, int _b, int _c)
{
a = _a;
//b = _b;//不可以访问,因为是基类的私有属性
d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
}
private:
protected:
};
#endif
/*注释*/
#if 0
class child2: private parent
{
void SetVar(int _a, int _b, int _c)
{
a = _a;
//b = _b;//不可以访问,因为是基类的私有属性
d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
}
public:
private:
protected:
};
#endif
class child3 :protected parent
{
void SetVar(int _a, int _b, int _c)
{
a = _a;//a在基类中的访问属性是public,在protected继承中变成protect属性,所以仅限于在基类和当前子类中使用不可以再类的外部使用
//b = _b;//不可以访问,因为是基类的私有属性
d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
}
public:
private:
protected:
};
int main()
{
child3 oop;
/*这三种方式都不可以使用,因为在子类中的集成方式为protect,所以,a在派生类中的访问属性成为protected的了,
b的原属性为private,所以在protected中任然是private属性
d在基类的属性为protect,所以在protected集成中任然是protected,在类的外部不可以使用
*/
//oop.a = 100;
//oop.b = 1000;
//oop.d=10000
system("pause");
return 0;
}