2012年1月19日 星期四

Multi-File C++ Programs


Multi-File C++ Programs 

It is common practice in larger C++ programs to separate the application into multiple files. The files typically consist of one or more header files, function definition files, and a file to hold main(). The header files generally contain class definitions, constants, enumerated types, structures, typedefs, and inline function definitions. Header files have the same name as the class or a name related to the application and have the extension .h (.hpp on some compilers). Class member function definitions are contained in another file(s). The filename is often the same name as the class name and extension .cpp (or whatever the requirement for your compiler). This file(s) should "include" the header file(s) and may be compiled separately. The file containing main() will usually have the name of the application and the usual C++ extension. main() will need to include the header file(s) and after compilation will link to the compiled class member definition file(s). This process is managed for you in many of the PC and Mac compilers with projects.

This example is logically the same as Example 3-7.

Example 3-11 - A Multi-file program

1      // File: ex3_11c.h - card class definition
2     
3      #ifndef EX3_11C_H
4      #define EX3_11C_H
5     
6      class card
7      {
8        private:
9          int value;
10          int suit;
11        public:
12          void assign(int);
13          int get_value(void) const;
14          int get_suit(void) const;
15          void print(void) const;
16      };
17     
18      #endif

Note: some compilers do not support header files with a hyphen in the file name.

1      // File: ex3_11d.h - deck class definition
2      #ifndef EX3_11D_H
3      #define EX3_11D_H
4      #include "ex3_11c.h"
5     
6      class deck
7      {
8        private:
9            card d[52];
10            int next_card;
11        public:
12            void create_deck(void);
13            void shuffle(void);
14            void deal(int=5);
15            void print(void) const;
16      };
17      #endif


1      // File:  ex3-11c.cpp  card class member function definitions
2     
3      #include <iostream>
4      using namespace std;
5     
6      #include "ex3_11c.h"
7     
8      const char* value_name[13] =
9       {"two","three","four","five","six",
10        "seven","eight","nine","ten","jack","queen","king","ace"};
11      const char* suit_name[4] =
12       {"clubs","diamonds","hearts","spades"};
13     
14      card::get_value(void) const {
15        return value;
16      }
17     
18      int card::get_suit(void) const {
19        return suit;
20      }
21     
22      void card::assign(int x) {
23        value = x % 13;
24        suit = x / 13;
25        return;
26      }
27     
28      void card::print(void) const {
29        cout << (value_name[value]) << " of "
30            << (suit_name[suit]) << endl;
31        return;
32      }


1      // File: ex3-11d.cpp - deck class member function definitions
2     
3      #include <iostream>
4      #include <cstdlib>            // needed for rand() function
5      using namespace std;
6     
7      #include "ex3_11d.h"
8     
9      void deck::create_deck(void) {
10        for (int i = 0; i < 52; i++) d[i].assign(i);
11        next_card = 0;
12      }
13     
14      void deck::shuffle(void) {
15        int i, k;
16        card temp;
17        cout << "I am shuffling the deck\n";
18        for (i = 0; i < 52; i++)   {
19            k = rand() % 52;
20            temp = d[i];
21            d[i] = d[k];
22            d[k] = temp;
23        }
24      }
25     
26      void deck::print(void) const {
27        cout << "\nHere's the deck:\n";
28        for (int i = 0; i < 52; i++) d[i].print();
29      }
30      void deck::deal(int no_of_cards) {
31        cout <<"\nOk, I will deal you "<<no_of_cards<<" cards:\n";
32        for (int i = 0;i<no_of_cards; i++) d[next_card++].print();
33      }


1      // File: ex3-11.cpp - main()
2     
3      #include "ex3_11d.h"
4     
5      int main (void) {
6        deck poker;
7        poker.create_deck();
8        poker.print();
9        poker.shuffle();
10        poker.print();
11        poker.deal();
12        poker.deal(3);
13        return 0;
14      }


The output is the same as examples 3-7.

比較大的 C++ 程式分為幾個檔案: 一或多個 header files, function definition files, 和 main file.

沒有留言:

張貼留言