C++访问控制

#include
using namespace std;
class base
{
public:
int a = 555;
private:
int b = 666;
protected:
int c = 777;
};
class publicTest :public base
{
public:
void fun()
{
cout << "publicTest: ";
cout <<"this->a = "<a;
cout <<"this->c = " << this->c;
//cout << "this->b = " << this->b;公有继承,父类私有变量不可访问
cout << endl;

}

};
class protectedTest :protected base
{
public:
void fun()
{
cout << "protectedTest: ";
cout << "this->a = " << this->a;
cout << "this->c = " << this->c;
//cout << "this->b = " << this->b;保护继承,父类私有变量不可访问
cout << endl;

}

};
class privateTest :private base
{
public:
void fun()
{
cout << "privateTest: ";
cout << "this->a = " << this->a;
cout << "this->c = " << this->c;
//cout << "this->b = " << this->b;保护继承,父类私有变量不可访问
cout << endl;

}

};
int main()
{
/公有集成测试/
publicTest ptest;
ptest.fun();
cout << ptest.a << endl;
//cout << ptest.b << endl;
//cout << ptest.c << endl;公有继承中,父类公有变量在子类中是公有变量,
//父类私有变量在子类中是不可访问,父类保护变量在子类中是保护变量
/保护继承测试/
protectedTest pretest;
pretest.fun();
//cout << pretest.a << endl;
//cout << pretest.b << endl;
//cout << pretest.c << endl;保护继承中,父类公有变量在子类中是保护变量,
//父类私有变量在子类中是不可访问,父类保护变量在子类中是保护变量
/私有继承测试/
privateTest pritest;
pritest.fun();
//cout << pritest.a << endl;
//cout << pritest.b << endl;
//cout << pritest.c << endl;私有继承中,父类公有变量在子类中是私有变量,
//父类私有变量在子类中是不可访问,父类保护变量在子类中是私有变量
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值