C++primer plus第六版课后编程题答案14.4

这道题我写的比较简单,想要复杂功能的自己写一下吧

Person.cpp

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Person{
private:
	string name;
	string xing;
public:
	Person(string n="陈",string x="某某"):name(n),xing(x){};
	virtual void show()=0{
		cout<<"姓名:"<<xing<<" "<<name<<endl;
	};//纯虚函数
};
class Gunslinger:virtual public Person
{
private:
	int Gunnumber;
public:
	Gunslinger(string n="枪王",string x="枪",int g=5):Person(n,x),Gunnumber(g){}
	double Draw()
	{
		return Gunnumber/5.0;//默认拔枪时间为Gunnumber/5
	}
	virtual void show()
	{
		Person::show();
		cout<<"把枪时间:"<<Draw();
	}
};

class PokerPlayer:virtual public Person{
private:
	int val;
public:
	PokerPlayer(string n="赌神",string x="扑克牌",int v=100):Person(n,x),val(v){
		set();
	}
	void set()
	{
		val=rand()%52;
	}
	double Draw()
	{	
		return val;
	}
	virtual void show()
	{
		Person::show();
		cout<<"牌点:"<<(int)Draw();
	}
};

class BadDude:public Gunslinger,public PokerPlayer{
public:
	double Gdraw()
	{
		return Gunslinger::Draw();
	}
	double Cdraw()
	{
		return PokerPlayer::Draw();
	}
	void show()
	{
		Person::show();
		cout<<"时间:"<<Gunslinger::Draw()<<endl;
		cout<<"牌点:"<<PokerPlayer::Draw()<<endl;
	}

};

main144.cpp

#include <iostream>
#include "Person.cpp"
#include <string>
using namespace std;
const int SIZE=5;
void main144()
{
	
	Person *lolas[SIZE];
	int ct;
	for(ct=0;ct<SIZE;ct++)
	{
		char choice;
		cout<<"Enter the employee category:"<<endl
		<<"g:Gunslinger		 p:PokerPlayer"<<endl
		<<"b:BadDude    q:quit"<<endl;
		cin>>choice;
		while(strchr("gpbq",choice)==NULL)//p564关于strchr有解释
		{		
			cout<<"Please enter a w,s,t or q:";
			cin>>choice;
		}
		if(choice=='q')
			break;
		switch(choice)
		{
			//这里我为了方便,都采用默认初始化了
		case 'p':lolas[ct]=new PokerPlayer();break;
		case 'g':lolas[ct]=new Gunslinger();break;
		case 'b':lolas[ct]=new BadDude();break;
		}
		cin.get();	
	}
	cout<<"Here is your staff:"<<endl;
	int i;
	for(i=0;i<ct;i++)
	{
		lolas[i]->show();
		cout<<endl;
	}

	cout<<endl<<endl;
	
	cin.get();

};


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值