-6

If I have have a classes "Card" (Base Class) "CardOfType1" (Derived Class) and a class named "Player" having pointers of type 'Card' referring to 'CardOfType1'. Is it possible that we have a pure virtual function named 'playCard(Player enemyPlayer)'?

For more understanding, the code is given below

class Card
{
public:
    virtual void playCard(Player enemyPlayer) = 0;
};

class CardOfType1
{
public:
    void playCard(Player enemyPlayer)
    {
         //Some Code Goes here
    }
};

class Player
{
stack<Card *> deckOfCards
//.
//.
//.

};
Talha Khan
  • 51
  • 2
  • 5

1 Answers1

1

yes, a PVF can have parameters.

virtual void playCard(Player enemyPlayer) = 0;

here = 0 (is not assigning), Simply we are informing to compiler that function will be pure and does not having any body(where its declared, in that class), but it can have parameter.

From the n4659 C++ standard

A pure virtual function need be defined only if called with, or as if with (15.4), the qualified-id syntax (8.1).

class shape {
  point center;
  public:
  virtual void rotate(int) = 0; // pure virtual
  virtual void draw() = 0; // pure virtual
};

But there is another observation

A function declaration cannot provide both a pure-specifier and a definition — end note ]

struct C {
   virtual void f() = 0 { };
};
Achal
  • 11,821
  • 2
  • 15
  • 37
  • 1
    not quite; =0 and "does not have a body" are not mutually exclusive. One can have `virtual void playCard(Player derp) = 0 {std::cout << "here";} ` [this](https://stackoverflow.com/questions/2609299/use-cases-of-pure-virtual-functions-with-body) question may be of use – UKMonkey Feb 26 '18 at 14:03
  • @UKMonkey I mean in `base class` a `PVF` can't have body, in respective `derived class` it should have. Correct me If I am wrong – Achal Feb 26 '18 at 14:06
  • @UKMonkey Not in the same declaration. – Angew is no longer proud of SO Feb 26 '18 at 14:06
  • @achal You can provide an implementation for a PVF, but it has to be out-of-line (i.e. not as part of the in-class declaration). – Angew is no longer proud of SO Feb 26 '18 at 14:07
  • @Angew thanks the one - thanks :) although I think VS has allowed me to do it inline in the past - but what VS do and the standard say aren't always the same ;) – UKMonkey Feb 26 '18 at 14:07
  • @Angew Ok. Thanks for more clarification. – Achal Feb 26 '18 at 14:08