没有错误,但运行时出现问题?求看
#include
using namespace std;
class fruit{
private:
int kind;
int weight;
public:
virtual void loseWeight(){};
virtual int getWeight(){ return weight; };
virtual int getKind(){ return kind; };
};
class apple :public fruit{
private:
int kind;
int weight;
public:
apple(){ kind = 1; weight = 50; };
void loseWeight(){
if ((weight-4) >= 30)
weight -= 4;
else
weight = 30;
}
int getWeight(){
return weight;
}
int getKind(){ return kind; }
};
class orange :public fruit{
private:
int kind;
int weight;
public:
orange(){ kind = 2; weight = 30; }
void loseWeight(){
if ((weight - 4) >= 18)
weight -= 4;
else
weight = 18;
}
int getWeight(){
return weight;
}
int getKind(){ return kind; }
};
class Box{
private:
fruit* f[8];
int a, o,num;
public:
Box(){
a = 0;
o = 0;
num = 0;
for (int j = 0; j < 8; j++)
f[j] = NULL;
}
void put(fruit* k){
if (num < 8){
f[num] = k;
num++;
if (k->getKind() == 1)a++;
if (k->getKind() == 2) o++;
}
else cout << "已满" << endl;
}
int getApple(){ return a; } //输出盒子中苹果的数量;
int getOrange(){ return o; } //输出盒子中桔子的数量;
int total(){ //当前水果总重量
int w=0;
for (int i = 0; i < 8; i++){
if (f[i] != NULL){
w += f[i]->getWeight();
}
}
return w;
}
int culWeight(){
int w=0;
for (int i = 0; i < 8; i++){
if (f[i] != NULL){
f[i]->loseWeight();
w += f[i]->getWeight();
}
}
return w;
}
int lost(){ //一天来盒子中水果减轻的总重量
return (total() - culWeight());
}
};
void main(){
Box *box=new Box();
fruit *a = new apple();
orange *o=NULL;
cout << a->getKind();
if (a->getKind() == 1) cout << "h";
box->put(a);
box->put(o);
cout << box->getApple() << endl;
cout << box->getOrange() << endl;
box->total();
box->lost();
system("pause");
}