I am brushing up for c++ for an interview next week. I figured the best way to do this was to make some fake classes and mess around.
I am working on making playing cards, and a deck of cards.
The Card Class seems to work well on its own. However, In order to remember some c++ I'm implementing a deck with use of templates and a dynamic array.
Here is the basic layout of Deck (I edited some methods out for brevity):
Here is how I am adding cards to a deck.
Testing Code:
output:
1 of Spades is the default constructor of a Card.
Whats more worrisome is that when i try to access cards[0] I get a bad access.
i.e.
Here is the Card constuctors and assignment operator for reference:
I know that the way I expand my array is not the best... but I need help figuring out why the cards aren't being assigned correctly.
Let me know if there is anything else that I should post.
I am working on making playing cards, and a deck of cards.
The Card Class seems to work well on its own. However, In order to remember some c++ I'm implementing a deck with use of templates and a dynamic array.
Here is the basic layout of Deck (I edited some methods out for brevity):
Code:
template <class T>
class Deck
{
private:
T* cards;
int deckSize;
public:
Deck(int size = INIT_SIZE);
Deck(int size,T card);
Deck(int size,T card[]);
~Deck();
Deck operator+(const Deck) const;
void addCard(T);
};
Here is how I am adding cards to a deck.
Code:
template<class T>
void Deck<T>::addCard(T card)
{
cout << "START ADD CARD " << deckSize << endl;
T* cards_new;
cards_new = new T[deckSize+1];
for(int i = 0; i < deckSize; i++)
{
cards_new[i] = cards[i];
cout << cards_new[i] << endl;
}
cards_new[deckSize] = card;
deckSize++;
cards = cards_new;
delete [] cards_new;
cout << "END ADD CARD " << deckSize << endl << endl;
}
Testing Code:
Code:
Deck<Card> createStdDeck()
{
cout << "Creating Deck" <<endl;
Deck<Card> rtn = Deck<Card>(1,Card(Suit(0),1));
cout << "Bottom 1 " <<rtn.showBottom() << endl;
for (int s = 0; s < 2; s++){
for (int i = 1;i < 3;i++){
rtn.addCard(Card(Suit(s),i+1));
}
}
return rtn;
}
int main (int argc, char * const argv[]) {
string junk;
Deck<Card> deck1 = Deck<Card>(1,Card(Suit(1),1));
Deck<Card> deck = createStdDeck();
cout << endl;
cout << "Done!" << endl;
}
output:
Code:
Creating Deck
Bottom 1 |1 Hearts|
START ADD CARD 1
|1 Hearts|
END ADD CARD 2
START ADD CARD 2
|1 Spades|
|1 Spades|
END ADD CARD 3
START ADD CARD 3
|1 Spades|
|1 Spades|
|3 Hearts|
END ADD CARD 4
START ADD CARD 4
|1 Spades|
|1 Spades|
|1 Spades|
|1 Spades|
END ADD CARD 5
Done!
1 of Spades is the default constructor of a Card.
Whats more worrisome is that when i try to access cards[0] I get a bad access.
i.e.
Code:
template<class T>
T Deck<T>::showBottom() const
{
return cards[0];
}
Here is the Card constuctors and assignment operator for reference:
Code:
Card::Card()
{
m_suit = Suit(3);
m_Cardinal_value = 1;
}
Card:: Card(Suit s, int n)
{
m_suit = s;
m_Cardinal_value = n;
}
Card& Card::operator = (const Card &right)
{
if (this != &right) // protect against invalid self-assignment
{
m_suit = right.m_suit;
m_Cardinal_value = right.m_Cardinal_value;
}
return *this;
}
I know that the way I expand my array is not the best... but I need help figuring out why the cards aren't being assigned correctly.
Let me know if there is anything else that I should post.
Last edited: