简体   繁体   中英

Splitting written class into two files (C++)

So, I have a C++ class written in the main.cpp file of the project,like this:

class CAPiece
{
public:
    CAPiece(char cColor) : mcColor(cColor) {}
    ~CAPiece() {}
    virtual char GetPiece() = 0;
    char GetColor() {
        return mcColor;
    }
    bool IsLegalMove(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
        CAPiece* qpDest = qpaaBoard[iDestRow][iDestCol];
        if ((qpDest == 0) || (mcColor != qpDest->GetColor())) {
            return AreSquaresLegal(iSrcRow, iSrcCol, iDestRow, iDestCol, qpaaBoard);
        }
        return false;
    }
private:
    virtual bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) = 0;
    char mcColor;
};

Is there a way to transfer it to the two separate files created by the Visual Studio Class Manager, without completely recoding it? (By two separate I mean CAPiece.h and CAPiece.cpp files)

Is as simple as creating the two files, cutting copying things inside and then adding #include "CAPiece.h" at the beginning of files in which you need this class.

CAPiece.h :

class CAPiece
{
public:
    CAPiece(char cColor);
    ~CAPiece();
    virtual char GetPiece() = 0;
    char GetColor();
    bool IsLegalMove(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]); 

private:
    virtual bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) = 0;
    char mcColor;
};

CAPiece.cpp

#include "CaPiece.h"

CAPiece::CAPiece(char cColor) 
       : mcColor(cColor) 
{}
CAPiece::~CAPiece() 
{}
char CAPiece::GetColor() 
{
   return mcColor;
}
bool CAPiece::CAPieceIsLegalMove(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) 
{
    CAPiece* qpDest = qpaaBoard[iDestRow][iDestCol];
    if ((qpDest == 0) || (mcColor != qpDest->GetColor())) {
       return AreSquaresLegal(iSrcRow, iSrcCol, iDestRow, iDestCol, qpaaBoard);
    }
    return false;
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM