473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multiple File Programs

71
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<<(ostream& 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.
Jun 22 '07 #1
2 2670
weaknessforcats
9,208 Expert Mod 8TB
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.cpp 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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
at the top of the file.
Jun 22 '07 #2
rsteph
71
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?
Jun 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
9
by: Daniel Moree | last post by:
I'm using MS VC++ 6.0 I'm working on a big project. I've currently have several files for this project. Here's the problem. I have one header file phead.h I have two code files main.cpp and...
1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
3
by: UJ | last post by:
Has anybody done anything with using log4net where you have multiple programs on a single machine writing to the same log file? TIA - J.
8
by: subramanian100in | last post by:
Suppose I have #include <stdio.h> #include <string.h> size_t strlen(const char *str) { printf("from strlen - %s\n", str); return 0; // return some dummy value
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
2
by: michael40card | last post by:
First of all... Hi I am attempting to create a html 'gallery creater' for a program... everythink was going ok, seems to work. the only trouble is i cannot find any way of allowing the user to...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
2
by: Steve | last post by:
Hi All I have several POS programs (Windows forms VS 2005) Some customers have multiple copies of the programs, all networked to the 1 SQL Server 2005 Database which resides on 1 of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.