📄 virtual_boss.cpp
字号:
//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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -