0

Does an abstract class without any data fields need a constructor?

Also, since the implementation of the makeAMove function is in the derived classes, is it necessary to create a separate implementation file for this Player class or is a this single .h file alright?

#pragma once // include guard
#include "Board.h"

class Player
{
    public:
        virtual void makeAMove(Board &myBoard) = 0; // pure virtual function
};
navig8tr
  • 1,724
  • 8
  • 31
  • 69

2 Answers2

3

Every class has a constructor, probably more than one. However, you don't always need to declare or define a constructor yourself, since under favourable conditions this happens implicitly. Such is the case in your example.

You also don't need an implementation file, since that would not contain anything.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The destructor would also be created implicitly as well, right? – navig8tr Dec 15 '13 at 23:25
  • @navig8tr: sure, and assignment operators, too. – Kerrek SB Dec 15 '13 at 23:29
  • He could theotetically implement the pure virtual functions too, if he felt the need for a default implementation that he could explicitly. Beyond the scope of this I suppose, but possible :-) – splrs Dec 15 '13 at 23:42
0

Yes, if the purpose of the abstract class is to provide polymorphic functionality through virtual funcions, that is, the class is an interface.

The base class should have a virtual dtor to ensure the correct destruction of polymorphic instances.

A good rule is: Every time a class hierarchy is dessigned to provide polymorphic functionality through dynamic binding, its base class should have a virtual dtor.

About classes and headers, C++ does not restrict you to write one class per file (as Java does).

What is more correct, to write one class per file or more than one classes? Depends, I think this is subjective. But in general C and C++ uses headers to provide functionality, and functionality commonly implies more than one class.

Manu343726
  • 13,969
  • 4
  • 40
  • 75