#include
using namespace std;
class A {
public:
static int a;
int b;
//A() { a = 0; }
void print() {
cout << "父类A" << endl;
cout << a << endl; // ok
}
protected:
private:
};
// 这句话的意思是 给静态变量初始化0
// 实际上告诉C++编译器为整个类分配4个字节内存空间
//int A::a = 0; // tag_1
class B : private A {
public:
int b;
int c;
void print() {
cout << "子类B" << endl;
cout << b << endl;
// 需要tag_1处的初始化,下面的代码才能编译通过
//cout << a << endl; //Error: LNK2001 无法解析的外部符号 "public: static int A::a" (? a@A@@2HA) Cplusplus继承
}
protected:
private:
};
void main() {
B b1;
b1.print();
system("pause");
}
暑期编程PK赛
得CSDN机械键盘等精美礼品!