inheritance (sp?) problem

Started by
2 comments, last by Rhaal 19 years, 8 months ago
I am making a small card game and ran into a problem.

class CCard
{
public:
	CCard(): m_pTile( NULL ) {}
	~CCard()				{}
	
protected:
	int		iValue;
	int		iColor;
	CTile *m_pTile;
};

class CDeck : public CCard
{
public:
	CDeck()
	{
		Deck.reserve( 56 );
		
		int c = 1, v = 1;

		for( p_Deck = Deck.begin(); p_Deck!= Deck.end(); p_Deck++ )
		{
			if( v <= 14 )
			{
				p_Deck->iValue = v;
				v++;
			}
			else
			{
				p_Deck->iColor = c;
				
				v = 1;
				c+= 1;
			}
		}
				

	}
	~CDeck(){}

private:
	vector<CCard>Deck;
	vector<CCard>::iterator p_Deck;
};

when I compile this code I get an error that p_Deck can't see iColor and iValue because it's protected, now I thought that I had it CCard inherited and thus all variables should be vissible. Could someone tell me what is wrong here?
Advertisement
First, a deck is not a card, so CDeck should not inherit from CCard.
Second, vector::reserve() preallocates memory - it doesn't actually insert elements in the vector - use vector::resize().
Third, for a deck of cards, std::deque may prove more useful than std::vector. std::random_shuffle() may also be handy.

Now, regarding your question, while CDeck is allowed to access protected CCard members via a CDeck variable (because it is derived from CCard), it is not allowed to access them via a CCard variable.

class Base{protected:  int i;};class Derived : public Base{  void Foo( Derived& d ) { d.i = 0; } // OK !  void Bar( Base& b )    { b.i = 0; } // NOT OK!};


CDeck would have to be a friend of CCard to allow that.

class CDeck;class CCard{public:  CCard(): m_pTile( NULL ) {}  ~CCard() {}	protected:  int   iValue;  int   iColor;  CTile *m_pTile;  friend class CDeck;};class CDeck : public CCard{public:  CDeck()  {    Deck.reserve( 56 );    int c = 1, v = 1;    for( p_Deck = Deck.begin(); p_Deck!= Deck.end(); p_Deck++ )    {      if( v <= 14 )      {        p_Deck->iValue = v;        v++;      }      else      {        p_Deck->iColor = c;        v = 1;        c+= 1;      }    }  }  ~CDeck(){}private:  vector<CCard>Deck;  vector<CCard>::iterator p_Deck;};
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
thank you, I didn't think of it that way.
The main point here is that a Card is not a Deck, and a Deck is not a Card.

A Card is a Paper or is a Square, etc...

A Deck is not a type of Card, but a Deck HAS Cards. Here's an example:

class CDeck{    CCard m_card[52]; // Creates 52 CCard Instances public:    void DisplayCard(int card);    {        cout << "Card " << card << " is: " << m_card[card].GetDesc() << endl;    }}


Call it:
CDeck Deck;Deck.DisplayCard(1);


Of course this would require implementing a CCard class that has a method GetDesc() that returns a character array or string, and assigning values to all the CCard instances in the CDeck class but hey, time is short :)
- A momentary maniac with casual delusions.

This topic is closed to new replies.

Advertisement