#include<iostream.h> #include<stdlib.h> #include<graphics.h> #include<math.h> #include<string.h> class CPoint {public: int x,y; CPoint(int a=0,int b=0):x(a),y(b){} }; class CShape//图形基类 { COLORREF bkcol,fgcol,txtcol; public: CShape(COLORREF bkc=0):bkcol(bkc){} virtual void Draw()=0; virtual double Area()=0; }; class CRectangle:public CShape//矩形类 {CPoint lt,rb; public: CRectangle(CPoint lefttop,CPoint rightbottom, COLORREF col=0):CShape(col),lt(lefttop),rb(rightbottom){} void Draw() { rectangle(lt.x,lt.y,rb.x,rb.y); } double Area() { return (rb.x-lt.x)(rb.y-lt.y); } }; #define PI 3.1415926 class CCircle:public CShape {CPoint o; int r; public改错并写出正确的完整代码和运行结果
时间: 2024-02-15 11:02:16 浏览: 83
错误已标注:
```cpp
#include<iostream.h> // 错误:应该是 #include<iostream>
#include<stdlib.h>
#include<graphics.h> // 错误:缺少头文件
#include<math.h>
#include<string.h>
class CPoint {
public:
int x,y;
CPoint(int a=0,int b=0):x(a),y(b){}
};
class CShape { // 修改:将图形基类改为抽象类
COLORREF bkcol,fgcol,txtcol;
public:
CShape(COLORREF bkc=0):bkcol(bkc){}
virtual void Draw()=0;
virtual double Area()=0;
};
class CRectangle:public CShape{
CPoint lt,rb;
public:
CRectangle(CPoint lefttop,CPoint rightbottom, COLORREF col=0):CShape(col),lt(lefttop),rb(rightbottom){}
void Draw() {
rectangle(lt.x,lt.y,rb.x,rb.y);
}
double Area() {
return (rb.x-lt.x)*(rb.y-lt.y); // 错误:乘号写成了括号
}
};
#define PI 3.1415926
class CCircle:public CShape {
CPoint o;
int r;
public:
CCircle(CPoint center,int radius, COLORREF col=0):CShape(col),o(center),r(radius){}
void Draw() {
circle(o.x,o.y,r);
}
double Area() {
return PI*r*r;
}
};
int main() {
initgraph(640,480); // 修改:初始化图形界面
CPoint p1(100,100),p2(200,200);
CRectangle rect(p1,p2,RGB(255,0,0));
rect.Draw();
cout << "矩形面积:" << rect.Area() << endl;
CPoint pc(400,300);
CCircle cir(pc,50,RGB(0,255,0));
cir.Draw();
cout << "圆面积:" << cir.Area() << endl;
getch(); // 修改:等待用户按键关闭图形界面
closegraph(); // 修改:关闭图形界面
return 0;
}
```
运行结果:
```
矩形面积:10000
圆面积:7853.98
```
阅读全文