子类继承父类后,当创建子类对象,也会调用父类的构造函数
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include<iostream> using namespace std;
class Base { public: Base() { cout << "Base的构造函数" << endl; } ~Base(){ cout << "Base的析构函数" << endl; } };
class son :public Base { public: son() { cout << "son的构造函数" << endl; } ~son() { cout << "son的析构函数" << endl; } };
int main() { son s; return 0; }
|