在c ++中继承,而不是访问变量

时间:2015-04-18 12:20:02

标签: c++ inheritance

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define pp 1
#define rp 5
#define bp 3
#define kp 3
#define qp 9
int maxp=0;
int minp=0;
class Play;
Play **play;

using namespace std;


class Game
{
    public:
    char board[8][8][3];
    char *****possibilities;

};


class Pawn:virtual public Game{

    public:
        int limit,cx,cy;
        char sym[2];
    int possiblec[4][2];
    Pawn(){

    }

    int move(int tx,int ty)
    {


        if(limit==0)
        {
            cout<<"This piece cannot be moved!!"<<endl;
            return 0;
        }
        for(int i=0;i<limit;i++)
        {
            if(tx==possiblec[i][0]&&ty==possiblec[i][1])
            {
                goto MV;
            }
        }
        cout<<"The Move you entered is not valid!\nPLease enter again!\n";
        return 0;

        MV:
            strcpy(board[cx][cy],"--");
            strcpy(board[tx][ty],this->sym);
    }
    void valid(int indx,int fx,int fy)
    {
        cout<<"Inside Valid!"<<endl;

        cx=fx;
        cy=fy;

        if(cx==-1&&cy==-1)
        {
            cout<<"no piece error";
            exit(0);
        }
        cout<<this->board[1][1][0]<<endl;
        //diagonal check
        int i=0;
        //pawn check
        if(board[cx+1][cy-1][0]=='p'||board[cx+1][cy-1][0]=='r'||board[cx+1][cy-1][0]=='n'||board[cx+1][cy-1][0]=='b'||board[cx+1][cy-1][0]=='q')
        {
            cout<<"Left Diagonal check!"<<endl;
            possiblec[i][0]=cx+1;
            possiblec[i][1]=cy-1;
            i++;
        }
        if(board[cx+1][cy+1][0]=='p'||board[cx+1][cy+1][0]=='r'||board[cx+1][cy+1][0]=='n'||board[cx+1][cy+1][0]=='b'||board[cx+1][cy+1][0]=='q')
        {
            possiblec[i][0]=cx+1;
            possiblec[i][1]=cy+1;
            i++;
        }
        if(cx==1&&board[cx+2][cy][0]=='-')
        {
            cout<<"Inside 3"<<endl;
            possiblec[i][0]=cx+2;
            possiblec[i][1]=cy;
            i++;
        }
        if(board[cx+1][cy][0]=='-')
        {
                cout<<"Inside 4"<<endl;
            possiblec[i][0]=cx+1;
            possiblec[i][1]=cy;
            i++;
        }
        limit=i;


    }
    Pawn(int i)
    {
        char a[2];
        a[0]='P';
         a[1]=(char)(i+48);

        strcpy(sym,a);
    }

};



class pawn:virtual public Game{
    public:
        char sym[2];
        int possiblec[4][2];
        pawn(){
        }
        pawn(int i)
        {
            char a[2];
            a[0]='p';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }


};

