[ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ]

4. More on Objects

4.1  Introduction

Alan Kay, considered as the father of object-oriented programming, identified the following characteristics as fundamental to OOP[Kay 1993]:

Let us look at some simple examples.

4.2  Cards Example

This program creates a deck of cards, shuffles them and displays the ordered deck as well as the shuffled deck. Every card object is associated with a corresponding numerical value and a suit type. Executing this program produces the following output.

Ordered Deck
 
2  Clubs           3  Clubs        4  Clubs        5  Clubs        6  Clubs
7  Clubs           8  Clubs        9  Clubs        10 Clubs        J  Clubs
Q  Clubs           K  Clubs        A  Clubs        2  Diamonds     3  Diamonds
4  Diamonds        5  Diamonds     6  Diamonds     7  Diamonds     8  Diamonds
9  Diamonds        10 Diamonds     J  Diamonds     Q  Diamonds     K  Diamonds
A  Diamonds        2  Hearts       3  Hearts       4  Hearts       5  Hearts
6  Hearts          7  Hearts       8  Hearts       9  Hearts       10 Hearts
J  Hearts          Q  Hearts       K  Hearts       A  Hearts       2  Spades
3  Spades          4  Spades       5  Spades       6  Spades       7  Spades
8  Spades          9  Spades       10 Spades       J  Spades       Q  Spades
K  Spades          A  Spades

Shuffled Deck
 
9  Diamonds     2  Clubs        9  Hearts       6  Spades       8  Clubs 
3  Diamonds     2  Hearts       J  Diamonds     3  Spades       7  Diamonds 
8  Spades       3  Hearts       Q  Hearts       J  Spades       A  Clubs 
Q  Diamonds     4  Clubs        4  Diamonds     4  Hearts       10 Hearts
J  Clubs        3  Clubs        J  Hearts       8  Diamonds     7  Hearts 
K  Spades       7  Clubs        4  Spades       10 Diamonds     2  Diamonds
Q  Spades       6  Hearts       A  Diamonds     8  Hearts       10 Spades 
5  Clubs        7  Spades       9  Spades       K  Diamonds     6  Diamonds 
2  Spades       10 Clubs        A  Hearts       6  Clubs        A  Spades 
5  Diamonds     K  Clubs        9  Clubs        5  Spades       Q  Clubs 
5  Hearts       K  Hearts

The class definition of the cards is as follows.
 
enum SuitName{clubs, diamonds, hearts, spades};
const int jack  = 11;
const int queen = 12;
const int king  = 13;
const int ace   = 14;

class card {private:
   int      rank;
   SuitName suit;
public:
   void init(int n, SuitName s) {suit = s; rank = n;}
   void display();
};

void card::display() {
   if (rank >= 2 && rank <= 9)  cout << rank << "  ";
   else if (rank == 10)         cout << rank << " ";
   else switch (rank) {
      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 << "Clubs";    break;
      case diamonds: cout << "Diamonds"; break;
      case hearts:   cout << "Hearts";   break;
      case spades:   cout << "Spades";   break;
   }
}
 

The main program using this class is as follows
 
#include<iostream.h>
#include<stdlib.h>

// This program prints the deck, shuffles it, and prints the result.
main() {
   card deck[52];
   int  k, j, num;
   SuitName suit;

   for (j = 0; j < 52; j++) {
      num = (j%13) + 2;
      suit = SuitName(j/13);
      deck[j].init(num, suit);
   }
   cout << "Ordered Deck" << endl;
   for (j = 0; j < 52; j++) {
      deck[j].display();
      if( j != 51 )       cout << "\t";
      if( (j+1) % 5 == 0) cout << endl;
   }
   for(j = 0; j < 52; j++) {
      k = rand() % 52;
      card temp=deck[j];
      deck[j] = deck[k];
      deck[k] = temp;
   }
   cout << endl << endl << "Shuffled Deck" << endl;
   for (j = 0; j < 52; j++) {
      deck[j].display();
      if(j != 51)      cout << "\t";
      if((j+1)%6 == 0) cout << endl;
   }
   cout << endl;
}

4.3  Washing Machine Example

This example is suggestive of a controller for the cycles of a washing machine. Executing this program produces the following output.
 
current_cycle = 1
current_cycle = 2
current_cycle = 3
current_cycle = 4
current_cycle = 5
current_cycle = 6
current_cycle = 0

The program is as follows

#include <iostream.h>
const int idle = 0;
const int pre_wash = 1;
const int spin1 = 2;
const int wash = 3;
const int spin2 = 4;
const int rinse = 5;
const int spin3 = 6;

