// card-class.cpp #include #include #include using namespace std; // A playing card is one of 4 suits const int HEARTS = 0; const int CLUBS = 1; const int SPADES = 2; const int DIAMONDS = 3; const int NUM_SUITS = 4; // and 13 ranks (A,2,3,4,5,6,7,8,9,10,J,Q,K) const int ACE = 1; const int JACK = 11; const int QUEEN = 12; const int KING = 13; const int NUM_RANKS = 13; const int NUM_CARDS = NUM_SUITS * NUM_RANKS; // 52 // Define my own type for a playing card class Card { int rank; // private by default int suit; public: Card(int r, int s); // Constructor declaration void print(); // member function aka method }; // note semi-colon // Define the << operator for Card. ostream& operator<< (ostream& out, Card& c) { c.print(); } Card::Card(int r, int s) // Constructor definition { rank = r; suit = s; } void Card::print() { switch(rank) { case ACE: cout << "Ace"; break; case JACK: cout << "Jack"; break; case QUEEN: cout << "Queen"; break; case KING: cout << "King"; break; default: cout << rank; break; } cout << " of "; switch(suit) { case HEARTS: cout << "hearts"; break; case CLUBS: cout << "clubs"; break; case SPADES: cout << "spades"; break; case DIAMONDS: cout << "diamonds"; break; } } int main() { // Initialize the random numbers srand(time(NULL)); // Declare a variable of type Card: Card c (3, CLUBS); c.print(); cout << "\n"; // Looks nicer with primitive types double pi = 3.14159; cout << pi << "\n"; // I'd LIKE to be able to do this: cout << "Is your card the " << c << "?\n"; return 0; }