头文件1:hero.h
#ifndef HERO_H_
#define HERO_H_
#include "skin.h"
#include <iostream>
using namespace std;
class Hero
{
protected:
char * name;
Skin * sk;
public:
~Hero()
{
delete sk;
}
char * get_name()
{
return name;
}
Skin * get_skin()
{
return sk;
}
virtual void show_skin() = 0;
virtual void set_skin() = 0;
virtual void show() = 0;
virtual void skill1() = 0;
virtual void skill2() = 0;
virtual void skill3() = 0;
};
class Zhaoyun:public Hero
{
public:
Zhaoyun()
{
name = "赵云";
sk = NULL;
}
void show_skin()
{
cout << "1.忍.炎影" << endl;
cout << "2.未来纪元" << endl;
cout << "3.引擎之心" << endl;
}
void set_skin()
{
int cmd;
mean:
show_skin();
cin >> cmd;
switch(cmd)
{
case 1:sk = new Zhaoyun_Skin1;
break;
case 2:sk = new Zhaoyun_Skin2;
break;
case 3:sk = new Zhaoyun_Skin3;
break;
default:goto mean;
}
}
void show()
{
cout << "赵云出场" << endl;
}
void skill1()
{
cout << "赵云使用了 惊雷之龙" << endl;
}
virtual void skill2()
{
cout << "赵云使用了 破云之龙" << endl;
}
virtual void skill3()
{
cout << "赵云使用了 天翔之龙" << endl;
}
};
class Zhugeliang:public Hero
{
public:
Zhugeliang()
{
name = "诸葛亮";
sk = NULL;
}
void show_skin()
{
cout << "1.黄金分割率" << endl;
cout << "2.武陵仙君" << endl;
}
void set_skin()
{
int cmd;
mean:
show_skin();
cin >> cmd;
switch(cmd)
{
case 1:sk = new Zhugeliang_Skin1;
break;
case 2:sk = new Zhugeliang_Skin2;
break;
default:goto mean;
}
}
void show()
{
cout << "诸葛亮出场" << endl;
}
void skill1()
{
cout << "诸葛亮使用了 东风破袭" << endl;
}
virtual void skill2()
{
cout << "诸葛亮使用了 时空穿梭" << endl;
}
virtual void skill3()
{
cout << "诸葛亮使用了 元气弹" << endl;
}
};
#endif
头文件2:skin.h
#ifndef SKIN_H_
#define SKIN_H_
#include <iostream>
using namespace std;
class Skin
{
protected:
char * skin_name;
public:
virtual void show() = 0;
char * get_skin_name()
{
return skin_name;
}
};
class Zhaoyun_Skin1:public Skin
{
public:
Zhaoyun_Skin1()
{
skin_name = "赵云:忍.炎影";
}
void show()
{
cout << skin_name << endl;
}
};
class Zhaoyun_Skin2:public Skin
{
public:
Zhaoyun_Skin2()
{
skin_name = "赵云:未来纪元";
}
void show()
{
cout << skin_name << endl;
}
};
class Zhaoyun_Skin3:public Skin
{
public:
Zhaoyun_Skin3()
{
skin_name = "赵云:引擎之心";
}
void show()
{
cout << skin_name << endl;
}
};
class Zhugeliang_Skin1:public Skin
{
public:
Zhugeliang_Skin1()
{
skin_name = "诸葛亮:黄金分割率";
}
void show()
{
cout << skin_name << endl;
}
};
class Zhugeliang_Skin2:public Skin
{
public:
Zhugeliang_Skin2()
{
skin_name = "诸葛亮:武陵仙君";
}
void show()
{
cout << skin_name << endl;
}
};
#endif
.主文件:main.cpp
#include "skin.h"
#include "hero.h"
#include <iostream>
using namespace std;
Hero * selecthero()
{
Hero * hero;
hero = NULL;
mean:
cout << "请选择英雄:" << endl;
cout << "1.赵云" << endl;
cout << "2.诸葛亮" << endl;
int cmd;
cin >> cmd;
switch (cmd)
{
case 1:
hero = new Zhaoyun;
break;
case 2:
hero = new Zhugeliang;
break;
default:
goto mean;
}
return hero;
}
void selectskin(Hero * hero)
{
cout << " 你选择了" << hero->get_name() << ", 请选择皮肤:" << endl;;
hero->set_skin();
}
void gamestart(Hero * hero)
{
cout << "游戏已开始,你选择了英雄:" << hero->get_name() << endl;
cout << "你选择了皮肤:" << hero->get_skin()->get_skin_name()<< endl;
hero->show();
char cmd;
cout << "按q使用1技能;按w使用二技能;按e使用3技能;按r结束游戏!" << endl;
while (1)
{
cin >> cmd;
switch (cmd)
{
case 'q':hero->skill1();
break;
case 'w':hero->skill2();
break;
case 'e':hero->skill3();
break;
case 'r':cout << "游戏结束" << endl;
delete hero;
return;
default:
break;
}
}
}
int main()
{
cout << "游戏开始" << endl;
Hero * hero = selecthero();
selectskin(hero);
gamestart(hero);
system("pause");
return 0;
}
效果演示截图:
备注:基于VS2010的编程