class Rook:virtual public Game{
        public:
        char sym[2];
        int possiblec[14][2];
        Rook(){
        }
        Rook(int i)
        {
            char a[2];
            a[0]='R';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class rook:virtual public Game{
    public:
        char sym[2];
        int possiblec[14][2];
        rook(){
        }
        rook(int i)
        {
            char a[2];
            a[0]='r';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }
};

class Knight:virtual public Game{
        public:
        char sym[2];
        int possiblec[8][2];
        Knight(){
        }

        Knight(int i)
        {
            char a[2];
            a[0]='N';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class knight:virtual public Game{
        public:
        char sym[2];
        int possiblec[8][2];
        knight(){
        }
        knight(int i)
        {
            char a[2];
            a[0]='n';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class King:virtual public Game{
        public:
        char sym[2];
        int possiblec[8][2];
        King(){
        }
        King(int i)
        {
            char a[2];
            a[0]='K';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class king:virtual public Game{
        public:
        char sym[2];
        int possiblec[8][2];
        king(){
        }
        king(int i)
        {
            char a[2];
            a[0]='k';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class Bishop:virtual public Game{
        public:
        char sym[2];
        int possiblec[13][2];
        Bishop(){
        }
        Bishop(int i)
        {
            char a[2];
            a[0]='B';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class bishop:virtual public Game{
        public:
        char sym[2];
        int possiblec[13][2];
        bishop(){
        }
        bishop(int i)
        {
            char a[2];
            a[0]='b';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }
};

class Queen:virtual public Game{
        public:
        char sym[2];
        int possiblec[27][2];
        Queen(){
        }
        Queen(int i)
        {
            char a[2];
            a[0]='Q';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};

class queen:virtual public Game{
        public:
        char sym[2];
        int possiblec[27][2];
        queen(){
        }
        queen(int i)
        {
            char a[2];
            a[0]='q';
            a[1]=(char)(i+48);
            strcpy(sym,a);
        }

};
class empty:virtual public Game{

};
class Play:public Pawn,public pawn,public Rook,public rook,public King,public king,public Queen,public queen,public bishop,public Bishop,public Knight,public knight,public empty{

public:
Pawn P[8];


pawn p[8];
Rook R[2];
rook r[2];
King K[1];
king k[1];
Queen Q[1];
queen q[1];
Knight Kn[2];
knight kn[2];
bishop b[2];
Bishop B[2];
empty e[64];


public:
    //SETTING THE MEMORY FOR FUTURE LOOK
    void memory()
    {
        int i,j,k,l,m;
        possibilities=new char****[128];
        for(i=0;i<16;i++)
        {
            possibilities[i]=new char***[128];

        }
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                possibilities[i][j]=new char**[128];
            }
        }
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                for(k=0;k<16;k++)
                {
                    possibilities[i][j][k]=new char*[128];
                }
            }
        }
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                for(k=0;k<16;k++)
                {
                    for(l=0;l<8;l++)
                    possibilities[i][j][k][l]=new char[8];
                }
            }
        }
        play=new Play*[8];
        for(i=0;i<8;i++)
        {
            play[i]=new Play[8];
        }

    }

    //SETTING UP THE BOARD
void setboard()
{


    for(int i=0;i<8;i++)
    {
        P[i]=i;
    }

    for(int i=0;i<8;i++)
    {
        p[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        r[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        R[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        kn[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        Kn[i]=i;
    }

    for(int i=0;i<1;i++)
    {
        K[i]=i;
    }


    for(int i=0;i<1;i++)
    {
        k[i]=i;
    }

    for(int i=0;i<1;i++)
    {
        Q[i]=i;
    }

    for(int i=0;i<1;i++)
    {
        q[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        b[i]=i;
    }

    for(int i=0;i<2;i++)
    {
        B[i]=i;
    }

    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            strcpy(board[i][j],"--");


        }
    }



    //placing White Pawns
    int j;

        for(j=0;j<8;j++)
        {
            strcpy(board[1][j],P[j].sym);
        }



    //placing black pawns
    for(j=0;j<8;j++)
        {
            strcpy(board[6][j],p[j].sym);
        }


    //placing white rooks
    strcpy(board[0][0],R[0].sym);
    strcpy(board[0][7],R[1].sym);

    //black rooks

        strcpy(board[7][0],r[0].sym);
    strcpy(board[7][7],r[1].sym);

    //white knights
        strcpy(board[0][1],Kn[0].sym);
    strcpy(board[0][6],Kn[1].sym);

    //white bishops

        strcpy(board[0][2],B[0].sym);
    strcpy(board[0][5],B[1].sym);

    //white king,queen

        strcpy(board[0][3],K[0].sym);
    strcpy(board[0][4],Q[0].sym);


    //black knights
        strcpy(board[7][1],kn[0].sym);
    strcpy(board[7][6],kn[1].sym);

    //black bishops

            strcpy(board[7][2],b[0].sym);
    strcpy(board[7][5],b[1].sym);

    //black king,queen
            strcpy(board[7][3],k[0].sym);
    strcpy(board[7][4],q[0].sym);


    for(int i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
        {
            cout<<board[i][j]<<" ";
        }
        cout<<endl;
        cout<<endl;
    }


        }
        char pc[2];
        char pi;
        int pos;
        void gplay()
        {
            for(int i=0;;i++)
            {
                PT:cout<<"Enter Your Move:";
                int fx,fy,tx,ty;
                cin>>fx>>fy>>tx>>ty;

                cout<<"Sending piece info!:"<<fx<<","<<fy<<endl;
                pieceinfo(fx,fy);
                if(pc[0]=='0')
                {
                    cout<<"There is no piece to be moved on this position"<<endl;
                    cout<<"Enter again"<<endl;
                    goto PT;

                }
                if(pc[0]=='p'||pc[0]=='r'||pc[0]=='q'||pc[0]=='n'||pc[0]=='b'||pc[0]=='k')
                {
                    cout<<"You cannot move your opponents piece!!"<<endl;
                    cout<<"Please try again!!"<<endl;
                    goto PT;
                }
                pi=pc[0];
                cout<<"The Model is:"<<pi<<endl;
                switch(pi)
                {
                    case 'P':goto Pawn;

                }
                Pawn:cout<<"The Piece is a Pawn!"<<endl;
                pos=(int)pc[1]-48;
                cout<<"The Position is:"<<pos;
                P[pos].valid(pos,fx,fy);
                if(P[pos].move(tx,ty)==0)
                goto PT;

                printb();
                break;



            }
}
void printb()
{
        for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            cout<<board[i][j]<<" ";
        }
        cout<<endl;
        cout<<endl;
    }
}

void pieceinfo(int x,int y)
{
    cout<<"Retrieving piece info!"<<endl;
    if(board[x][y]=="--")
    {
        pc[0]='0';
        pc[1]='0';
        return;
    }
    cout<<"The Piece is:"<<board[x][y][0]<<board[x][y][1]<<endl;
    pc[0]=board[x][y][0];
    pc[1]=board[x][y][1];
    cout<<"The Piece is:"<<pc[0]<<pc[1]<<endl;
    return;
}

};




int main()
{

    Play a;

    a.memory();
    cout<<"Welcome to Chess!"<<endl;
    a.setboard();
    cout<<"The BOard has been set!";
    a.gplay();
    cout<<"This is a chess program!!"<<endl;
    return 0;
}

有一个问题。 有一个超级基类Game,其中包含一个3D char数组board[8][8][3]Pawn继承了继承Game的基类board。类Play继承了类Pawn,从而间接继承了Gameboard。现在在Play的函数内部,我能够访问和打印board[1][1][0]等的值。现在在Play内我创建了一个Pawn的对象,并通过它调用其成员函数像

这样的对象
    Pawn P;
    P.valid();

以上陈述在Play内。 这会调用valid()中的成员函数Pawn。 现在在valid()内。当我尝试使用board[1][1][0]或尝试打印时,它会打印一个空白区域。

那么为什么这样做呢?这是什么问题或我的错误? 此外,我无法粘贴整个代码,因为它非常大。 此外,这是代码的一部分..

输入1 1 2 1

你应该看到它在给出位置之后还应该打印board[1][1][0]的值。

2 个答案:

答案 0 :(得分:1)

Pawn p;

这将创建Pawn类的新实例,并具有自己的Game超类。

即使Pawn类的这个实例被实例化为Play类的成员,它也是一个独立的独立对象。 Play对象的实例包含其自己的,独立的Game超类的独立实例。

您的问题是,您正在访问boardPawn成员的board成员,并且您希望访问Play {{1}}成员实例。两个对象都是独立的,它们是独立的类。仅仅因为两个类都继承自同一个超类,并且一个类是另一个类的成员,它并不意味着它们的实例将共享同一个超类。这不是虚拟继承的原因。

答案 1 :(得分:0)

如果我已正确理解您的问题并根据您的代码判断,您需要更改此信息:

class Game {
public:
  char board[8][8][3];
};

到这个

class Game {
public:
  static char board[8][8][3];
};

以便每个对象共享同一块板。否则,每个对象都有自己的板,这可能不是你想要完成的行为。通过按照建议更改代码,board现在将是一般类而不是单独的对象。因此,在提及board时,您不应该使用this->board等,而是单独使用boardGame::board

This可能会帮助您了解正在完成的工作。