标签:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Item_base 7 { 8 public: 9 Item_base(const string &is,double p):isbn(is),price(p){} 10 string book() 11 { 12 return isbn; 13 } 14 virtual double calMoney(int c) 15 { 16 return c * price; 17 } 18 private: 19 string isbn; 20 protected: 21 double price; 22 }; 23 24 class Bulk_item : public Item_base 25 { 26 public: 27 Bulk_item::Bulk_item(const string &is,double p,int c,double dis): 28 Item_base(is,p),min_py(c),discocunt(dis){} 29 double calMoney(int c) 30 { 31 cout<<endl<<"子类"<<endl; 32 if(c > min_py) 33 { 34 return c * price * discocunt; 35 } 36 else 37 { 38 return c * price; 39 } 40 } 41 void test() 42 { 43 cout<<endl<<"子类测试!!"<<endl; 44 } 45 private: 46 int min_py; 47 double discocunt; 48 }; 49 50 51 52 int main() 53 { 54 string itemIsbn = "x-123-1234-x"; 55 Item_base item(itemIsbn,10.0); 56 cout<<item.calMoney(100)<<endl; 57 58 Bulk_item item2("123-456-x",10.0,30,0.8); 59 cout<<item2.calMoney(100)<<endl; 60 item2.test(); 61 62 Item_base *item3 = new Bulk_item("123-456-789-x",10.0,10,0.8); 63 cout<<item3->calMoney(20)<<endl; 64 //不能调用子类的test方法 65 return 0; 66 }
后续更新中....
标签:
原文地址:http://www.cnblogs.com/calence/p/5880013.html