程序代码:
#include<iostream>
class Box
{
public:
//构造
Box()
{
Fruitname = "";
Weight = 0;
Color = "";
}
//析构
~Box()
{
}
//添加
void AddFruit(const string sFruitname, double dWeight, string sColor);
//删除
void DeleteFruit(const string sFruitname);
//显示
void Show();
private:
string Fruitname; // 名字
double Weight; // 重量
string Color; // 颜色
string szFruitname[1024];
string szColor[1024];
double dbWeight[512];
};
void Box::AddFruit(const string sFruitname, double dWeight, string sColor)
{
szFruitname[0] = sFruitname;
szColor[0] = sColor;
dbWeight[0] = dWeight;
}
void Box::DeleteFruit( string sFruitname)
{
if (szFruitname[0] == sFruitname)
{
szFruitname[0] = "";
szColor[0] = "";
dbWeight[0] = 0;
}
}
void Box::Show()
{
if (dbWeight[0] == 0 )
{
cout<<"无数据"<<endl;
return;
}
cout<<"水果名:"<<szFruitname[0]<<" 重量:"<<dbWeight[0]<<" 颜色:"<<szColor[0]<<endl;
}
int main()
{
Box box;
string name,color,name1;
double weight;
cout<<"input param:"<<endl;
cin>>name;
cin>>weight;
cin>>color;
box.AddFruit(name,weight,color);
box.Show();
cin>>name1;
box.DeleteFruit(name1);
box.Show();
return 0;
}