1 / 63

Lecture 10: Classes and Data Abstraction

Lecture 10: Classes and Data Abstraction. Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing Members of Structures 6.4 Implementing a User-Defined Type Time with a Struct 6.5 Implementing a Time Abstract Data Type with a Class 6.6 Class Scope and Accessing Class Members

Download Presentation

Lecture 10: Classes and Data Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: Classes andData Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing Members of Structures 6.4 Implementing a User-Defined Type Time with a Struct 6.5 Implementing a Time Abstract Data Type with a Class 6.6 Class Scope and Accessing Class Members 6.7 Separating Interface from Implementation 6.8 Controlling Access to Members 6.9 Access Functions and Utility Functions

  2. Introduction • Object-oriented programming (OOP) • Encapsulates data (attributes) and functions (behavior) into packages called classes • Information hiding • Implementation details are hidden within the classes themselves • Classes • Classes are the standard unit of programming • A class is like a blueprint – reusable • Objects are instantiated (created) from the class • For example, a house is an instance of a “blueprint class”

  3. Structure tag Structure members Structure Definitions • Structures • Aggregate data types built using elements of other types • Structures provide the ability to organize simple variables into more complex entities struct Time { int hour; int minute; int second; }; • Data items (variables) in a structure are called members • Members of the same structure must have unique names • Two different structures may contain members of the same name • Each structure definition must end with a semicolon • The members contained in the structure can be different types • Similar to “records” in other languages

  4. Structure Definitions and Declarations • Self-referential structure • Contains a member that is a pointer to the same structure type • Used for linked lists, queues, stacks and trees • struct • Creates a new data type that is used to declare variables • Structure variables are declared like variables of other types • Example: Time timeObject, timeArray[ 10 ], *timePtr, &timeRef = timeObject;

  5. Structure Variable Declarations • The syntax we saw for declaring a structure serves as a blueprint for creating variables of the new type • It doesn’t define a variable • It doesn’t allocate memory • It’s merely a type specification • Defining a Structure Variable structNamevar1; • Just like defining any other variable, the “type” is the name of the structure

  6. Simple Structure • Syntax: structstructName { data_type1 member1; data_type2 member2; data_type3 member3; } • Keyword – struct • Structure name or tag • Members • Semicolon following the brace • Example – parts.cpp

  7. Examples • Employee • Name • Salary • Product • Wholesale Cost • Suggested Retail • Name • Company • Departments • Employees • Customers • Products

  8. Accessing Members of Structures • Member access operators: • Dot operator (.) for structures and objects • Arrow operator (->) for pointers • Print member hour of timeObject: cout << timeObject.hour; OR timePtr = &timeObject; cout << timePtr->hour; • timePtr->hour is the same as ( *timePtr ).hour • Parentheses required: * has lower precedence than .

  9. 2. Create a struct data type 1 // Fig. 6.1: fig06_01.cpp 2 // Create a structure, set its members, and print it. 3 #include <iostream> Creates the user-defined structure type Time with three integer members: hour,minute and second. 4 5 using std::cout; 6 using std::endl; 7 8 struct Time { // structure definition 9 int hour; // 0-23 10 int minute; // 0-59 11 int second; // 0-59 12 }; 13 14 void printMilitary( const Time & ); // prototype 15 void printStandard( const Time & ); // prototype 16 17 int main() 18 { 19 Time dinnerTime; // variable of new type Time 20 21 // set members to valid values 22 dinnerTime.hour = 18; 23 dinnerTime.minute = 30; 24 dinnerTime.second = 0; 25 26 cout << "Dinner will be held at "; 27 printMilitary( dinnerTime ); 28 cout << " military time,\nwhich is "; 29 printStandard( dinnerTime ); 30 cout << " standard time.\n"; 31 Define the struct 1.1 Define prototypes for the functions 2. Create a struct data type 2.1 Set and print the time Dinner will be held at 18:30 military time, which is 6:30:00 PM standard time.

  10. 32 // set members to invalid values 33 dinnerTime.hour = 29; 34 dinnerTime.minute = 73; 35 36 cout << "\nTime with invalid values: "; 37 printMilitary( dinnerTime ); 38 cout << endl; 39 return 0; 40 } 41 42 // Print the time in military format 43 void printMilitary( const Time &t ) 44 { 45 cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":" 46 << ( t.minute < 10 ? "0" : "" ) << t.minute; 47 } 48 49 // Print the time in standard format 50 void printStandard( const Time &t ) 51 { 52 cout << ( ( t.hour == 0 || t.hour == 12 ) ? 53 12 : t.hour % 12 ) 54 << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute 55 << ":" << ( t.second < 10 ? "0" : "" ) << t.second 56 << ( t.hour < 12 ? " AM" : " PM" ); 57 } 2.2 Set the time to an invalid hour, then print it 3. Define the functions printMilitary and printStandard Time with invalid values: 29:73

  11. Dinner will be held at 18:30 military time, which is 6:30:00 PM standard time. Time with invalid values: 29:73 Program Output

  12. Other Structure Features • Combining Declaration and Definition • Add the variable name after the closing brace, but prior to the semicolon • Example – partscom.cpp • The tag name may be omitted if no further variables will be defined for this type • Initializing Structure Members Part1 = {6244, 373, 217} • Examples – partinit.cpp and englstrc.cpp • Strings as Data Members • Strings can be a data member for a struct • Example – strpart.cpp

  13. Arrays of Structures • The elements of arrays are not restricted to being simple data types, they can also be user-defined types such as Structures or Objects • Example – partaray.cpp • Defining an Array of Structures part apart[SIZE] • Accessing a data member of a structure in an array apart[n].modelnumber • Arrays of structures would be a way to collect related data that may be different types • Personnel data • Geographical data • Deck of Cards

  14. Array of Structures: Deck of Cards // array of cards #include <iostream> #include <cstdlib> #include <ctime> using namespace std; enum Suit { clubs, diamonds, hearts, spades }; //from 2 to 10 are integers without names const int jack = 11; const int queen = 12; const int king = 13; const int ace = 14; const int CARDS_IN_DECK = 52; const int CARDS_IN_SUIT = 13; struct card { Suit suit; int num; };

  15. Array of Structures: Deck of Cards int main() { int i, j; card deck[CARDS_IN_DECK]; for(i=0; i<CARDS_IN_DECK; i++) // make an ordered deck { deck[i].suit = Suit(i / CARDS_IN_SUIT); deck[i].num = (i % CARDS_IN_SUIT) + 2; } srand( time(NULL) ); // shuffle the deck for(i=0; i<CARDS_IN_DECK; i++) { j = rand() % CARDS_IN_DECK; card temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; }

  16. Array of Structures: Deck of Cards for(i=0; i<CARDS_IN_DECK; i++) // display the deck { if( (i % CARDS_IN_SUIT) == 0 ) cout << endl; if(deck[i].num >= 2 && deck[i].num <= 10) cout << deck[i].num; else switch(deck[i].num) { case jack: cout << 'J'; break; case queen: cout << 'Q'; break; case king: cout << 'K'; break; case ace: cout << 'A'; break; } switch(deck[i].suit) { case clubs: cout << static_cast<char>(5); break; case diamonds: cout << static_cast<char>(4); break; case hearts: cout << static_cast<char>(3); break; case spades: cout << static_cast<char>(6); break; } } return 0; }

  17. Structures as Function Arguments • Examples – engldisp.cpp and circstrc.cpp • Structures are treated the same as any other type • The structure must be defined before it can be used as a type in a function declaration • Passing structures as parameters is the same as passing a variable of any other type • Modularize our deck of cards with functions…

  18. Structures as Function Arguments // Enhance prior solution with functions for displaying a card, shuffling // a deck, and swapping two cards #include <iostream> #include <cstdlib> #include <ctime> using namespace std; enum Suit { clubs, diamonds, hearts, spades }; //from 2 to 10 are integers without names const int jack = 11; const int queen = 12; const int king = 13; const int ace = 14; const int CARDS_IN_DECK = 52; const int CARDS_IN_SUIT = 13; struct card { Suit suit; int num; }; void display( card* ); void swap( card&, card& ); void shuffle( card [] );

  19. Structures as Function Arguments void display ( card *aCard ) { if(aCard->num >= 2 && aCard->num <= 10) cout << aCard->num; else switch(aCard->num) { case jack: cout << 'J'; break; case queen: cout << 'Q'; break; case king: cout << 'K'; break; case ace: cout << 'A'; break; } switch(aCard->suit) { case clubs: cout << static_cast<char>(5); break; case diamonds: cout << static_cast<char>(4); break; case hearts: cout << static_cast<char>(3); break; case spades: cout << static_cast<char>(6); break; } } void swap( card &c1, card &c2 ) { card temp = c1; c1 = c2; c2 = temp; }

  20. Structures as Function Arguments void shuffle( card deck[] ) { int i, j; srand( time(NULL) ); // shuffle the deck for(i=0; i<CARDS_IN_DECK; i++) { j = rand() % CARDS_IN_DECK; swap(deck[i], deck[j]); } }

  21. Structures as Function Arguments int main() { int i; card deck[CARDS_IN_DECK]; for(i=0; i<CARDS_IN_DECK; i++) // make an ordered deck { deck[i].suit = Suit(i / CARDS_IN_SUIT); deck[i].num = (i % CARDS_IN_SUIT) + 2; } shuffle( deck ); for(i=0; i<CARDS_IN_DECK; i++) // display the deck { if( (i % CARDS_IN_SUIT) == 0 ) cout << endl; display( &deck[i] ); } return 0; }

  22. Structures Within Structures • You can create a structure which has a member who’s data type is itself another structure – aka “nested” structures • Example – englarea.cpp • Access the nested structure members with additional levels of dot operators • Initializing Nested Structures Room dining = { {13, 6.5} {10, 0.0} } • Let’s create a deck of cards…

  23. Structures Within Structures – A Deck of Cards // Enhance prior solution by adding a struct for the deck #include <iostream> #include <cstdlib> #include <ctime> using namespace std; enum Suit { clubs, diamonds, hearts, spades }; //from 2 to 10 are integers without names const int jack = 11; const int queen = 12; const int king = 13; const int ace = 14; const int CARDS_IN_DECK = 52; const int CARDS_IN_SUIT = 13; struct card { Suit suit; int num; }; struct deck { // new struct! card cards[CARDS_IN_DECK]; int topCard; };

  24. Structures Within Structures – A Deck of Cards void display( card* ); // doesn’t change from prior example void swap( card&, card& ); // doesn’t change from prior example void shuffle( deck& ); // changed void init( deck& ); // added, initialize the deck void display( deck ); // added, display the entire deck void shuffle( deck &aDeck ) { int i, j; srand( time(NULL) ); // shuffle the deck for(i=0; i<CARDS_IN_DECK; i++) { j = rand() % CARDS_IN_DECK; swap(aDeck.cards[i], aDeck.cards[j]); //notice the change here } aDeck.topCard = 0; } void init( deck &aDeck ) { aDeck.topCard = 0; for(int i=0; i<CARDS_IN_DECK; i++) // make an ordered deck { aDeck.cards[i].suit = Suit(i / CARDS_IN_SUIT); aDeck.cards[i].num = (i % CARDS_IN_SUIT) + 2; } }

  25. Structures Within Structures – A Deck of Cards void display( deck aDeck ) { for(int i=0; i<CARDS_IN_DECK; i++) // display the deck { if( (i % CARDS_IN_SUIT) == 0 ) cout << endl; display( &aDeck.cards[i] ); } } int main() // look at how main() has shrunk { int i; deck aDeck; init( aDeck ); shuffle( aDeck ); display( aDeck ); return 0; }

  26. Returning Structures from Functions • Structures can be return values, just the same as they can be passed as arguments • Example – retstrc.cpp • Notice that you cannot simply add the two structures together, you must add the members individually and store the result in another variable • Let’s draw a card from our deck…

  27. Returning Structures from Functions – Let’s draw a card from our deck // add one more function card* drawACard( deck aDeck ) { return &(aDeck.cards[aDeck.topCard++]); } int main() // look at how main() has shrunk { deck aDeck; init( aDeck ); shuffle( aDeck ); cout << "The top card is "; display( drawACard( aDeck ) ); cout << "The next card is "; display( drawACard( aDeck ) ); return 0; }

  28. Just Do It! I’m sorry if you’re afraid of decks of cards… Let’s add one more extension to the deck of cards example. Start with the last version (located in carddeck.cpp) and add a playerstruct. This struct should have two data members – one to hold their hand (13 cards) and another that keeps track of how many cards the player holds. Add an array of 4 players to main(). After you initialize and shuffle the deck, deal the cards to each player by going around giving the players one card at a time from the deck until all cards are dealt. You will likely want to use the drawACard() function in your solution. It would also be helpful if you write another function that adds a card to a player’s hand.

  29. Sample Solution // additions to the prior solution const int CARDS_IN_HAND = 13; const int NUM_OF_PLAYERS = 4; struct player { card hand[CARDS_IN_HAND]; int cardsInHand; }; void addCardToHand( card* aCard, player &aPlayer ) { aPlayer.hand[aPlayer.cardsInHand].num = aCard->num; aPlayer.hand[aPlayer.cardsInHand].suit = aCard->suit; ++(aPlayer.cardsInHand); }

  30. Sample Solution int main() // look at how main() has shrunk { // this replaces other main() int i, j; deck aDeck; player players[NUM_OF_PLAYERS]; init( aDeck ); for(i=0; i<NUM_OF_PLAYERS; i++) // init the players to hold no cards players[i].cardsInHand = 0; shuffle( aDeck ); for(i=0; i<CARDS_IN_DECK; i++) // deal the cards addCardToHand(drawACard(aDeck), players[i%4]); for(i=0; i<NUM_OF_PLAYERS; i++) // display player hands { cout << "Player " << i + 1 << "'s hand is: "; for(j=0; j<CARDS_IN_HAND; j++) { display(players[i].hand[j]); cout << endl; } cout << endl; } return 0; }

  31. Introduction to Objects • Object-oriented programming (OOP) • Encapsulates data (attributes) and functions (behavior) into packages called classes • Information hiding • Implementation details are hidden within the classes themselves • Classes • Classes are the standard unit of programming • A class is like a blueprint – reusable • Objects are instantiated (created) from the class • For example, a house is an instance of a “blueprint class” • The Cookie Cutter • Combine the elements of a structure for grouping data elements and functions for organizing program actions

  32. Object Modeling • As Physical Objects • The Class • Noun • Represents an abstract or concrete person, place, or thing in the real world • Data Members • Adjectives • Represent attributes/characteristics of the object • Member Functions • Verbs • Model the activities an object can participate in • Examples – objpart.cpp, circles.cpp • As Data Types • More complicated data types can be modeled with Classes – remember the linked list? • Data and Behavior can be bundled together in a single package • Example – englobj.cpp

  33. 1 class Time { 2 public: 3 Time(); 4 void setTime( int, int, int ); 5 void printMilitary(); 6 void printStandard(); 7 private: 8 int hour; // 0 - 23 9 int minute; // 0 - 59 10 int second; // 0 - 59 11 }; Classes • Classes • Model objects that have attributes (data members) and behaviors (member functions) • Defined using keyword class • Have a body delineated with braces ({ and }) • Class definitions terminate with a semicolon • Example: Public: and Private: are member-access specifiers. setTime, printMilitary, and printStandard are member functions.Time is the constructor. hour, minute, and second are data members.

  34. Declaring a Class • Syntax classclass_name { private: // declare the data members data_type1 data_member1; data_type2 data_member2; public: return_type func1_name( param_list ) { statements; } return_type func2_name( param_list ) { statements; }; return_type func3_name( param_list ) { statements; }; }; • Example smallobj.cpp

  35. Elements of a Class Declaration • Keywords • class - The class declaration begins with this keyword • private: - Used to denote the data members and member functions ONLY accessible inside the class itself • public: - Identifies data and functions accessible OUTSIDE (and inside) the Class • Typically – Data is private, Functions are public • Class Data refers to the individual data attributes contained in a class • Member Functions are the actions you can perform on (or with) instances of a Class

  36. Implementing a Time Abstract Data Type with a Class • Member access specifiers • Classes can limit the access to their member functions and data • The three types of access a class can grant are: • Public— Accessible wherever the program has access to an object of the class • private— Accessible only to member functions of the class • Protected— Similar to private and discussed later • Constructor • Special member function that initializes the data members of a class object • Cannot return values • Have the same name as the class

  37. Implementing a Time Abstract Data Type with a Class • Class definition and declaration • Once a class has been defined, it can be used as a type in object, array and pointer declarations • Example: • The variable declaration actually creates objects that the program can use, the class definition does not Time sunset, // object of type Time arrayOfTimes[ 5 ], // array of Time objects *pointerToTime, // pointer to a Time object &dinnerTime = sunset; // reference to a Time object Note: The class name becomes the new type specifier.

  38. 1 // Fig. 6.3: fig06_03.cpp 2 // Time class. Note the :: preceding the function names. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // Time abstract data type (ADT) definition 9 class Time { 10 public: 11 Time(); // constructor 12 void setTime( int, int, int ); // set hour, minute, second 13 void printMilitary(); // print military time format 14 void printStandard(); // print standard time format 15 private: 16 int hour; // 0 – 23 17 int minute; // 0 – 59 18 int second; // 0 – 59 19 }; 20 21 // Time constructor initializes each data member to zero. 22 // Ensures all Time objects start in a consistent state. 23 Time::Time() { hour = minute = second = 0; } 24 25 // Set a new Time value using military time. Perform validity 26 // checks on the data values. Set invalid values to zero. 27 void Time::setTime( int h, int m, int s ) 28 { 29 hour = ( h >= 0 && h < 24 ) ? h : 0; 30 minute = ( m >= 0 && m < 60 ) ? m : 0; 31 second = ( s >= 0 && s < 60 ) ? s : 0; 32 } 1. Define a Time class 1.1 Define default values for the time

  39. 33 1.2 Define the two functions printMilitary and printstandard2. In main, create an object of class Time2.1Print the initial (default) time 34 // Print Time in military format 35 void Time::printMilitary() 36 { 37 cout << ( hour < 10 ? "0" : "" ) << hour << ":" 38 << ( minute < 10 ? "0" : "" ) << minute; 39 } 40 41 // Print Time in standard format Notice how functions are called using the dot (.) operator. 42 void Time::printStandard() 43 { 44 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 45 << ":" << ( minute < 10 ? "0" : "" ) << minute 46 << ":" << ( second < 10 ? "0" : "" ) << second 47 << ( hour < 12 ? " AM" : " PM" ); 48 } 49 50 // Driver to test simple class Time 51 int main() 52 { 53 Time t; // instantiate object t of class Time 54 55 cout << "The initial military time is "; 56 t.printMilitary(); 57 cout << "\nThe initial standard time is "; 58 t.printStandard(); 59 The initial military time is 00:00 The initial standard time is 12:00:00 AM

  40. 60 t.setTime( 13, 27, 6 ); 2.2 Set and print the time2.3 Set the time to an invalid hour2.4 Print the timeProgram Output 61 cout << "\n\nMilitary time after setTime is "; 62 t.printMilitary(); 63 cout << "\nStandard time after setTime is "; 64 t.printStandard(); 65 66 t.setTime( 99, 99, 99 ); // attempt invalid settings 67 cout << "\n\nAfter attempting invalid settings:" 68 << "\nMilitary time: "; 69 t.printMilitary(); 70 cout << "\nStandard time: "; 71 t.printStandard(); 72 cout << endl; 73 return 0; 74 } Military time after setTime is 13:27 Standard time after setTime is 1:27:06 PM After attempting invalid settings: Military time: 00:00 Standard time: 12:00:00 AM The initial military time is 00:00 The initial standard time is 12:00:00 AM Military time after setTime is 13:27 Standard time after setTime is 1:27:06 PM After attempting invalid settings: Military time: 00:00 Standard time: 12:00:00 AM

  41. Implementing a Time Abstract Data Type with a Class • Binary scope resolution operator (::) • Member Functions can be declared inside a function but defined outside • The scope resolution operator allows you to specify what Class to associate to • Combines the class name with the member function name • Different classes can have member functions with the same name • Format for defining member functions • Declaration ReturnType MemberFunctionName( params ); • Definition ReturnType ClassName::MemberFunctionName( params ){ … }

  42. Implementing a Time Abstract Data Type with a Class • If a member function is defined inside the class • Scope resolution operator and class name are not needed • Defining a function outside a class does not change it being public or private

  43. Pointers to Objects • Pointers can point to objects as well as to simple data types and arrays • Syntax Classname* ptrToObj; ptrToObj = new ClassName; • Example – englptr.cpp • Referring to the object’s members (*distptr).getdist(); distptr->getdist(); • Array of Pointers Example – ptrobjs.cpp • Linked List Example – linklist.cpp

  44. 1. Class definition2. Create an object of the class2.1 Assign a value to the object. Print the value using the dot operator2.2 Set a new value and print it using a reference 1 // Fig. 6.4: fig06_04.cpp 2 // Demonstrating the class member access operators . and -> It is rare to have public member variables. Usually only member functions are public; this keeps as much information hidden as possible. 3 // 4 // CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! 5 #include <iostream> 6 7 using std::cout; 8 using std::endl; 9 10 // Simple class Count 11 class Count { 12 public: 13 int x; 14 void print() { cout << x << endl; } 15 }; 16 17 int main() 18 { 19 Count counter, // create counter object 20 *counterPtr = &counter, // pointer to counter 21 &counterRef = counter; // reference to counter 22 23 cout << "Assign 7 to x and print using the object's name: "; 24 counter.x = 7; // assign 7 to data member x 25 counter.print(); // call member function print 26 27 cout << "Assign 8 to x and print using a reference: "; 28 counterRef.x = 8; // assign 8 to data member x 29 counterRef.print(); // call member function print 30 1. Class definition2. Create an object of the class2.1 Assign a value to the object. Print the value using the dot operator2.2 Set a new value and print it using a reference

  45. 31 cout << "Assign 10 to x and print using a pointer: "; 32 counterPtr->x = 10; // assign 10 to data member x 33 counterPtr->print(); // call member function print 34 return 0; 35 } 2.3 Set a new value and print it using a pointerProgram Output Assign 7 to x and print using the object's name: 7 Assign 8 to x and print using a reference: 8 Assign 10 to x and print using a pointer: 10

  46. Just Do It! Oh, No… Oh, Yes!!! Convert your prior Just Do It! Deck of Cards solution to Classes and Objects. There is a copy of the first “Just Do It!” exercise in playerhands.cpp if you’d like to start fresh. You had to know this was coming…

  47. Sample Solution //All the includes and constants from before class card { private: Suit suit; int num; public: void init(int, Suit); void display(); int getNumber(); Suit getSuit(); }; class deck { private: card cards[CARDS_IN_DECK]; int topCard; void swap( int, int ); // swap two cards public: void shuffle( ); // randomly shuffle the deck void init( ); // initialize the deck void display( ); // display the entire deck card* drawACard( ); // draw the top card from the deck };

  48. Sample Solution (cont.) class player { private: card hand[CARDS_IN_HAND]; int cardsInHand; public: void addCardToHand( card* aCard ); void init(); void displayHand(int); }; // Definition of card member functions void card::init(int n, Suit s) { num = n; suit = s; }

  49. Sample Solution (cont.) void card::display() { if(num >= 2 && num <= 10) cout << num; else switch(num) { case jack: cout << 'J'; break; case queen: cout << 'Q'; break; case king: cout << 'K'; break; case ace: cout << 'A'; break; } switch(suit) { case clubs: cout << static_cast<char>(5); break; case diamonds: cout << static_cast<char>(4); break; case hearts: cout << static_cast<char>(3); break; case spades: cout << static_cast<char>(6); break; } }

  50. Sample Solution (cont.) int card::getNumber() { return num; } Suit card::getSuit() { return suit; } // Definition of deck member functions void deck::swap( int c1, int c2 ) { card temp = cards[c1]; cards[c1] = cards[c2]; cards[c2] = temp; } void deck::shuffle() { int i, j; srand( time(NULL) ); // shuffle the deck for(i=0; i<CARDS_IN_DECK; i++) { j = rand() % CARDS_IN_DECK; swap(i, j); } topCard = 0; }

More Related