#include <iostream>
using namespace std;
/*多态分為靜态多态和動态多态:
*靜态多态是編譯時發生了多态,多指函數重載和模闆實作;
*動态多态是運作時多态,一般通過虛函數實作。
*
*實作動态多态三個條件
*1:要有public繼承;
*2:要有虛函數重寫;
*3:用父類指針(父類引用)指向子類對象
*/
class HeroPlane
{
public:
virtual int power()
{
return 10;
}
};
class EnemyPlane
{
public:
virtual int power()
{
return 15;
}
};
class advPlane : public HeroPlane
{
public:
int power()
{
return 20;
}
};
class advPlane2 : public HeroPlane
{
public:
int power()
{
return 30;
}
};
void Combat(HeroPlane *hp, EnemyPlane *ep)
{
if (hp->power() > ep->power())
{
cout << "hero win..." << endl;
}
else
{
cout << "hero lose..." << endl;
}
}
int main()
{
HeroPlane h1;
EnemyPlane e;
Combat(&h1, &e);
advPlane h2;
Combat(&h2, &e);
advPlane2 h3;
Combat(&h3, &e);
system("pause");
return 0;
}