
作业社区
探索学习新天地,共享知识资源!
胡汉三66 的学生作业:
Chess.hpp #ifndef _CHESS_H_ #define _CHESS_H_ #include using namespace std; class Chess{ // 抽象类 public: Chess(const string &color,int x,int y):color(color),x(x),y(y){} // 构造函数 int getX(void) const{ return x; } // 返回 坐标点 x int getY(void) const{ return y; } // 返回 坐标点 y string getColor(void) const{ return color;} // 返回 棋子颜色 virtual void show(void) const = 0; // 纯虚函数 private: int x; // 坐标 x int y; // 坐标 y string color; // 棋子颜色 }; #endif BlackChess.hpp #ifndef _BLACKCHESS_H_ #define _BLACKCHESS_H_ #include #include "Chess.hpp" class BlackChess:public Chess { // 继承类 Chess 的公有成员 public: BlackChess(int x,int y):Chess("black",x,y){} // 构造函数 void show(void) const{ fprintf(stderr,"\033[%d;%dH\033[44;31m[☻ ]\033[0m",getY(),getX() - 1); // 棋子坐标 // 蓝底红字 棋子[☻ ] // 关闭所有属性 fprintf(stderr,"\033[%d;%dH\n",getY(),getX()); // 定义在棋子中心左侧 // 每个棋子宽度占3个符号 } }; #endif WhiteChess.hpp #ifndef _WHITECHESS_H_ #define _WHITECHESS_H_ #include #include "Chess.hpp" class WhiteChess:public Chess { // 继承类 Chess 的公有成员 public: WhiteChess(int x,int y):Chess("white",x,y){} // 构造函数 void show(void) const{ fprintf(stderr,"\033[%d;%dH\033[43;37m[☻ ]\033[0m",getY(),getX() - 1); // 棋子坐标 // 黄底白字 棋子 : [☻ ] // 关闭所有属性 fprintf(stderr,"\033[%d;%dH\n",getY(),getX()); // 定义在棋子中心左侧 // 每个棋子宽度占3个符号 } }; #endif main.cpp #include "BlackChess.hpp" #include "WhiteChess.hpp" int main(int argc, const char *argv[]){ WhiteChess w(5,6); BlackChess b(9,6); w.show(); b.show(); return 0; } 【图片】




