C++虚析构和纯虚析构

 

 

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    //纯虚函数
    virtual void speak() = 0;

};

class Cat : public Animal {
public:
    void speak() {
        cout << "cat speak" << endl;
    }

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat();
    a->speak();
    delete a;
}

//下面是有问题代码

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

    ~Animal() {
        cout << "Animal 析构函数" << endl;
    }

    //纯虚函数
    virtual void speak() = 0;

};

class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak

1、利用虚析构函数解决此问题

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
    virtual   ~Animal() {
        cout << "Animal 虚析构函数" << endl;
    }


    //纯虚函数
    virtual void speak() = 0;

};


class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak
Cat 析构函数
Animal 虚析构函数

2、利用纯虚析构函数 解决此问题

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
//    virtual   ~Animal() {
//        cout << "Animal 虚析构函数" << endl;
//    }

    //或者纯虚析构  二者选其一
    //有了纯虚析构 之后,这个类也属于抽象类 无法实例化
    virtual  ~Animal() = 0;

    //纯虚函数
    virtual void speak() = 0;

};

Animal::~Animal() {
    cout << "Animal 纯虚析构函数" << endl;
}

class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak
Cat 析构函数
Animal 纯虚析构函数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值