Little Rusty on C++, need OO and pointer Help.

Discussion in 'Mac Programming' started by jmerkow, Jan 20, 2011.

  1. jmerkow, Jan 20, 2011
    Last edited: Jan 20, 2011

    jmerkow macrumors newbie

    Joined:
    Jan 20, 2011
    #1
    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):

    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.
     
  2. jmerkow thread starter macrumors newbie

    Joined:
    Jan 20, 2011
    #2
    I think i figured it out....

    Code:
    	delete [] cards;
    	cards = cards_new;
    
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    Hmm.

    If I were interviewing you, this code would have me raise my eyebrows. You demonstrate knowledge of template metaprogramming and operator overloading, but your home-grown storage is a tell-tale sign you haven't used C++ in quite a while. If a production programmer checked in code like this, I'd be at his desk in no time.

    Look into the STL, specifically std::vector. You'll love it.

    P.S. Good luck in the interview, of course!
     
  4. jmerkow thread starter macrumors newbie

    Joined:
    Jan 20, 2011
    #4
    I know about the vector class, the whole point of doing this was to re-learn a lot of stuff. i.e. how do use pointers properly etc. Plus its my second day of review so gimmie some slack, I haven't programmed c++ in about 5 years.

    I will likely be posting random things on here when I get stuck! thanks for the help though.
     
  5. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    If you answered this (during your job interview), saying "Of course I know about the higher level stuff; I just want to understand how this stuff works", I'd probably give you an extra brownie point.

    On the other hand, the STL is so rich that you might want to brush up on it soon (you said you only have one week). Note that the STL is quite unique in that it gives you performance boundaries too, so the often-heard argument *against* using it ("I'm not sure whether it's optimal, so I'd rather code something myself") doesn't score points with me :)

    Again, good luck, and keep us posted.
     
  6. jmerkow thread starter macrumors newbie

    Joined:
    Jan 20, 2011
    #6
    Thanks. I am more of a machine learning guy, which is what I will be working on. But I was told it would involve some coding. I don't want to be a code monkey but the interview is with google, so I figure brushing up on data structures, c++ basics, can't hurt since its so competitive.
     

Share This Page