PERSIN.h
#pragma once
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
class Person {
private:
std::string fname;
std::string sname;
public:
Person();
Person(const std::string & f, const std::string & s);
virtual ~Person() {};//记得定义虚析构函数,即使它什么都不做
virtual void Set();
virtual void Show()const;
};
class Gunslmger :virtual public Person {
private:
double times;
int digit;//枪手枪上的刻痕数
public:
Gunslmger(const std::string f="null", const std::string s="null", double t=0.0, int d=0);
Gunslmger(double time, int di);
virtual ~Gunslmger() {};
void Set();
double Draw();//返回枪手拔枪时间
virtual void Show()const;
};
class PokerPlays : virtual public Person {
public:
virtual ~PokerPlays() {};
PokerPlays(const std::string f="null",const std::string s="null");
int Draw();//返回一个1-52的随机数显示扑克牌
virtual void Show()const;//使用Person的Show方法
};
class BadDude :public Gunslmger, public PokerPlays {
private:
double Badtime;
public:
BadDude(const std::string f="null",const std::string s="null",double ba=0.0);
double Gdraw();//返回坏蛋的拔枪时间
int Cdraw();//返回下一张扑克牌
void Set();
void Show()const;
};
#endif
PERSON.cpp
#include "PERSON.h"
#include <iostream>
#include <string>
Person::Person()
{};
Person::Person(const std::string & f, const std::string & s)
{
fname = f;
sname = s;
}
void Person::Set()
{
using std::cout;
using std::cin;
cout << "Please enter a fistname: ";
getline(cin, fname);
cout << "Please enter a secondname: ";
getline(cin, sname);
}
void Person::Show() const
{
using std::cout;
using std::endl;
cout << fname << endl
<< sname << endl;
}
Gunslmger::Gunslmger(const std::string f, const std::string s, double t, int d) {};
Gunslmger::Gunslmger(double time, int di) :Person(), times(time), digit(di)
{
}
void Gunslmger::Set()
{
Person::Set();
std::cout << "Please enter a time of Gun: ";
std::cin >> times;
std::cout << "Please enter a digit: ";
std::cin >> digit;
while (std::cin.get() != '\n')
continue;
}
double Gunslmger::Draw()
{
return times;
}
void Gunslmger::Show() const
{
Person::Show();
std::cout << "time: " << times << std::endl
<< "Digit: " << digit << std::endl;
}
PokerPlays::PokerPlays(const std::string f, const std::string s):Person(f,s)
{
}
int PokerPlays::Draw()
{
int re = rand() % 52 + 1;
return re;
}
void PokerPlays::Show() const
{
Person::Show();
}
BadDude::BadDude(const std::string f, const std::string s, double ba)
:Person(f,s),Badtime(ba)
{
}
double BadDude::Gdraw()
{
return Badtime;
}
int BadDude::Cdraw()
{
return PokerPlays::Draw();
}
void BadDude::Set()
{
Person::Set();
std::cout << "Please enter a time of BadDude: ";
std::cin >> Badtime;
while (std::cin.get() != '\n')
continue;
}
void BadDude::Show() const
{
Gunslmger::Show();
std::cout << "Badtime: " << Badtime << std::endl;
}
main.cpp
#include <iostream>
#include <ctime>
#include "PERSON.h"
const int Size = 5;
int main()
{
srand(time(0));
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
Person * temp[Size];
int n;
char ch;
for (n = 0; n < Size; n++)
{
cout << "Enter the employee:\n"
<< "w: Person s: Gun\n"
<< "t: Bad q: quit\n";
cin >> ch;
while (strchr("wstq", ch) == nullptr)
{
cout << "Please enter a w, s,t or q";
cin >> ch;
}
if (ch == 'q')
break;
switch (ch)
{
case 'w': temp[n]= new Person;
break;
case 's': temp[n] = new Gunslmger;
break;
case 't': temp[n] = new BadDude;
}
cin.get();
temp[n]->Set();
}
cout << "\nHere is your staff:\n";
int i;
for (i = 0; i < n; i++)
{
cout << endl;
temp[i]->Show();
}
for (i = 0; i < n; i++)
delete temp[i];
cout << "Bye\n";
cin.get();
return 0;
}