Arrays and Classes
Arrays of Objects
Declaring
Recall that an array is an indexed collection of data elements of the same type (where the indexing runs from 0 through size-1)
In addition to building arrays of built-in types, we can have arrays of objects.
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!)
In an array of objects, each array position is a single object.
For instance, given the above declaration of the "rationals" array, there are 20 Fraction objects, named rationals[0], rationals[1], ... , rationals[19]
Initialization
Normally the constructor initializes an object. But how to invoke the appropriate constructor for each object in an array?
The normal array declaration style uses the default constructor for each object in the array (if the class has a default constructor)
Fraction numList[4]; // builds 4 fractions using default constructor
To specify different constructors for different array items, an initializer set can be used. Since there are no literals for class types, use explicit constructor calls:
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
Using
Indexing works the same as with regular arrays
The dot-operator works the same as with single names:
objectName.memberName
Just remember that the name of such an object is now:
arrayName[index]
Examples:
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();
Arrays as Class Member Data
C-style arrays are pretty primitive, and they don't come with boundary checking. In some ways, they are unsafe, especially in novice hands
If an array is used as member data of a class, the member functions can add in error-checking and boundary protection.
This is a good technique for creating safer array types -- user defined classes that store large amounts of data
Here is the start of such a class, which stores an array of floating point numbers
This class stores a list of up to 10 values of type double
Note that the array is allocated to size 10, but the list can have up to 10 items in it -- the "list" is not always "full"!
This is managed with a tracking variable -- current. This is a member data variable that keeps track of how many elements are in the "list".
Note that the "list" and the "array" are not the same thing. The array is the physical storage used by the class. The "list" is the abstract concept that an object of this class type represents.
To represent an empty list, for example, set current to 0.
Also notice that when an array is member data of a class, it's already in scope for the member functions. So there will be less need to pass the array as a parameter.
This doesn't mean you'll never have array parameters, however. Just less frequently
Exercises: Try adding the following member functions to the class (for practice). Add in const wherever appropriate:
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.
Card Game Example
This example program is a Blackjack card game simulation
Features include the following
Multiple classes, using composition relationship
Card objects embedded in the Deck -- fits idea of "components"
Deck and Player objects member data of class Dealer -- shows use of a manager class
Arrays of objects as member data
Includes use of member data tracking variables, to keep information about array usage
enumeration usage (suits of the cards)
Notes:
Here is a portion of the declaration of the Deck class, which shows the setup of an array of objects, in a composition ("has-a") relationship:
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
Updated 195d ago