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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值