staticキーワードの承継




#include<iostream>
using namespace std;

class A
{
public:
	A()
	{
		cout<<"A的构造函数"<<endl;
	}
public:
	static int a;
	int b;
public:
	void get()
	{
		cout<<"A的b: "<<b<<endl;
	}
	void print()
	{
		cout<<"父类的AAAA"<<endl;
	}
protected:
private:
};


int A::a = 100;//静态成员变量初始化---不是简单的赋值告诉编译器分配内存,我在继承中用到了A,否则报错

class B:private A
{
public:
	B() //这样不写public默认是私有的
	{
		cout<<"B的构造函数"<<endl;
	}
public:
	int c;
	int b;
public:
	void get_child()
	{
		cout<<"B的b: "<<b<<endl;
		cout<<a<<endl;
	}
	void print()
	{
		cout<<"子类的BBBB"<<endl;
	}
protected:
private:
};



//static 关键字 遵守 派生类的访问控制规则
int main01()
{
	B b1;
	//b1.a = 200;//Error
	system("pause");
	return 0;
}
//A类中添加构造函数
int main()
{
	
	
	
	system("pause");
	return 0;
}


公開された33元の記事 ウォンの賞賛2 ビュー8518

おすすめ

転載: blog.csdn.net/QQ960054653/article/details/60955795