Lec17 - Lecture 17 Vectors PIC 10A Todd Wittman Drawing Cards When we talked about random numbers we mentioned using a switch statement to pick a

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

View Full Document Right Arrow Icon
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?
Background image of page 1

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

View Full DocumentRight Arrow Icon
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!
Background image of page 2
3 Simulate Drawing Cards r In next week’s HW, you can assume the cards are drawn from more than one deck. r
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-050-21 taught by Professor Wittman during the Fall '10 term at UCLA.

Page1 / 7

Lec17 - Lecture 17 Vectors PIC 10A Todd Wittman Drawing Cards When we talked about random numbers we mentioned using a switch statement to pick a

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online