示例
#include<iostream>
using namespace std;
class father {
public:
father() {
cout << "fater" << endl;
}
~father() {
cout << "fater" << endl;
}
};
class son:public father {
public:
int num;
son(int num) {
this->num = num;
cout << "son" << endl;
}
~son() {
cout << "son" << endl;
}
};
class other {
public:
other() {
cout << "other" << endl;
}
~other() {
cout << "other" << endl;
}
};
class grandson:public son {
public:
grandson(int a=10):son(a) {
cout << "grandson" << endl;
}
~grandson() {
cout << "grandson" << endl;
}
other o1;
};
void test() {
grandson s(100);
cout << s.num << endl;
}
int main() {
test();
system("pause");
return EXIT_SUCCESS;
}
运行结果
fater
son
other
grandson
100
grandson
other
son
fater