编写一个游戏平台。功能描述:
程序启动后,显示游戏列表(至少有三个游戏)
游戏1
游戏2
游戏3
用户选择其中一个游戏后,进入该游戏。游戏结束后,回到游戏列表。游戏列表按照游戏访问量动态排序。
定义游戏1类、游戏2、游戏3类,公有继承自CGame。游戏类自行设计,必须提供void start()作为游戏入口。
上面的要求如何实现???
#include<iostream>
#include<algorithm>
using namespace std;
class Game
{
private:
char* name;
int visitCount;
public:
Game()
{
visitCount = 0;
}
int GetVisitCount()
{
return visitCount;
}
void addvisitCount();
void ShowName();
virtual void start() = 0;
};
void Game::ShowName()
{
cout << name << endl;
}
class GGame1:public Game
{
private:
public:
virtual void start();
};
void GGame1::start()
{
cout << "欢迎进入游戏" << endl;
}
class GGame2:public Game
{
private:
public:
virtual void start();
};
void GGame2::start()
{
cout << "欢迎进入游戏" << endl;
}
class GGame3:public Game
{
private:
public:
virtual void start();
};
void GGame3::start()
{
cout << "欢迎进入游戏" << endl;
}
bool cmp(Game& a,Game& b)
{
return a.GetVisitCount() > b.GetVisitCount();
}
void menu(Game& gaems) // 显示游戏列表
{
for (int i = 0; i < 3; i++) (gaems).ShowName();
}
int main()
{
Game *games[3];
games[0] = new GGame1;//为什么可以用基类指针数组来new一个派生类??
games[1] = new GGame2;
games[2] = new GGame3;
do
{
/*sort(games,games+3,cmp); 将游戏列表按访问量排序*/
//如何实现类指针数组的排序??
menu(games);
int choice;
cin >> choice;
// 用户输入选择
if (choice >= 0 && choice <= 2)
{
int i = choice;
(*games)[i].start();
}
else
{
break; // 退出系统
}
} while (true);
}