[英]C++ implementing abstract classes
I am trying to write a derived class called TerminalPlayer that inheritances a class Player given the declaration of virtual const Card playCard(const Card opponentCard) = 0; 我正在尝试编写一个名为TerminalPlayer的派生类,该类继承给定了虚拟const Card playCard(const Card反对者Card)= 0的声明的Player类。 how would you implement the inherited playCard in the abstract class and what does the = 0 at the end of the prototype mean?
您将如何在抽象类中实现继承的playCard?原型末尾的= 0是什么意思?
I also have the error in the main testing code that gives the error: cannot allocate an object of abstract type 'Player'. 我在提供错误的主要测试代码中也有错误:无法分配抽象类型“ Player”的对象。 I think it is because I am not implementing the Player class correctly but I don't know how to fix it.
我认为这是因为我没有正确实现Player类,但我不知道如何解决它。
Player.h Player.h
#ifndef PLAYER_H_
#define PLAYER_H_
#include <vector>
#include "Card.h"
#define MAX_HAND_SIZE 3
// Abstract Player classS
class Player {
public:
// Deconstructor
virtual ~Player() {
}
// Play a card. If the player receives a joker then this player is going first
virtual const Card playCard(const Card opponentCard) = 0;
// Receive a card from the dealer
void receiveCard(const Card c) {
hand.push_back(c);
}
// Add points to the score
void addScore(unsigned s) {
score += s;
}
// Get the score
int getScore() const {
return score;
}
// Return true if the player has cards in the hand
bool hasCards() const {
return (hand.size() != 0);
}
// Receive the cards played from the previous round. This member function would be used by a computer player that may need to 'see' what cards were played.
void cardsPlayed(const Card card1, const Card card2) {
}
// Output the players name
friend std::ostream& operator <<(std::ostream& out, const Player& p);
protected:
// Constructor. Since this is an abstract class we do not want anyone instantiating a player class so we make it protected.
Player(std::string name) :
score(0), name(name), hand(0) {
}
int score;
std::string name;
std::vector<Card> hand;
};
#endif
TerminalPlayer.h TerminalPlayer.h
#ifndef TERMINALPLAYER_H_
#define TERMINALPLAYER_H_
#include "Player.h"
class TerminalPlayer : public Player {
public:
TerminalPlayer(std::string name);
virtual ~TerminalPlayer();
};
#endif
TerminalPlayer.cpp TerminalPlayer.cpp
#include "Player.h"
Card playCard(const Card opponnentCard){
// TODO: playCard code here
}
Test.cpp TEST.CPP
int main(){
// This initialization give error: cannot allocate an object of abstract type ‘Player’
TerminalPlayer player1 = Player("Player1");
return 0;
}
The = 0'
means that this is a pure virtual
function. = 0'
表示这是一个pure virtual
函数。 This type of function must be defined by any class which inherits from the base class AND is instantiated within a program. 这种类型的函数必须由从基类继承并在程序中实例化的任何类定义。
Since your base class declares: 由于您的基类声明:
// Play a card. If the player receives a joker then this player is going first
virtual const Card playCard(const Card opponentCard) = 0;
You should implement this function within your derived class. 您应该在派生类中实现此功能。 You come close in TerminalPlayer.cpp:
您在TerminalPlayer.cpp中接近:
const Card TerminalPlayer::playCard(const Card opponnentCard){
// TODO: playCard code here
}
What you are missing the the TerminalPlayer::
scoping shown above. 您缺少的是上面显示的
TerminalPlayer::
作用域。 Also missing is the function declaration in the derived class. 还缺少派生类中的函数声明。 You need to add:
您需要添加:
virtual const Card playCard(const Card opponentCard) override;
Within the class TerminalPlayer
. 在类
TerminalPlayer
。 Put it right after the destructor. 将其放在析构函数之后。
That should do it. 那应该做。
One thought: the const qualifier on the return value is not necessary since you are returning by value. 一个想法:返回值上的const限定符不是必需的,因为您是按值返回的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.