CS245 - Comp Sci II

Chapter 5 - Compound Data

OBJECTIVES


Describe the array compound type and give examples of how arrays may be used.
Discuss the natural correspondence between arrays and for loops.
Review how we declare, define, and use classes as compound data.
Illustrate how arrays and classes can be used together in this chapter's PIP to model a card game.

5.1 Arrays

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
  1. the definition begins with the specifier typedef and
  2. the type name synonym is used where the variable name would be.

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)};

  1. Arrays may be passed as arguments to functions.
  2. Arrays may not be returned from functions
  1. Assignments between arrays is not allowed.

5.2 Classes, Revisited

Declaring Classes

//----------------- 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. };

Defining Classes

//----------------- 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;
  		}
 	}
}

Using Classes

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.

5.3 Program in Progress: Blackjack

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.

Exercises