57 - Abstract Classes and Pure v
作者:
社交帐号直接注册 | 来源:发表于
2018-01-08 17:05 被阅读0次1.
#include <iostream>
using namespace std;
class Enemy
{
public:
virtual void attack()
{
cout << "i am the enemy class" << endl;
}
};
class Ninja :public Enemy
{
public:
void attack()
{
cout << "ninja attack!" << endl;
}
};
class Monster :public Enemy
{
public:
};
int main()
{
Ninja n;
Monster m;
Enemy *enemy1 = &n;
Enemy *enemy2 = &m;
enemy1->attack();
enemy2->attack();
system("pause");
return 0;
}
2.
#include <iostream>
using namespace std;
class Enemy
{
public:
virtual void attack() = 0;
};
class Ninja :public Enemy
{
public:
void attack()
{
cout << "ninja attack!" << endl;
}
};
class Monster :public Enemy
{
public:
void attack()
{
cout << "mponster attack!" << endl;
}
};
int main()
{
Ninja n;
Monster m;
Enemy *enemy1 = &n;
Enemy *enemy2 = &m;
enemy1->attack();
enemy2->attack();
system("pause");
return 0;
}
本文标题:57 - Abstract Classes and Pure v
本文链接:https://www.haomeiwen.com/subject/zbbbnxtx.html
网友评论