//文件Graphics.h #include<iostream> using namespace std; class Graphics{ public: Graphics(int n); // 用参数初始化数据成员size virtual void construction()=0; // 生成大小为size的图形 protected: int size; // 图形的大小 }; class Triangle:public Graphics{ // 三角形 public: Triangle(int n); void construction(); }; class Rectangle:public Graphics{ // 矩形 public: Rectangle(int n); void construction(); }; void writeToFile(const string); // 不用考虑此语句的作用 //文件Graphics.cpp #include"Graphics.h" #pragma warning(disable:4996) Graphics::Graphics(int n):size(n) { } Triangle::Triangle(int n):Graphics(n) { } void Triangle::construction() { cout<<"三角形为:"<<endl; int i,j; //**********begin********** //**********end********** } Rectangle::Rectangle(int n):Graphics(n) { } void Rectangle::construction() { cout<<"矩形为:"<<endl; int i,j; for(i=0;i<size;i++){ for(j=0;j<size;j++) if(i==0||i==size-1||j==0||j==size-1) // 输出边界的* cout<<'*'<<' '; // 输出内部空格 else cout<<' '<<' '; cout<<endl; } } //文件main.cpp #include"Graphics.h" void fun(Graphics * p) { p->construction(); } int main() { Triangle t(9); fun(&t); Rectangle r(9); fun(&r); system("pause"); return 0; }
时间: 2024-04-27 14:25:42 浏览: 27
这段代码实现了一个基类 `Graphics`,和两个派生类 `Triangle` 和 `Rectangle`,分别用于构造三角形和矩形。其中,`Graphics` 是一个抽象类,具有一个纯虚函数 `construction()`,用于输出图形。`Triangle` 和 `Rectangle` 分别实现了 `construction()` 函数,按照题目要求输出对应的图形。
在 `main` 函数中,定义了一个函数 `fun`,参数为一个指向 `Graphics` 类对象的指针。函数 `fun` 调用了参数指针的 `construction()` 函数,输出图形。然后,分别创建了 `Triangle` 和 `Rectangle` 对象,并调用了 `fun` 函数,输出相应的图形。
最后,程序调用了 `system("pause")` 函数,等待用户按下任意键,以防止控制台窗口一闪而过。
相关问题
#include <graphics.h> #include<conio.h> #include<cmath> #include<iostream> using namespace std; class SineGraph { protected: double A, w, b; int m, n;//m,n表示函数显示范围 int color, step; public: SineGraph(double i, double j, double k, int x, int y, int getcolor = WHITE, int getstep = 1) { A = i; w = j; b = k; m = x; n = y; color = getcolor; step = getstep; } void draw() { for (int x = m; x < n; x += step) { cleardevice(); line(x, A * sin(w * x + b) + 200), x + 1, A * sin(w * (x + 1) + b) + 200); Sleep(30); } } void update() { b += 0.1; if (b > 100) b = -100; } }; int main() { initgraph(640, 480,1); // 设置窗口大小为 640x480 SineGraph sg(100,0.05,10,200,500,RED); for (int i = 0; i < 1000; i++) { sg.update(); sg.draw(); } sg.draw(); // 绘制正弦图形 _getch(); // 等待按键退出 closegraph(); // 关闭图形窗口 return 0; } 代码无法运行,找出原因
这段代码无法运行的原因是缺少了图形库的支持。在C++中,`graphics.h` 是一种图形库,用于绘制图形和界面。但是,这个库并不是标准C++库的一部分,而是特定于某些编译器和操作系统的库。
根据代码中使用的 `graphics.h` 和 `initgraph()` 函数,可以判断你是使用的是 Turbo C++ 编译器和 DOS 环境。然而,在现代操作系统中,Turbo C++ 不再被广泛支持,并且不再适用于现代的图形编程。
如果你想继续使用图形库进行编程,可以考虑使用更现代的图形库,如 OpenGL 或者 SDL,并选择适合你所使用的编译器和操作系统的版本。或者,你也可以使用其他支持图形绘制的编程语言和库来实现类似的功能。
Man.h #pragma once #include "Chess.h" class Man { public: void init(Chess* chess); void go(); private: Chess* chess; }; AI.h #pragma once #include "Chess.h" class AI { public: void init(Chess* chess); void go(); private: Chess* chess; vector<vector<int>>scoreMap; private: void calculateScore(); ChessPos think();//private权限 }; Chess.h #pragma once #include<graphics.h> #include<vector> using namespace std; typedef enum { CHESS_WHITE = -1, // 白方 CHESS_BLACK = 1 // 黑方 } chess_kind_t; struct ChessPos { int row; int col; ChessPos(int r = 0, int c = 0) :row(r), col(c) {} }; class Chess { public: Chess(int gradeSize, int marginX, int marginY, float chessSize); // 棋盘的初始化:加载棋盘的图片资源,初始化棋盘的相关数据 void init(); // 判断在指定坐标(x,y)位置,是否是有效点击 // 如果是有效点击,把有效点击的位置(行,列)保存在参数pos中 bool clickBoard(int x, int y, ChessPos* pos); // 在棋盘的指定位置(pos), 落子(kind) void chessDown(ChessPos* pos, chess_kind_t kind); // 获取棋盘的大小(13线、15线、19线) int getGradeSize(); // 获取指定位置是黑棋,还是白棋,还是空白 int getChessData(ChessPos* pos); int getChessData(int row, int col); // 判断棋局是否结束 bool checkOver(); //bool checkWin(); private: // 棋盘尺寸 int gradeSize; float margin_x;//49; int margin_y;// 49; float chessSize; //棋子大小(棋盘方格大小) IMAGE chessBlackImg; IMAGE chessWhiteImg; // 存储当前游戏棋盘和棋子的情况,空白为0,黑子1,白子-1 vector<vector<int>> chessMap; // 标示下棋方, true:黑棋方 false: AI 白棋方(AI方) bool playerFlag; void updateGameMap(ChessPos* pos); bool checkWin();//如果胜负已分,就返回true,否则返回假 ChessPos lastPos;//最近落子点的位置 }; //对棋盘进行数据初始化 ChessGame.h #pragma once #include "AI.h" #include "Chess.h" #include "Man.h" #include<iostream> #include<vector> using namespace std; class ChessGame { private: Man* man; AI* ai; Chess* chess; public: ChessGame(Man *man, AI *ai, Chess *chess); void play(); };中的局部变量及解释
ChessGame.h文件中的局部变量有:Man* man, AI* ai, Chess* chess。这三个变量分别代表人类玩家、AI玩家和棋盘对象。在ChessGame类的构造函数中,需要传入这三个对象的指针,以便在play()函数中调用它们的方法来进行下棋。这些变量都是private权限的,只能在类的内部访问。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)