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();};

版权声明:本文为博主原创文章,未经博主允许不得转载。

,生命中,每一种苦难的背后都有一片晴朗的天空

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

相关文章:

你感兴趣的文章:

标签云: