Sign in to follow this  
AndrewBlalock

abstract class problem

Recommended Posts

I am learning about abstract classes and when I go through the example I am getting an error saying that I cannot use a class beacuse its base class is an abstract class.

This is the abstract class that I am dealing with.

[code]
class GenericPlayer : public Hand

{

friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);



public:

GenericPlayer(const string& name = "");

virtual ~GenericPlayer();



// indicates where or not the generic player wants to keep hitting

virtual bool isHitting() const = 0;



// returns wethere generic player has busted - has a total greater than 21

bool isBusted() const;



// announces the gnereic player busts

void Bust() const;



protected:

string m_Name;

};



GenericPlayer::GenericPlayer(const string& name): m_Name(name) {}



GenericPlayer::~GenericPlayer()



{}



bool GenericPlayer::isBusted() const

{

return (GetTotal() > 21);

}



void GenericPlayer::Bust() const

{

cout << m_Name << " busts.\n";

}
[/code]

Now These are two players that are dervied from the GenericPlayer class. One is a Computer and one is a Player.

[code]


class Player : public GenericPlayer

{

public:

Player(const string& name = "");



virtual ~Player();



// returns whther or not the player wants another hit

virtual bool IsHitting() const;



// announces that the player wins

void Win() const;



// announces that the player loses

void Lose() const;



// announces that the player pushes

void Push() const;

};



Player::Player(const string& name): GenericPlayer(name)

{}



Player::~Player()

{}



bool Player::IsHitting() const

{

cout << m_Name << ", do you want a hit (Y/N): ";

char response;

cin >> response;

return (response == 'y' || response == 'Y');

}



void Player::Win() const

{

cout << m_Name << " wins.\n";

}



void Player::Lose() const

{

cout << m_Name << " loses.\n";

}



void Player::Push() const

{

cout << m_Name << " pushes.\n";

}



class House : public GenericPlayer

{

public:

House(const string& name ="");



virtual ~House();



// indicates wether the house is hitting - will alawys hit on 16 or less

virtual bool IsHitting() const;



// flips over firsts card

void FlipFirstCard();

};



House::House(const string& name): GenericPlayer(name) {}



House::~House()

{}



bool House::IsHitting() const

{

return (GetTotal() <= 16);

}



void House::FlipFirstCard()

{

if(!(m_Cards.empty()))

m_Cards[0]->Flip();

else

cout << "No card to flip!\n";

}



[/code]

Now I have a game class and which hands the computer and player objects and process them depending on which one they are using

[code]


class Game

{

public:

Game(const vector<string>& names);



~Game();



// plays the game of blackjack

void Play();

private:

Deck m_Deck;

House m_House;

vector<Player> m_Players;

};



Game::Game(const vector<string>& names)

{

// create a vector of players from a vector of names

vector<string>::const_iterator pName;

for(pName = names.begin(); pName != names.end(); ++pName)

m_Players.push_back(Player(*pName));



srand(time(0)); // seed the random number generator

m_Deck.Populate();

m_Deck.Shuffle();

}

[/code]

Now the game class is where I am getting my errors

[font="Consolas"][size="1"][font="Consolas"][size="1"]

cannot instantiate abstract class

[/size][/font][/size][/font]and this is refering to both the Player and the House objects. But I thought that the GenericPlayer was the abstract class and I am not trying to initliaize any methods from that as I can see. Im kind of confused as to why this is happening.
[font="Consolas"][size="1"][font="Consolas"][size="1"]



[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
C++ is case sensitive. At least one of your problems comes from the fact that isHitting is not the same as IsHitting.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1312749729' post='4845889']
C++ is case sensitive. At least one of your problems comes from the fact that isHitting is not the same as IsHitting.
[/quote]


You were right I know thats its case sensitive just didnt pick up on that thanks for the help

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