胡漢三66 的學生作業(yè):
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){} // 構造函數(shù)
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; // 純虛函數(shù)
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){} // 構造函數(shù)
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){} // 構造函數(shù)
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;
}
【圖片】