2012年1月19日 星期四

Nested Classes


Nested Classes

This example illustrates nested classes. Note the use of the scope resolution operator in the member function definitions. This example is logically the same as example 3-7.

Example 3-10 - Nested Classes

1      // File: ex3-10.cpp - the card and deck example with nested classes
2     
3      #include <iostream>
4      #include <stdlib>
5      using namespace std;
6     
7      const char* value_name[13] =  {"two","three","four","five","six",
8         "seven","eight","nine","ten","jack","queen","king","ace"};
9      const char* suit_name[4] =
10         {"clubs","diamonds","hearts","spades"};
11     
12     
13      class deck
14      {
15        public:        // Why is this public?
16          class card
17          {
18            private:
19              int value;
20              int suit;
21            public:
22              void assign(int);
23              int get_value(void) const;
24              int get_suit(void) const;
25              void print(void) const;
26          };
27     
28        private:
29          card d[52];
30          int next_card;
31        public:
32          void create_deck(void);
33          void shuffle(void);
34          void deal(int=5);
35          void print(void) const;
36      };
37     
38     
39      int deck::card::get_value(void) const
40      {
41        return value;
42      }
43     
44      int deck::card::get_suit(void) const
45      {
46        return suit;
47      }
48     
49      void deck::card::assign(int x)
50      {
51        value = x % 13;
52        suit = x / 13;
53      }
54     
55      void deck::card::print(void) const
56      {
57        cout << (value_name[value]) << " of "
58             << (suit_name[suit]) << endl;
59        return;
60      }
61     
62      void deck::create_deck(void)
63      {
64        for (int i = 0; i < 52; i++) d[i].assign(i);
65        next_card = 0;
66      }
67     
68      void deck::shuffle(void)
69      {
70        int i, k;
71        card temp;
72        cout << "I am shuffling the deck\n";
73        for (i = 0; i < 52; i++)
74        {
75          k = rand() % 52;
76          temp = d[i];
77          d[i] = d[k];
78          d[k] = temp;
79        }
80      }
81     
82      void deck::print(void) const
83      {
84        cout << "\nHere's the deck:\n";
85        for (int i = 0; i < 52; i++) d[i].print();
86      }
87     
88      void deck::deal(int no_of_cards)
89      {
90        cout <<"\nOk, I will deal you "<<no_of_cards<<" cards:\n";
91        for (int i=0;i<no_of_cards; i++)  d[next_card++].print();
92        return;
93      }
94      int main (void)
95      {
96      //  card C;                // Why is this commented out?
97        deck::card C;            // Instatiate a card object
98        C.assign(17);
99        C.print();                // prints six of diamonds
100     
101        deck poker;
102        poker.create_deck();
103        poker.print();
104        poker.shuffle();
105        poker.print();
106        poker.deal();
107        poker.deal(3);
108        return 0;
109      }


******  Output same as Example 3-7 ******

Note: A nested class, like the card class of this example, is subject to access specifiers, just like data members or member functions. In other words, if the card class within the deck class of this example is specified as private, then the card type is not visible except to deck class member functions.

Nested class 的從屬關係就如同它 access specifiers 的定義.

沒有留言:

張貼留言