# Lec17 - Lecture 17 Vectors PIC 10A Todd Wittman Drawing...

This preview shows pages 1–4. Sign up to view the full content.

1 Lecture 17: Vectors PIC 10A Todd Wittman Drawing Cards r When we talked about random numbers, we mentioned using a switch statement to pick a random card suit. string suit; int suit_number = 1+rand()%4; switch(suit_number) { case 1: suit = "Clubs"; break; case 2: suit = "Diamonds"; break; case 3: suit = "Hearts"; break; case 4: suit = "Spades"; break; } r I’ll let you figure out how to pick a random rank. r Can we use this to make a Card class?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 The Card Class r We could construct a Card class that stores a playing card. class Card { public: Card( ); .... private: string suit; string rank; }; r It would be cool if a call to our constructor ( Card card1; ) drew a random card. What other member functions should we have? The Card Class r It would be nice if the default constructor for a Card class generated a random card. Card::Card( ) { string suit; int my_rand = 1+rand()%4; switch(my_rand) { case 1: suit = "Clubs"; break; case 2: suit = "Diamonds"; break; case 3: suit = "Hearts"; break; case 4: suit = "Spades"; break; } .... We already have suit as a private variable. Don’t re-declare it!
3 Simulate Drawing Cards r In next week’s HW, you can assume the cards are drawn from more than one deck. r So it’s OK to get card1 = “Queen of Hearts” card2 = “Queen of Hearts” r To simulate drawing 2 cards from a single deck, we could “redraw” if we get the same card. Card card1; Card card2; while (card1 == card2) Card card2; r But this assumes == is defined for cards. r How would you redraw using just the < operator? Storing Data in Lists r Suppose we want to keep track of a list 5 numbers. 11 -12 42 38 -105 r We could create 5 int variables. int value1, value2, value3, value4, value5;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

Lec17 - Lecture 17 Vectors PIC 10A Todd Wittman Drawing...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online