An array is an indexed collection of data elements of the same type.
An array declaration takes the following form:
typename variableName[n]; This declares variableName to be an array of n elements of type typeName, indexed from 0 to n-1. These elements are named variableName[0] through variableName[n-1]. Ex. int scores[5]; |
A type may be given a name by using a typedef declaration. A typedef is written exactly as if we were making a variable declaration, except that
Ex. typedef char CharString[20]; |
When using a string to initialize an array of characters, don't forget the terminating '\0' character.
Ex. char greeting[6] = "hello"; |
If a user-defined type has default constructors (with no arguments), then any definition of an array of elements of that type will use the default constructors, unless you specify otherwise.
Ex. Fraction rationals[3] = {Fraction(), Fraction(2,3), Fraction(4)}; |
|
//----------------- CARDDECK.H -----------------
// These are the declarations of classes Card // and Deck, to be used by card-playing programs. enum Suit {clubs, diamonds, hearts, spades};
class Card // this class describes a single card { public: void Display(); // for displaying a card in a readable fashion int GetVal(); // retrieves the card's value void SetVal(int); // sets the card's value to the value provided Suit GetSuit(); // retrieves the card's suit void SetSuit(Suit); // sets the card's suit to the suit provided private: // we keep these private so that only authorized // users of the class can access them directly Suit s; // each card has a suit int val; // and a value (2 ... 14, representing 2 ... Q, K, A) }; class Deck // This class describes a collection of 52 cards { friend class Dealer; // since a Dealer has complete control over a deck public: Deck(); // the constructor opens a new deck of cards. private: int topCard; // points to position of current top card of deck Card cards[52]; // a deck is 52 cards. };
//----------------- CARDDECK.CPP ----------------- // This file contains the definitions for the // classes Card and Deck. #include <iostream.h> // for cout #include "CARDDECK.H" void Card::Display() // display the suit and value of an individual card { int v = GetVal(); // get the card's value if ((2 <= v) && (v <= 10)) // for number cards, show value cout << v; else // for face cards, use abbreviation { switch (v) { case 11: cout << 'J'; break; case 12: cout << 'Q'; break; case 13: cout << 'K'; break; case 14: cout << 'A'; break; } } switch (GetSuit()) // display suit { case clubs: cout << " of clubs"; break; case diamonds: cout << " of diamonds"; break; case hearts: cout << " of hearts"; break; case spades: cout << " of spades"; break; }; cout << '\n'; } int Card::GetVal() // return the numeric value of a card { return (val); } void Card::SetVal(int v) // set the numeric value of a card { val = v; } Suit Card::GetSuit() // return the suit value of a card { return (s); } void Card::SetSuit(Suit st) // set the suit of a card { s = st; } Deck::Deck() // constructor for initializing a new deck of 52 cards { topCard = 0; // we haven't dealt any cards yet for (int i = 0; i < 52; i++) // for each card in the deck: { cards[i].SetVal((i % 13) + 2); // assign it a numeric value (2 - 14) switch (i / 13) // and a suit. { case 0: cards[i].SetSuit(clubs); break; case 1: cards[i].SetSuit(diamonds); break; case 2: cards[i].SetSuit(hearts); break; case 3: cards[i].SetSuit(spades); break; } } }
Card c;
c.SetVal(4);
c.SetSuit(diamonds);
c.Display();
If a class contains member data that are themselves objects of other classes, and if those classes have default constructors, then those constructors are called at the start of the constructor for the class that contains them. |
To access elements in a compound object, always work from the top conceptual level down, following each component by its selector. |
You can use the dot operator to access a member of a class object. It makes no sense to access a member of a class type. |
Designing the PIP - need classes
Card, Deck, Player, & Dealer
Include testing as part of the programming process. As soon as you've finished writing a new class, test it before going on. |