美文网首页
[GeekBand][C++面向对象高级编程]第五周作业

[GeekBand][C++面向对象高级编程]第五周作业

作者: 散夜霜 | 来源:发表于2017-02-20 22:56 被阅读0次

测试结果

上结果,当调用Apple的构造函数时,会先调用Fruit的构造函数。
当调用new时,会依次调次operator new()、转型和构造函数。
当调用delete时,会依次调用析构函数和operator delete()
值得一提的是,当指针的基本类型没有重载operator delete()函数时,在delete该指针时,将会调用::operator delete(由结果可见)

(这里有个'\n',被简书省略)
==========
        Code:
        Fruit* pFa = new Fruit;
----------
Global new() was called.
The ctor of class Fruit was called.

==========
        Code:
        Fruit* pFb = new Apple;
----------
The new() of class Apple was called.
The ctor of class Fruit was called.
The ctor of class Apple was called.

==========
        Code:
        Apple* pAc = new Apple;
----------
The new() of class Apple was called.
The ctor of class Fruit was called.
The ctor of class Apple was called.

==========
Code:
        delete pFa;
----------
The dtor of class Fruit was called.
Global delete() was called.

==========
Code:
        delete pFb;
----------
The dtor of class Fruit was called.
Global delete() was called.

==========
Code:
        delete pAc;
----------
The dtor of class Apple was called.
The dtor of class Fruit was called.
The delete() of class Apple was called.
请按任意键继续. . .

测试代码

文件1:Fruit.h
#ifndef __MYFRUIT__
#define __MYFRUIT__

#include <iostream>

void* operator new(size_t size)
{
    std::cout << "Global new() was called." << std::endl;
    return malloc(size);
}

void operator delete(void* pdead)
{
    std::cout << "Global delete() was called." << std::endl;
    return free(pdead);
}

class Fruit
{
private:
    int no;
    double weight;
    char key;
public:
    Fruit()
        :no(0), weight(0), key('\0') 
    { std::cout << "The ctor of class Fruit was called." << std::endl; }
    ~Fruit()
    { std::cout << "The dtor of class Fruit was called." << std::endl; }
    void print() {}
    virtual void process() {}
};

class Apple :public Fruit
{
private:
    int size;
    char type;
public:
    Apple()
        :size(0), type('M')
    { std::cout << "The ctor of class Apple was called." << std::endl; }
    ~Apple()
    { std::cout << "The dtor of class Apple was called." << std::endl; }
    void* operator new(size_t size)
    {
        std::cout << "The new() of class Apple was called." << std::endl;
        return malloc(size);
    }
    void operator delete(void* pdead)
    {
        std::cout << "The delete() of class Apple was called." << std::endl;
        return free(pdead);
    }
    void save() {}
    virtual void process() {}
};

#endif
文件2:Fruit-test.cpp
#include <iostream>
#include "Fruit.h"

int main()
{
    std::cout << R"(
==========
    Code:
    Fruit* pFa = new Fruit;
----------
)";
    Fruit* pFa = new Fruit;
    
    std::cout << R"(
==========
    Code:
    Fruit* pFb = new Apple;
----------
)";
    Fruit* pFb = new Apple;
    
    std::cout << R"(
==========
    Code:
    Apple* pAc = new Apple;
----------
)";
    Apple* pAc = new Apple;

    std::cout << R"(
==========
Code:
    delete pFa;
----------
)";
    delete pFa;
    
    std::cout << R"(
==========
Code:
    delete pFb;
----------
)"; 
    delete pFb;
    
    std::cout << R"(
==========
Code:
    delete pAc;
----------
)"; 
    delete pAc;

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:[GeekBand][C++面向对象高级编程]第五周作业

      本文链接:https://www.haomeiwen.com/subject/sbfywttx.html