C++ Premier Plus 6th edition - Programming excercise - Chapter14 - 4

有些在本例中不会用到的功能,如class Card中的函数,也尽可能详细的写上了,既然是为了练习,索性就一起练了吧。尽管会显得臃肿。
犯了好几个错误,能发现的都改过来了。
这个回头还是需要重新写一下,一是做到精简,二是看同样的错误还会不会再犯。
person.h

#ifndef PERSON_H_
#define PERSON_H_
#include<iostream>
#include<string>
#include<cstdlib> // rand(),srand() prototype
#include<ctime> // time() prototype

using std::string;
using std::cout;
using std::endl;


// declaration of class Person
class Person
{
private:
    string fname;
    string lname;
public:
	Person() { fname = "", lname = ""; };
    Person(const char*fn, const char*ln) :fname(fn), lname(ln) {};
	virtual void Show() const;
	virtual ~Person() {};
};

// declaration of Gunslinger
class Gunslinger:virtual public Person
{
private:
    double picktime;
    int nick;
public:
    Gunslinger() :Person(), picktime(0.0), nick(0) {};
    Gunslinger(const char*fn, const char*ln, double pt, int ni) :Person(fn, ln),
        picktime(pt), nick(ni) {};
    Gunslinger(Person& pe, double pt, int ni):Person(pe),
        picktime(pt), nick(ni) {};
	double Draw() const { return picktime; };
	virtual void Show()const;
	virtual ~Gunslinger() {};
};

// declaration of class Card
// not necessary to create so complicated class card
// will not use set() in main
// here I only wanna pratice
class Card
{
protected:
	// just to indicate how many colors there are
	// will never use this enum
	enum { heart, spade, club, diamond };
	// just to create a static const value for following array
	enum { pat = 4 };
private:
	// this array needs to be initialized seperately
	// as it is static
	static const char* Pat[pat];
	int fvalue;
	int pattern;// user's choice
public:
	Card()
	{
		fvalue = (rand() % 53) + 1;
		pattern = rand() % 4;
	};
	Card(int f, int p) :fvalue(f), pattern(p) {};
	int value() const { return fvalue; };
	void Show()const;
	void Set();
	~Card() {};
};

// declaration of class PokerPlayer
class PokerPlayer :virtual public Person
{
private:
    Card rdom;
public:
    PokerPlayer() :Person(), rdom() {};
	PokerPlayer(const char*fn, const char*ln, Card& cd) :Person(fn, ln), rdom(cd) {};
	int Draw() const { return rdom.value(); };
	virtual void Show() const;
	virtual ~PokerPlayer(){};
};

// declaration of class BadDude
// the base-classes have same ancestor, so we use virtual base-class
class BadDude :virtual public Gunslinger, virtual public PokerPlayer
{
public:
    BadDude() :Gunslinger(), PokerPlayer() {};
	BadDude(const char*fn, const char*ln, double pt, int ni, Card& cd) :
		Person(fn,ln),Gunslinger(fn, ln, pt, ni), PokerPlayer(fn, ln, cd) {};
    double Gdraw()const;
    int Cdraw()const;
	virtual void Show() const;
	virtual ~BadDude() {};
};

#endif

person.cpp

//implementations
#include<iostream>
#include"person.h"

// class Person
void Person::Show() const
{
	cout << fname << " " << lname << endl;
}

// class Gunslinger
void Gunslinger::Show() const
{
	Person::Show();
	cout << "The Gunslinger's picktime: " << picktime << endl
		<< "The Gunslinger's nicks: " << nick << endl;
};



// class Card
const char* Card::Pat[] = { "heart", "spade", "club", "diamond" };
void Card::Set()
{
    // display options to user
    cout << "please enter a number to make a choice:\n";
    int i;
    for (i = 0; i < pat; i++)
    {
        cout << Pat[i] << "  ";
        if (i % 3 == 2)// new line operator when there are 3 (0,1,2) in one line
            cout << endl;
    }
    if (i % 3 != 0)
        cout << endl;
    // ask user to make option
    while (std::cin >> pattern && (pattern<0 || pattern>pat))
        cout << "Please enter a value >=0 and < " << pat << endl;
    // clear input in case user input too much numbers
    while (std::cin.get() != '\n')
        continue;
}
void Card::Show() const
{
    cout << "Face value: " << fvalue << endl
         << "Pattern: " << pattern << endl;
}

// class PokerPlayer
void PokerPlayer::Show() const
{
	Person::Show();
	rdom.Show();
}

// class BadDue
double BadDude::Gdraw() const
{
    return Gunslinger::Draw();
}
int BadDude::Cdraw() const
{
    return PokerPlayer::Draw();
}
void BadDude::Show() const
{
	// include show() of Person, as show() of Gunslinger contians base-class's show()
	// PokerPlayer use show() of base-class
	Gunslinger::Show();
	// below part is for PokerPlayer
	cout << "Next poker face value: " << Cdraw() << endl;
}

main.cpp

#include <iostream>
#include <cstring>
#include "person.h"

const int SIZE = 5;

int main()
{
	std::srand(std::time(0)); // randomize rand()
	using std::cin;
    using std::cout;
    using std::endl;
    using std::strchr;
    Person * lolas[SIZE];
    int ct;
	Card temp;
	Card temp1;
    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("GunMan","Kush",0.9,15);
            break;
        case 'p':
            lolas[ct] = new PokerPlayer("PolerMan","Push",temp);
            break;
        case 'b':
			lolas[ct] = new BadDude("BadMan", "Dush", 0.8, 25, temp1);
            break;
        }
        cin.get();
    }
    cout << "\nHere is your staff:\n";
    int i;
    for (i = 0; i < ct; i++)
    {
        cout << endl;
        lolas[i]->Show();
    }
    // make sure release heap zone ct times
	for (i = 0; i < ct; i++)
        delete lolas[i];
    cout << "Bye.\n";
	cin.get();
	cin.get();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值