virtual_boss.cpp

来自「the source of dev_c++ most of them for 」· C++ 代码 · 共 65 行

CPP
65
字号
//Virtual Boss
//Demonstrates virtual functions

#include <iostream>

using namespace std;

class Enemy
{
public:
    Enemy(int damage = 10)
    { m_pDamage = new int(damage); }

    virtual ~Enemy()
    {
        cout << "m_pDamage deleted\n";
        delete m_pDamage;
    } 

    void Taunt() const
    { cout << "The enemy says he will fight you.\n"; }  

    void virtual VTaunt() const
    { cout << "The enemy says he will fight you.\n"; }

protected:
    int* m_pDamage;
};

class Boss : public Enemy
{
public:
    Boss(int multiplier = 3)
    { m_pDamageMultiplier = new int(multiplier); }
    
    virtual ~Boss()
    {
        cout << "m_pDamageMultiplier deleted\n";
        delete m_pDamageMultiplier;
    }  

    void Taunt() const
    { cout << "The boss says he will end your pitiful existence.\n"; }  

    void virtual VTaunt() const
    { cout << "The boss says he will end your pitiful existence.\n";}
    
protected:
    int* m_pDamageMultiplier; 
};

int main()
{
    cout << "Pointer to Enemy that points to a Boss object:\n";
    Enemy* pBadGuy = new Boss();
    pBadGuy->Taunt();
    pBadGuy->VTaunt();
   
    cout << "\nDeleting pointer to Enemy:\n";
    delete pBadGuy;
    pBadGuy = 0;

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?