![]() |
#2
倾听心跳2020-03-24 17:55
![]() #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; } |
利用c++编写程序实现:
有一个水果箱(Box),箱子里装有水果(Fruit):苹果(Apple),梨(Pear),橘子(Orange)。每种水果都有不同的重量和颜色。可以向水果箱(Box)里添加水果(AddFruit),也可以去除水果(DeleteFruit),还可以显示水果的重量和颜色,编写代码实现上述功能。要求编写的类除了包含上面基本功能要求外还要添加构造函数,析构函数,来体现对象生成和销毁顺序。
好难啊,我不会编写这个,求大神写出来。我目前学到数组。头大,没思路