Sign in to follow this  
kappa

inheritance (sp?) problem

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this