美文网首页
57 - Abstract Classes and Pure v

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