// File: ex3-7.cpp - card and deck classes #include #include // needed for rand() function using namespace std; const char* value_name[13] = {"two","three","four","five","six", "seven","eight","nine","ten","jack","queen","king","ace"}; const char* suit_name[4] = {"clubs","diamonds","hearts","spades"}; class card { private: int value; int suit; public: void assign(int); int get_value() const; // accessor function int get_suit() const; // accessor function void print() const; }; int card::get_value() const { return value; } int card::get_suit() const { return suit; } void card::assign(int x) { value = x % 13; suit = x / 13; } void card::print() const { cout << (value_name[value]) << " of " << (suit_name[suit]) << endl; } class deck { private: card* d; int next_card; public: void create_deck(); void shuffle(); void deal(int numberOfCardsToDeal = 5); void print() const; card* getD() const { return d; } void release() { delete [] d; d = nullptr; } }; void deck::create_deck() { d = new card[52]; for (int i = 0; i < 52; i++) { d[i].assign(i); } next_card = 0; } void deck::shuffle() { int i, k; card temp; cout << "I am shuffling the deck\n"; for (i = 0; i < 52; i++) { k = rand() % 52; temp = d[i]; d[i] = d[k]; d[k] = temp; } } void deck::print() const { cout << "\nHere's the deck:\n"; for (int i = 0; i < 52; i++) d[i].print(); } 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(); } int main () { deck poker; poker.create_deck(); poker.print(); poker.shuffle(); poker.print(); poker.deal(); poker.deal(3); poker.release(); }