Fraction rationals[20]; // array of 20 Fraction objects Complex nums[50]; // an array of 50 Complex objects Hydrant fireplugs[10]; // an array of 10 objects of type Hydrant (a dog's dream!)
Fraction numList[4]; // builds 4 fractions using default constructor
Fraction numList[3] = { Fraction(2,4) , Fraction(5) , Fraction() }; // this allocates an array of 3 fractions, initialized to 2/4, 5/1, and 0/1
objectName.memberNameJust remember that the name of such an object is now:
arrayName[index]
Fraction rationals[20]; // create an array of 20 Fraction objects ... rationals[2].Show(); // displays the third Fraction object rationals[6].Input(); // calls Input function for the 7th Fraction cout << rationals[18].Evaluate();
bool Delete(int n); // delete the nth element of the list // return true for success, false if n not a valid position double Sum(); // return the sum of the elements of the list double Average(); // return the average of the elements of the list double Max(); // return the maximum value in the list void Clear(); // reset the list to empty int Greater(double x); // count how many values in the list are greater // than x. Return the count.
class Deck { public: .... // member functions private: int topCard; // points to position of current top card of deck Card cards[52]; // a deck is 52 cards. };
Note the use of the topCard variable. While not data that the class user specifically sees or is interested in, it helps iterate through the array for dealing.
Here is a portion of the Player class:
class Player { public: .... // member functions private: Card hand[5]; int numCards; // the number of cards currently in the hand int HandValue(); // calculates the numeric value of the hand void ShowHand(); // displays a player's hand and value };
Note the numCards tracking variable. In this case, the array of Cards (called hand) can store up to 5 cards. Sometimes it's not full. numCards keeps track of how much of the allocated array is in use.
Example of numCards being used in a function:
for (int i = 0; i < numCards; i++) hand[i].Display(); // only displays the filled card slots