继承中定义一个子类:
调用构造器和析构器的顺序为?
1 基类的构造器
2.子类的构造器
3.子类的析构器
4.基类的析构器
#include <iostream>
#include <string>
class BaseClass
{
public:
BaseClass();
~BaseClass();
void doSomething();
};
class SubClass : public BaseClass
{
public:
SubClass();
~SubClass();
};
BaseClass::BaseClass()
{
std::cout << "我进入基类构造器"<<"\n";
}
BaseClass::~BaseClass()
{
std::cout << "我进入基类析构器"<<"\n";
}
void BaseClass::doSomething()
{
std::cout << "我在doSomeThing"<<"\n";
}
SubClass::SubClass()
{
std::cout << "我进入子类构造器"<<"\n";
}
SubClass::~SubClass()
{
std::cout << "我进入子类析构器"<<"\n";
}
int main()
{
SubClass subclass;
subclass.doSomething();
return 0;
}