|
发表于 2014-6-23 16:53:04
|
显示全部楼层
- #include <string>
- #include <iostream>
- using namespace std;
- class Goods {
- public:
- Goods( string name, double price, int amount )
- :Name(name),Price(price),Amount(amount){}
- void Sale( int number );
- void Add( int number ) { Amount += number; }
- ostream& ShowMe( ostream& out ) const;
- private:
- string Name;
- double Price;
- int Amount;
- };
- void Goods::Sale( int number ) {
- if( number>Amount ) {
- cout<<"仅有"<<Amount<<"件商品"<<endl;
- return;
- }
- Amount -= number;
- cout<<"应付钱:"<<number*Price<<endl;
- }
- ostream& Goods::ShowMe( ostream& out ) const{
- out<<"商品名称:"<<Name<<"\t单价:"<<Price<<"库存:"<<Amount;
- return out;
- }
- ostream& operator<<( ostream& out, const Goods& goods ) {
- return goods.ShowMe(out);
- }
- int main() {
- Goods goods("程序作业", 20.5, 3);
- cout<<goods<<endl;
- goods.Sale(100);
- goods.Sale(2);
- cout<<goods<<endl;
- goods.Add(10);
- cout<<goods<<endl;
- }
复制代码 |
|