不知道為什麼程式run完後會因錯誤而關閉,請幫我改一下~謝了!!
#include
using namespace std;
class Shape
{
public:
virtual double compute_area() = 0;
virtual void draw() = 0;
};
class Circle : public Shape
{
private:
double x,y;
double radius;
public:
Circle(double x,double y,double radius);
double compute_area();
void draw();
};
class Rectangle : public Shape
{
private:
double x1,y1;
double x2,y2;
public:
Rectangle(double x1,double y1,double x2,double y2);
double compute_area();
void draw();
};
int main()
{
int i;
double a,b,c,d,e,f,g;
Shape *p[2];
cout << \"請輸入2個值作為圓心:\";
cin >> a >> b;
cout << \"請輸入1個值作為半徑:\";
cin >> c;
p[0] = new Circle(a,b,c);
cout << \"\\n請輸入2個值作為Rectangle其中一點:\";
cin >> d >> e;
cout << \"請再輸入2個值作為Rectangle另一點:\";
cin >> f >> g;
p[1] = new Rectangle(d,e,f,g);
for(i=0;i<3;i++)
{
cout << \"Area of shape[\" << i << \"] = \" << p[i]->compute_area() << endl;
p[i]->draw();
}
delete p[0];
delete p[1];
cin.get();
cin.get();
system(\"PAUSE\");
return 0;
}
Circle::Circle(double a, double b, double r)
{
x=a;
y=b;
radius=r;
}
double Circle::compute_area()
{
return (3.14 * radius * radius);
}
void Circle::draw()
{
cout << \"Draw : Circle of radius \" << radius
<< \" at (\" << x << \", \" << y << \")\" << endl;
}
Rectangle::Rectangle(double a, double b, double c, double d)
{
x1=a;
y1=b;
x2=c;
y2=d;
}
double Rectangle::compute_area()
{
double temp_1,temp_2;
if(x1>x2){
temp_1=x2;
x2=x1;
x1=temp_1;}
if(y1>y2){
temp_2=y2;
y2=y1;
y1=temp_2;}
return (x2-x1)*(y2-y1);
}
void Rectangle::draw()
{
cout << \"Draw: Rectangle with corners (\" << x1 << \", \"
<< x2 << \") (\" << x2 << \", \" << y2 << \")\" << endl;
}
2006-05-22 03:08:30 · 1 個解答 · 發問者 金魚 2 in 電腦與網際網路 ➔ 程式設計
沒有仔細看完,不過你只有p[0]和p[1],但是
for(i=0;i<3;i++)
{
cout << "Area of shape[" << i << "] = " << p[i]->compute_area() << endl;
p[i]->draw();
}
會跑到p[2]去,這樣會有問題,應該改i<2,這樣才是0,1
2006-05-22 05:56:40 · answer #1 · answered by chan 5 · 0⤊ 0⤋