和普通的构造器析构器类似,不过在继承的类中进行操作
#include <iostream>
#include <string>
class BaseClass
{
public:
BaseClass();
~BaseClass();
void doSomething();
};
class SubClass : public BaseClass
{
public:
SubClass();
~SubClass();
};
BaseClass::BaseClass()
{
std::cout<<"进入基类构造器"<<std::endl;
};
BaseClass::~BaseClass()
{
std::cout<<"进入基类析构器"<<std::endl;
};
void BaseClass::doSomething()
{
std::cout<<"doSomething做些事情"<<std::endl;
}
SubClass::SubClass()
{
std::cout<<"进入子类构造器,"<<std::endl;
}
SubClass::~SubClass()
{
std::cout<<"进入子类析构器,"<<std::endl;
}
int main()
{
SubClass subclass;
subclass.doSomething();
std::cout<<"结束"<<std::endl;
return 0;
}