abstract class problem

Started by
1 comment, last by AndrewBlalock 12 years, 8 months ago
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.


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";

}


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




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";

}





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




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();

}



Now the game class is where I am getting my errors

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

cannot instantiate abstract class

[/font][/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"]



[/font][/font]
Advertisement
C++ is case sensitive. At least one of your problems comes from the fact that isHitting is not the same as IsHitting.

C++ is case sensitive. At least one of your problems comes from the fact that isHitting is not the same as IsHitting.



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

This topic is closed to new replies.

Advertisement