// File: ex4-5.cpp #include #include // needed for rand() function using namespace std; const string ValueName[13] = {"two","three","four","five","six", "seven","eight","nine","ten","jack","queen","king","ace"}; const string SuitName[4] = {"clubs","diamonds","hearts","spades"}; class Card { private: int value; int suit; public: Card(); Card(int); int get_value() const { return value;} // accessor function int get_suit() const { return suit;} // accessor function void print() const; }; int G = 0; Card::Card() : value(abs(G)%52%13), suit(abs(G)%52/13) { G++; cout << G << ' '; print(); } Card::Card(int x) : value(abs(x)%52%13), suit(abs(x)%52/13) { } void Card::print() const { cout << (ValueName[value]) << " of " << (SuitName[suit]) << endl; } class Deck { private: Card* d; int size; int next_Card; public: Deck(int s = 52); // default ctor ~Deck(); void shuffle(); void deal(int=5); void print(); }; Deck::Deck(int s) : d(new Card[s]), size(s), next_Card(0) { // for (int i = 0; i < size; i++) d[i] = Card(i); // Card(i) creates and anonymous Card object } Deck::~Deck(void) { // for (int i = 0; i < size; i++) delete d[i]; delete [] d; cout << "The Deck is gone" << endl; } void Deck::shuffle(void) { int i, k; Card temp; cout << "I am shuffling the Deck\n"; for (i = 0; i < size; i++) { k = rand() % size; temp = d[i]; d[i] = d[k]; d[k] = temp; } return; } void Deck::print(void) { for (int i = 0; i < size; i++) d[i].print(); return; } void Deck::deal(int no_of_Cards) { cout << "\nOk, I will deal you " << no_of_Cards << " Cards:\n"; for (int i = 0; i < no_of_Cards; i++) d[next_Card++].print(); return; } int main (void) { Deck poker; // poker.shuffle(); poker.print(); poker.deal(); poker.deal(3); return 0; }