C++ protected的用法

#include<iostream>
 
using namespace std;
 
 
class A
{
    
    
protected:
	int m;
};
 
class B: public A
{
    
    
public:
    void show()
	{
    
    
       m=10;
	   cout<<"m is :"<<m<<endl;
	}
};
 
 
void main()
{
    
    
	
 
	B p;
//	p.m= 10; 这个会报错,因为m在A中是protected型,然后在派生类中会变成private,因此在类外是不可以访问 
	p.show();
 
 
}

如上代码所示,在基类中为protected类型,在子类中会变成private类型,类外无法访问。

猜你喜欢

转载自blog.csdn.net/qq_35037684/article/details/121357209