The best of both worlds – HP Integrity NonStop J-Series User Manual

Page 131

Advertising
background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

The Best of Both Worlds

The following example is a complete program that creates a deck of cards and shuffles it. The
purpose of the example is to show how the Tools.h++ template collections can be used in
conjunction with the Standard C++ Library. See your Standard C++ Library documentation for
more information on the features used in the example.

/* Note: This example requires the C++ Standard Library */

#include <iostream.h>
#include <algorithm>
#include <rw/tvordvec.h>

struct Card {
char rank;
char suit;

bool operator==(const Card& c) const
{ return rank == c.rank && suit == c.suit; }

Card() { }
Card(char r, char s) : rank(r), suit(s) { }

// print card: e.g. '3-C' = three of clubs, 'A-S' = ace of spades
friend ostream& operator<<(ostream& ostr, const Card& c)
{ return (ostr << c.rank << "-" << c.suit << " "); }
};

/*
* A generator class - return Cards in sequence
*/
class DeckGen {
int rankIdx; // indexes into static arrays below
int suitIdx;
static const char Ranks[13];
static const char Suits[4];
public:
DeckGen() : rankIdx(-1), suitIdx(-1) { }

Advertising
This manual is related to the following products: