private继承
实例:
#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
class child2: private parent
{
void SetVar(int _a, int _b, int _c)
{
a = _a;
//b = _b;//不可以访问,因为是基类的私有属性
d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
}
public:
private:
protected:
};
int main()
{
child2 oop;
/*这三种方式都不可以使用,因为在子类中的集成方式为private,所以,基类所有成员属性在子类中都变成private,所以在外部不可以访问了*/
//oop.a = 100;
//oop.b = 1000;
//oop.d=10000
system("pause");
return 0;
}
还没有评论,来说两句吧...