1、拥有static关键字的变量遵守派生类的访问控制规则
2、父类中的static修饰的成员变量,在子类继承之前必须进行初始化,否则子类继承会报错;
#include
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:
};
int main()
{
B b1;
//b1.a = 200;//Error,//static 关键字遵守派生类的访问控制规则
system(“pause”);
return 0;
}