class test{
protected:
int a;
public:
test(int a = 0);
~test();
void show();
};
class B :public test {
private:
int b;
public:
B(int b = 0);
void show();
};
#include
#include "test.h"
using namespace std;
test::test(int a) {
cout << "a = " << a << endl;
cout << "this a = " << this->a << endl;
this->a = a;
cout << "基类实例化:a : " << this->a << endl;
}
test::~test() {
cout << "退出基类实例化" << endl;
}
void test::show() {
cout << "a : " << a << endl;
}
B::B(int b) : test(a)
{
cout << "派生类实例化" << endl;
cout << "b = " << b << endl;
cout << "this b = " << this->b << endl;
this->b = a;
}
void B::show() {
cout << "b : " << b << endl;
}
#include
#include "test.h"
using namespace std;
int main() {
B t1;
t1.show();
return 0;
}
在基类声明时赋值,即:
class test{
protected:
int a = 0;
public:
test(int a);
~test();
void show();
};
因为 a 没有初始化也没有赋值