父类和子类的析构顺序主要是

  父类构造函数->子类构造函数->执行main里的函数调用->子类析构器->父类析构器 

//
// Created by qq302 on 2020/7/19.
//
#include <iostream>
#include <string>

class BaseClass{
public:
    BaseClass();
    ~BaseClass();
    void dosomething();

    void doSomething();
};

class SubClass : public BaseClass {
public:
    SubClass();
    ~SubClass();
};

BaseClass::BaseClass() {
    std::cout << "进入基类构造器...'\n'";
    std::cout << "我在基类构造器里面干了些事...\n";
}

BaseClass::~BaseClass() {
    std::cout << "进入基类析构器...\n";
    std::cout << "我在基类析构器也干了某些事....\n";
}

void BaseClass::doSomething() {
    std::cout << "我干了某些事...\n";
}

SubClass::SubClass() {
    std::cout << "进入子类构造器" << std::endl;
    std::cout << "我在子类构造器里还干了某些事" << std::endl;
}

SubClass::~SubClass() {
    std::cout << "进入子类析构器" << std::endl;
}

int main() {

    SubClass subclass;
    subclass.doSomething();

    std::cout << "完事收工" << std::endl;
    return 0;
}

 

posted on 2020-07-19 13:41  python我的最爱  阅读(1770)  评论(1编辑  收藏  举报