Multiple File Programs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsteph
    New Member
    • Oct 2006
    • 71

    Multiple File Programs

    I took a number of classes back in college with Java, VB, and VB.NET, and a few in C++. I was looking through some of my books the other day and decided to brush up on my C++ programming, as I'd like to get back into writing programs.

    I went out and bought a book called "Beginning C++ Game Programming" figuring since I like games it might be fun to refresh myself in the syntax in logic in a very basic game programming type format. I'm having a little problem though with working out how to break a given program into multiple files, to help make things more clear. The last program in the book is a very simple version of blackjack. It's 606 lines long with code and all my commenting, with 7 different classes. I want to break each class into it's own file that's included.

    The way the suggest to do this is to break the class into two files each with the same name as the class, one a header file (.h) that has the class definition, and one a (.cpp) file with all the member function definitions. My first question is do you have to break things down like this, or could you include both in one file like you can in other languages? Second I am having a problem with an overloaded operator (<<) that they use that's a friend of the Card class. When I try to break it into it's own file, and then include it (#include "card.h") it gives me an error with an undefined ostream. I include the iostream header in the card.cpp file, do I need to include it as well in the card.h file; or is there some other more basic problem I'm missing?

    Here's the Card class definition (without the member function definitions):

    class Card
    {
    public:
    //Create a list of card values
    enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
    TEN, JACK, QUEEN, KING};
    //Create a list of card suits
    enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};

    //Overloading << operator so can send Card object to standard output
    friend ostream& operator<<(ostr eam& os, const Card& aCard);

    //Constructor
    Card(rank r = ACE, suit s = SPADES, bool ifu = true);

    //Returns the value of a card, 1 - 11
    int GetValue() const;

    //Flips a card; if face up, becomes face down and vice versa
    void Flip();

    private:
    rank m_Rank; //Holds the cards rank (value)
    suit m_Suit; //Holds the cards suit
    bool m_IsFaceUp; //Holds wether the card is face up or not
    }; //End of class Card

    That is essentially what I have in the card.h file, between the #ifndef, #define, and #endif lines. Any help in this would be greatly appreciated. If I can figure how to work this out I'm going to start planning what kind of program I want to try and make on my own accord - to see how well I've refreshed myself.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Let's take this in pieces.

    First, C++ supports separate compilation. That is multiple .cpp files can be compiled into object files that are then copied by the linker into the final executable. The linker will resolve any unresolved external references (things not in the object file).

    So, each file is separately compiled. If two .cpp files need your Card class then you can a) keep a copy of the class in both files and do double maintenance to keep the copies in sync (yech) or b) put the class definition in it's own file and #include that file in both of your .cpp files.

    I know which way I would go.

    Conventionally, The Card member functions are in Card.cpp and the class definition is in Card.h. But this is just a convention. If you organize your code this way, others will have an easier time maintaining it. I mean, if the Card member functions are in pootwattle10.cp p it's not obvious they're there.

    Now about your friend function. A friend function is not a member function. It is just a regular global function that has been given permission to access the class private members by virtue of placing the prototype of the function in the class defintion as a friend.

    So, if the operator<< function is in it's own file, then you must have (as I saidabove) the ostream class definition in the file or a copy of it. Here's where you #include <iostream>.

    Lastly, most library classes are in the C++ Standard Library and these library classes are in the std namespace so be sure the file with your operator<< has:

    [code=cpp]
    #include <iostream>
    using namespace std;
    [/code]

    at the top of the file.

    Comment

    • rsteph
      New Member
      • Oct 2006
      • 71

      #3
      Thank you for the help, I figured it was something basic I was missing. I wasn't putting the 'using namespace std;' line in the .h or .cpp files for the classes. I got it up and running now, thank you.


      Out of curiosity is it possible in C++ to include both the class definition and member function definitions in the header file (ex: card.h), and skip making the .cpp class file? Or do you have to have both the .h and .cpp file for the class with the class definition and member functions defiintions respectively?

      Comment

      Working...