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!