标签:
1 class Sales_Item 2 { 3 //注意:形参为引用时,实参和形参才指向共同的对象,否则修改形参的数据不影响实参。 4 friend ostream &operator<< (ostream &out,const Sales_Item &item); 5 friend istream &operator >> (istream &in,Sales_Item & item); 6 public : 7 Sales_Item() 8 { 9 count = 0; 10 price = 0; 11 } 12 Sales_Item(string &book,int number,double unit) 13 { 14 isbn = book; 15 count = number; 16 price = unit * number; 17 } 18 19 private: 20 string isbn; 21 int count; 22 double price; 23 }; 24 25 ostream &operator<< (ostream &out,const Sales_Item &item) 26 { 27 out<<item.isbn<<" , "<<item.count<<" , "<<item.price<<endl; 28 return out; 29 } 30 31 istream &operator>> (istream &in,Sales_Item &item) 32 { 33 double price; 34 in >> item.isbn >> item.count >> price; 35 if(in) 36 { 37 item.price = price * item.count; 38 } 39 else 40 { 41 item = Sales_Item(); 42 } 43 44 return in; 45 } 46 int main() 47 { 48 string s("0-123-12345-X"); 49 Sales_Item item(s,10,12.00); 50 cout<<item<<endl; 51 52 cin>>item; 53 cout<<item<<endl; 54 cout<<"ok"<<endl; 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/calence/p/5858398.html