class WashingMachine {private:
   int current_cycle;
public:
   void start() {current_cycle = idle;}
   void next();
};
void WashingMachine::next() {
   switch(current_cycle) {
      case idle:     current_cycle = pre_wash; break;
      case pre_wash: current_cycle = spin1;    break;
      case spin1:    current_cycle = wash;     break;
      case wash:     current_cycle = spin2;    break;
      case spin2:    current_cycle = rinse;    break;
      case rinse:    current_cycle = spin3;    break;
      case spin3:    current_cycle = idle;     break;
      default:       current_cycle = idle;
   }
   cout << "current_cycle = " << current_cycle << endl;
 }

main() {
   int i;
   WashingMachine WM;
   WM.start();
   for (i = 0; i < 7; i++) WM.next();
 }
 

4.4  Word Scrambler Example

We now turn to a richer example of a class. This class takes a string as input and then scrambles it. This allows one to create a game that scrambles a word at random, and then asks the user to unscramble the word. The following is an example dialogue of this word scrambler.

Welcome to the Jumble Game
You have maximum 5 attempts to unscramble the given word.
Guess the unjumbled word of: sclas
Enter your guess for the unjumbled word: class

Congratulation!  You got it right!!!
Would you like to play again? (y/n) y
Welcome to the Jumble Game
You have Maximum 5 attempts to unscramble the given word.
Guess the unjumbled word of: ieacrtnhein
Enter your guess for the unjumbled word: inheritance

Congratulation!  You got it right!!!
Would you like to play again? (y/n) n
Thank you for playing WordScrambler game.

The following is the class definition for the word scrambler producing the above output.
 
class WordScrambler {private:
   char* sourceWord;
   char* jumbledWord;
   char* words[10];        //list of words to be jumbled
   int getRandomInt(int );
   void scrambleWord(char*);
public:
   WordScrambler();
   void playGame();
};

The member functions are defined as follows:

WordScrambler::WordScrambler(){
   words[0] = "instance";
   words[1] = "destructor";
   words[2] = "inheritance";
   words[3] = "polymorphism";
   words[4] = "encapsulation";
   words[5] = "persistence";
   words[6] = "overloading";
   words[7] = "object";
   words[8] = "class";
   words[9] = "constructor";
}

int WordScrambler::getRandomInt(int length){
   int j = rand()%length;
   return j;
}

void WordScrambler::scrambleWord(char* str){
   int length, count, randomInt ;
   int* selectedIndexes;     //stores all generated random integers.
   int* NumSelections;       //marks occurence of a given random integer

   length = strlen(str);
   NumSelections = new int[length];
   selectedIndexes = new int[length];
   for(count = 0;count < length;++count)
      NumSelections[count] = 0;
   for(count = 0;count < length;++count){
      randomInt = getRandomInt(length);
      if(NumSelections[randomInt] == 0){
         selectedIndexes[count] = randomInt;
         ++NumSelections[randomInt];
      }
      else
        --count;
  }
   for (count = 0;count < length; ++count){
      jumbledWord[count] = str[selectedIndexes[count]];
   }
   jumbledWord[count] = '\0';  //appends NULL to jumbled word
 }

void WordScrambler::playGame() {
   int index, count, length;
   char* guessString;

   index  = rand()%10;
   length = strlen(words[index]);
   sourceWord  = new char[length];
   jumbledWord = new char[length];
   guess = new char[length];
   strcpy (sourceWord, words[index]);
   scrambleWord (sourceWord);
   cout << "Guess the unjumbled word of: ";
   cout << jumbledWord << endl;
   count = 2;
   while (count < 6) {
      cout << "Enter your guess for the unjumbled word: ";
      cin  >>  guess;
      if (strcmp(guess, sourceWord) == 0){
         cout << endl << "Congratulation!  You got it right!!!" << endl;
         return;
      }
      if (count <= 5){
         cout << "  Attempt   " <<  count;
         cout << "   out of 5 attempts was unsuccessful " << endl;
      }
      if (count == 4)   cout << "Your Last Chance";
      count++;
   }
   cout << "Correct Word is: " << sourceWord << endl;
}
 

The following is an example of a main procedure that utilizes this class.
 
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

main() {
   WordScrambler WS;
   char response = 'y';
   while (response == 'y') {
      cout << "Welcome to the Jumble Game" << endl ;
      cout << "You have maximum 5 attempts to unscramble the given ";
           << "word." << endl;
           WS.playGame();
      cout << "Would you like to play again? (y/n) ";
      cin  >> response;
      if(response != 'y')
         cout << "Thank you for playing WordScrambler game." << endl;
   };
}
 

References

1. [Budd, 1997] Timothy Budd. An Introduction to Object-Oriented Programming, Second Edition. Addison-Wesley, 1997.
2. [Kay, 1993] Alan C. Kay. "The Early History of Smalltalk," The Second ACM SIGPLAN History of Programming Languages Conference (HOPL-II), ACM SIGPLAN Notice 28(3): 69-75, March 1993.
 

[ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ]