请问这个代码为啥不行呢。。。。。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
static double pi = 3.1416;
class shape{
public: virtual double qiuc()=0;
};
class circle: public shape
{
public:
circle(double a=1) { r = a; };
virtual double qiuc(){ return r*2*pi; }
private:double r;
};
class rectangle:public shape
{
public:
rectangle(double a, double b) { longth = a; width = b; };
virtual double qiuc() { return (longth + width) * 2; }
private:double longth, width;
};
class triangle :public shape {
public:triangle(double a,double b, double c)
{ x = a; y = b; z = c;
}
virtual double qiuc() { return x + y + z; }private:double x, y, z;
};
double total(vector<shape*>&a) {
double sum=0;
for(int i=0;i<a.size();i++)
{
sum +=a[i]->qiuc();
}return sum;
}
int main() {
vector<shape*>a;
string b="",c="circle",d="rectangle",e="triangle";
do {
cin >> b;
if (b == c) {
double r;
cin >> r;
circle yuan(r);
a.push_back(&yuan);
}
else if (b == d)
{
double lon, wid;
cin >> lon >> wid;
rectangle changfang(lon, wid);
a.push_back(&changfang);
}
else if (b == e)
{
double a1, a2, a3;
cin >> a1 >> a2 >> a3;
triangle san(a1, a2, a3);
a.push_back(&san);
}
else {}
} while (b == c || b == d || b == e);
cout << total(a)<<endl;
return 0;
}
就是创建两个以上的长方体时会出现问题。。。