473,769 Members | 5,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple File Programs

71 New Member
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.
Jun 22 '07 #1
2 2701
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

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 New Member
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
6003
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 original service under a new name but the SCM complains that the executable does not have this service implemented. Please note that I need to have distinct instances of executables installed not merely multiple windows services defined within...
9
2593
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 gameloop.cpp phead.h has all my core declarations in it like my main globals. main.cpp has all my window initilization functions and my winproc loop.
1
7221
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 copied into the directory and inserts the data into a data warehouse. Process 3 queries the data warehouse and transfers the results to a data mart.
3
15655
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
2191
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
9364
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 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
2
1401
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 add multiple files at the same time. for example in windows, you can select multiple programs to exicute at the same time. i am looking to do something similar, in that the user selects the files in the classic windows 'open...' window. from...
19
6344
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 can all access. It seems like a needless waste of memory to make them all maintain their own copy of the same data in RAM at the same time. What's the best way to achieve this? I've heard of memory mapped files, so maybe that's the answer. ...
2
1358
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 computers The programs do auto updating from my website, where they download any new updates and the update program runs on the next restart of the computer
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.