📄 insect.cpp
字号:
#include <iostream>
#include <string>
using namespace std ;
class Insect
{
public:
// Public methods
virtual bool CanFly() const;
};
bool Insect::CanFly() const
{
return false;
}
class Dragonfly : public Insect
{
public:
// Overriden method
bool CanFly() const;
};
bool Dragonfly::CanFly() const
{
return true;
}
class Beetle : public Insect
{ };
class Ant : public Insect
{ };
void main()
{
Beetle ABeetle;
Ant AnAnt;
Dragonfly ADragonFly;
// Fill up our array of bug pointers
Insect* InsectArray[] = { &AnAnt, &ADragonFly, &ABeetle };
// Ask each insect if it can fly
for (int loop = 0; loop < 3; loop++)
{
cout << "Insect #" << loop+1;
// Here's the polymorphic method call
if (InsectArray[loop]->CanFly() == true)
cout << " can ";
else
cout << " cannot ";
cout << "fly.\n";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -