Thread: Splitting Code Into Multiple Files

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Splitting Code Into Multiple Files

    Hi guys, basically I'm wondering if it's possible to split my code into multiple files. At the moment I've got my class declaration in its own header (.h) file, but I'm wanting to put the actual function definitions in a seperate .cpp file because they're taking up a lot of room... So I tried to make a file called MySwitch.cpp, and did #include "MySwitch.cpp" but that didn't seem to work, so I'm not quite sure what to do. I basically want to chop out those functions with the switch statements in them, and put them in their own file, and then link it into the main source file. Here is the code:

    bits.cpp:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include "MyClass.h"
    
    using namespace std;
    
    CardDeck::CardDeck() {
    
    	this->cards = new unsigned char[52];
    	int n = 0;
    
    	for (int i = 0; i < 0xD; i++)
    		for (int j = 0; j < 4; j++)
    			this->cards[n++] = ((j << 4) | i);
    }
    
    CardDeck::~CardDeck() {
    	delete [] this->cards;
    }
    
    char CardDeck::getCard(int x) {
    	return this->cards[x];
    }
    
    int main() {
    	CardDeck c;
    	for (int x = 0; x < 52; x++) {
    		cout << endl << "Card " << x << ": ";
    		c.outputBinary( c.getCard( x ) );
    		cout << " number: " << c.Number( c.getCard( x ) ) << " Suit: " << c.Suit( c.getCard( x ) );
    	}
    }
    
    void CardDeck::outputBinary(char value) {
    
    	unsigned char x;
    	
    	for (x = 0x80; x; x >>= 1)
    		if (x & value) printf("1");
    		else printf("0");
    
    }
    
    string CardDeck::Number(char value) {
    
    	unsigned char x = (0xF0 & (value << 4));
    	string number;
    	string suit;
    
    	switch ( x ) {
    
    		case 0x0:
    			number = "Ace";
    			break;
    		case 0x10:
    			number = "2";
    			break;
    		case 0x20:
    			number = "3";
    			break;
    		case 0x30:
    			number = "4";
    			break;
    		case 0x40:
    			number = "5";
    			break;
    		case 0x50:
    			number = "6";
    			break;
    		case 0x60:
    			number = "7";
    			break;
    		case 0x70:
    			number = "8";
    			break;
    		case 0x80:
    			number = "9";
    			break;
    		case 0x90:
    			number = "10";
    			break;
    		case 0xA0:
    			number = "Jack";
    			break;
    		case 0xB0:
    			number = "Queen";
    			break;
    		case 0xC0:
    			number = "King";
    			break;
    	}
    
    	return number;
    }
    
    string CardDeck::Suit(char value) {
    
    	unsigned char x = 0x30 & value;
    	string suit;
    
    	switch ( x ) {
    
    		case 0x0:
    			suit = "Spade";
    			break;
    		case 0x10:
    			suit = "Heart";
    			break;
    		case 0x20:
    			suit = "Diamond";
    			break;
    		case 0x30:
    			suit = "Club";
    			break;
    	}
    	
    	return suit;
    }
    MyClass.h:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class CardDeck {
    
    public:
    	CardDeck();
    	~CardDeck();
    	void outputBinary(char value);
    	char getCard(int x);
    	string Number(char value);
    	string Suit(char value);
    
    private:
    	unsigned char * cards;
    
    };
    Thanks!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Generally:
    1. Put class definitions and function declarations in header file.
    2. Put function definitions in source file, and include header files if necessary.
    3. Compile source files and link the generated object files.

    So you would not include "MySwitch.cpp" anywhere. Of course, when things like C++ templates come into the picture, you would place the function template definitions in the header file as well.

    Incidentally, do not use using directives (e.g., using namespace std) in header files except within some restricted scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Typically, if you have a class declaration in the header, you put the implementation in a separate cpp file, include the header in both this file and main.cpp and make sure that all cpp files are compiled and linked (the IDE should help with it).

    What you have is basically still a one-file project, since there is only one cpp file and the contents of the header just get pasted to the place where you include it.

    As for the switch, couldn't you just shift the binary value back and use it as an index to a string array that contains the names of the values and suits, so you wouldn't have to use any switches?

    (And unless it is not an assignment, why not use two chars - or even ints - in the first place?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Just for a challenge anon, but I'll try the string array method, sounds like fun Cheers for the help guys.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Moving files from one directory to multiple dirs
    By csj561 in forum C Programming
    Replies: 7
    Last Post: 03-18-2005, 03:52 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM