Abstract / Basic structure in C ++?

I am making a chess game and I would like to have an array of pieces.

If I'm right, in Java you can have an abstract Piececlass and Kingor Queenextend this class. If I had to create an array Piece, I could have put the snippet Kingin this array, and Queenelsewhere, because Kingand Queenexpand Piece.

Is there a way to do this with structures in C ++?

+5
source share
6 answers

Yes. You can create an abstract base class in C ++. Just set one or more methods as virtual:

class Piece {
    public:
        Piece ();
        virtual ~Piece ();
        virtual void SomeMethod () = 0;  // This method is not implemented in the base class, making it a pure virtual method. Subclasses must implement it
 };

,

class King : public Piece {
    // ... the rest of the class definition
    virtual void SomeMethod (); // You need to actually implement this
};
+5

++, .

++ , .
++ .

+4

.

class AbstractPiece
{
public:
virtual void aFunction()=0; //this makes AbstractPiece an abstract class.
virtual ~AbstractPiece();
};

class King : public AbstractPiece
{
public:
virtual void aFunction(); //this is the one that would get called
virtual ~King();
};

, ,

AbstractPiece* array[size];

array[index] = new King();
+3

++ :

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/construct.hpp>

using namespace std;

struct piece
{
    virtual ~piece() {};
    virtual void dosomething() = 0;
};

struct king : piece
{
    ~king() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout << __PRETTY_FUNCTION__ << endl; }
};

struct queen : piece
{
    ~queen() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout <<  __PRETTY_FUNCTION__ << endl; }
};

struct pawn : piece
{
    ~pawn() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout << __PRETTY_FUNCTION__ << endl; }
};

:

int main()
{
    typedef std::vector< piece * > pieces_t;
    pieces_t pieces;
    pieces.push_back(new queen());
    pieces.push_back(new king());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());

    // calling dosomething()
    std::for_each(pieces.begin(), pieces.end(), 
          boost::lambda::bind(&piece::dosomething, boost::lambda::_1));

    // destructing
    std::for_each(pieces.begin(), pieces.end(), 
          boost::lambda::bind(boost::lambda::delete_ptr(), boost::lambda::_1));
}

:

virtual void queen::dosomething()
virtual void king::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual queen::~queen()
virtual king::~king()
virtual pawn::~pawn()
virtual pawn::~pawn()
virtual pawn::~pawn()
virtual pawn::~pawn()
+2

, , .

. PieceType, . , , :

class Board;

class PieceType
{
public:
    virtual showValidMoves(Board& board) const = 0;
    virtual showInitialPosition(Board& board) const = 0;
    // ...  
};

class Pawn : public PieceType
{
public:
    virtual showValidMoves(Board& board) const;
    virtual showInitialPosition(Board& board) const;
    // ...
};

class Rook : public PieceType
{
    // ...
};
//...

PieceType, , const:

const Pawn PAWN;
const Rook ROOK;
const Knight KNIGHT;

, :

class ChessMan
{
public:
    enum Colour {Black, White};

    ChessMan(const Colour& colour, PieceType& pieceType);

    void showValidMoves(Board& board);
    void showInitialPosition(Board& board);
private:
    Colour m_colour;
    PieceType& m_pieceType;
};

ChessMan :

void add(vector<ChessMan>& chessmen,
         const ChessMan::Colour& colour,
         const PieceType& pieceType,
         const unsigned int amount)
{
    chessmen.insert(chessmen.end(), ChessMan(colour, pieceType), amount);
}

void main()
{
    using std::vector;

    vector<ChessMan> chessmen;
    add(chessmen, 16, ChessMan::Black, PAWN);
    add(chessmen, 2, ChessMan::Black, ROOK);
    add(chessmen, 2, ChessMan::Black, KNIGHT);
    //...
}

!

+2

++ , Java. ++ , , .

Abstract classes in C ++ are simply classes with pure virtual methods in them. These are methods without a method definition.

Your solution might look something like this:

class Piece {
public:
   virtual ~Piece ();
   virtual void move() = 0; //this is a pure virtual function, making this an abstract class.
};

class King : public Piece {
public:
   void move() {};
};

class Queen : public Piece {
public:
  void move() {};
};

....

Piece *myPieces[2] = new Piece*[2];
myPieces[0] = new King();
myPieces[1] = new Queen(); 
+1
source

All Articles