C++练习
P228 4.12
Derive classes Fruit,Dairy and meat, appropriate for a grocery chain,from class Product of Programming 3.23 ,also derive classes from each of Fruit,Dairy and meat.
Include any reasonable data members and methods .Assume that evey product has supply and demand data memberswhose values are one of the qualitative values low,medium,high.Incluse a method that computes a product's price change based on its values of supply and demand.
#include<iostream> #include<string> using namespace std; class Product{ public: int supply,demand; double p;
Product(){}; Product(string na):name(na){ }; Product(string na,double pri):name(na),price( pri){ }; Product(string na,double pri,int da):name( na),price( pri),date(da){ }; void Input(); void Output(); void setSupply(); void setDemand(); void setPrice(); private: string name; double price; int date; string manuf; }; class Fruit: public Product{ }; class Dairy:public Product{ }; class Meat:public Product{ }; class Cuke:public Fruit{ public: void pricechange() { if(supply>demand) { p=p*0.95; cout<<"output the changed price:"<<endl; cout << p<<endl; } if(supply=demand) { p=p*1.0; cout<<"output the changed price:"<<endl; cout<< p<<endl; } if(supply<demand) { p=p*2.0; cout<<"output the changed price:"<<endl; cout<<p<<endl; } } }; class Butter:public Dairy{ public: void pricechange() { if(supply>demand) { p=p*0.95; cout<<"output the changed price:"<<endl; cout << p<<endl; } if(supply=demand) { p=p*1.0; cout<<"output the changed price:"<<endl; cout<< p<<endl; } if(supply<demand) { p=p*2.0; cout<<"output the changed price:"<<endl; cout<<p<<endl; } } }; class Milk:public Dairy{ public: void pricechange() { if(supply>demand) { p=p*0.95; cout<<"output the changed price:"<<endl; cout << p<<endl; } if(supply=demand) { p=p*1.0; cout<<"output the changed price:"<<endl; cout<< p<<endl; } if(supply<demand) { p=p*2.0; cout<<"output the changed price:"<<endl; cout<<p<<endl; } }//后面没有分号 }; class Beef:public Meat{ public: void pricechange() { if(supply>demand) { p=p*0.95; cout<<"output the changed price:"<<endl; cout << p<<endl; } if(supply==demand) { p=p*1.0; cout<<"output the changed price:"<<endl; cout<< p<<endl; } if(supply<demand) { p=p*2.0; cout<<"output the changed price:"<<endl; cout<<p<<endl; } }; }; void Product :: Input() { cout << "input name:"<< endl; getline(cin,name); cout << "input price:"<< endl; cin >> price; cout << "input date:" << endl; cin >> date; } void Product :: Output() { string st; cout<< "input the information you want:"<< endl; cout << "name,price,date "<<endl; cin >> st; if(st == "name" ) cout << name <<endl; if(st =="price") cout << price << endl; if(st == "date") cout << date << endl; } void Product::setSupply() { cout<<"input the supply:"<<endl; cout<<"low=-1,medium=0,high=1"<<endl; cin >> supply; } void Product::setDemand() { cout<<"input the demand:"<<endl; cout<<"low=-1,medium=0,high=1"<<endl; cin >> demand; } void Product::setPrice() { cout<<"input the price"<<endl; // double p;//p已经在基类定义了 此处再定义p 作用域只是在这个函数里面,输入的p不能代表基类的p 会使上面的p没有值出现#%#¥%@#%¥ cin >> p; } int main() { Product p1; p1.Input(); p1.Output(); Cuke p2; p2.setDemand(); p2.setSupply(); p2.setPrice(); p2.pricechange(); return 0; }
终于改好了这个程序。果然深夜写代码感觉很棒、
CS大坑呢,还是舍不得弃,虽然要学接电线什么。。。真的让人头很痛啊!