c++第十四章第四题

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
class Person
{
    
    
public:
 Person() {
    
     lastname = '\0', firstname = '\0'; }
 ~Person() {
    
    };
 Person(const std::string& fun, const std::string& fin)
  :lastname(fun),firstname(fin){
    
    }
 virtual void Show()const = 0;
 virtual void Set() = 0;
protected:
 virtual void Get();
 virtual void Data()const;
private:
 std::string lastname;
 std::string firstname;
};
class Gunslinger:virtual public Person
{
    
    
public:
 Gunslinger():Person(),time(0.0),henji(0) {
    
    }
 Gunslinger(const std::string& fun, const std::string& fin, double d, int i) 
  :Person(fun, fin), time(d), henji(i) {
    
    }
 Gunslinger(const Person& pe, double d, int i) :Person(pe), time(d), henji(i) {
    
    }
 double Drawtime() {
    
     return time; }
 void Show()const;
 void Set();
protected:
 void Data()const;
 void Get();
private:
 double time;
 int henji;
};
class PokerPlayer:virtual public Person
{
    
    
public:
 PokerPlayer() :Person(), card(1) {
    
    }
 PokerPlayer(const std::string& fun, const std::string& fin, int c) 
  :Person(fun, fin), card(c) {
    
    }
 PokerPlayer(const Person& pe, int c) :Person(pe), card(c) {
    
    }
 int Drawcard() {
    
     return card; }
 void Show()const;
 void Set();
protected:
 void Data()const;
 void Get();
private:
 int card;
};
class BadDude : public Gunslinger, public PokerPlayer
{
    
    
public:
 BadDude() {
    
    }
 BadDude(const std::string& fun, const std::string& fin, double d, int i, int c) 
  :Person(fun, fin), Gunslinger(fun, fin, d, i), PokerPlayer(fun, fin, c) {
    
    }
 BadDude(const Person& pe, double d, int i, int c)
  :Person(pe), Gunslinger(pe, d, i), PokerPlayer(pe, c) {
    
    }
 BadDude(const Gunslinger& gu, int c)
  :Person(gu), Gunslinger(gu), PokerPlayer(gu, c) {
    
    }
 BadDude(PokerPlayer& po, double d, int i)
  :Person(po), Gunslinger(po, d, i), PokerPlayer(po) {
    
    }
 double GDraw() {
    
     return Drawtime(); }
 int CDraw() {
    
     return Drawcard(); }
 void Show()const;
 void Set();
protected:
 void Data()const;
 void Get();
};
#endif
#include "diyi.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void Person::Data() const
{
    
    
    cout << "First name: " << firstname << endl;
    cout << "Last name: " << lastname << endl;
}
void Person::Get()
{
    
    
    cout << "Enter perosn's firstname: ";
    getline(cin, firstname);
    cout << "Enter perosn's lastname: ";
    getline(cin, lastname);
}
void Gunslinger::Set()
{
    
    
    Person::Get();
    Get();
    cout << endl;
}
void  Gunslinger::Show() const
{
    
    
    cout << "Category: Gunslinger\n";
    Person::Data();
    Data();
}
void Gunslinger::Data() const
{
    
    
    cout << "The time: " << time << endl;
    cout << "The number of scotch: " << henji << endl;
}
void Gunslinger::Get()
{
    
    
    cout << "Enter gunslinger's time: ";
    cin >> time;
    cout << "Enter gunslinger's number of scotch: ";
    cin >> henji;
    while (cin.get() != '\n')
        continue;
}
void PokerPlayer::Set()
{
    
    
    Person::Get();
    Get();
    cout << endl;
}
void PokerPlayer::Show() const
{
    
    
    cout << "Gategory: PokerPlayer\n";
    Person::Data();
    Data();
}
void PokerPlayer::Data() const
{
    
    
    cout << "The card number: " << card << endl;
}
void PokerPlayer::Get()
{
    
    
    cout << "Enter PokerPlayer's card: ";
    cin >> card;
    while (cin.get() != '\n')
        continue;
}
void BadDude::Data() const
{
    
    
    Gunslinger::Data();
    PokerPlayer::Data();
}
void BadDude::Get()
{
    
    
    Gunslinger::Get();
    PokerPlayer::Get();
}
void BadDude::Set()
{
    
    
    Person::Get();
    Get();
    cout << endl;
}
void BadDude::Show() const
{
    
    
    Person::Data();
    Data();
}
#include <iostream>
#include <cstring>
#include "diyi.h"
const int SIZE = 5;
int main()
{
    
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::strchr;
    Person* lolas[SIZE];
    int ct;
    for (ct = 0; ct < SIZE; ct++)
    {
    
    
        char choice;
        cout << "Enter the employee category:\n"
            << "g: Gunslinger p: PokerPlayer  "
            << "b: BadDude q: Quit\n";
        cin >> choice;
        while (strchr("gpbq", choice) == NULL)
        {
    
    
            cout << "Please enter a g, p, b or q: ";
            cin >> choice;
        }
        if (choice == 'q')
            break;
        switch (choice)
        {
    
    
        case 'g':   lolas[ct] = new Gunslinger;
            break;
        case 'p':   lolas[ct] = new PokerPlayer;
            break;
        case 'b':   lolas[ct] = new BadDude;
            break;
        }
        cin.get();
        lolas[ct]->Set();
    }
     cout << "\nHere is your person:\n";
    int i;
    for (i = 0; i < ct; i++)
    {
    
    
        cout << endl;
        lolas[i]->Show();
    }
    for (i = 0; i < ct; i++)
        delete lolas[i];
    cout << "Bye.\n";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wode_0828/article/details/108763859