📄 vc0806.cpp
字号:
//Example 8.6: 定义一个棋子类
#include <iostream.h>
#include <string.h>
// 棋子类
class Stone
{
protected:
int m_nColor; // 颜色
int m_nCol; // 路
int m_nRow; // 行
bool m_bShow; // 是否显示
bool m_Selected; // 是否被选择
public:
Stone(int color, int col, int row);
void MoveTo(int col, int row) { m_nCol=col, m_nRow=row; }
void KillIt() { m_bShow = false; }
void Select() { m_Selected = !m_Selected; }
};
Stone::Stone(int color, int col, int row) // 棋子类的构造函数
{
m_nColor = color;
m_bShow = true;
m_Selected = false;
m_nCol = col;
m_nRow = row;
}
// 中国象棋棋子类
class CStone : public Stone
{
char m_strType[10]; // 棋子类型
int m_nR; // 棋子半径
public:
CStone(int color, int col, int row, char *type); // 构造函数
void Show(); // 显示信息
};
CStone::CStone(int color, int col, int row, char *type):Stone(color, col, row)
{
strcpy(m_strType, type);
m_nR = 23;
}
void CStone::Show()
{
cout<<"-- 这是一个象棋棋子 --"<<endl;
cout<<" 棋子类型:"<<m_strType<<endl;
if(m_nColor==0)
{ cout<<" 棋子颜色:红色"<<endl; }
else { cout<<" 棋子颜色:黑色"<<endl; }
cout<<" 棋子位置:("<<m_nCol<<","<<m_nRow<<")"<<endl;
if(m_bShow==true)
{ cout<<" 是否显示:是"<<endl; }
else { cout<<" 是否显示:否"<<endl; }
if(m_bShow==true && m_Selected==true)
{ cout<<" 是否被选:是"<<endl; }
else { cout<<" 是否被选:否"<<endl; }
cout<<" 棋子半径:"<<m_nR<<endl<<endl;
}
// 测试主函数
void main()
{
CStone c1(1,3,6,"炮"); //建立一个棋子对象
c1.Show(); //显示棋子信息
c1.Select(); //选中棋子
c1.MoveTo(3,2); //移动棋子
c1.Show(); //显示棋子信息
c1.KillIt(); //棋子被吃掉
c1.Show(); //显示棋子信息
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -