- #include <iostream>
- #include<string>
- using namespace std;
- class BookItem
- {
- private:
- string bookName;
- size_t cnt;
- public:
- BookItem(const string&s,size_t c,double p):
- bookName(s),cnt(c),price(p)
- {}
- ~BookItem(){}
- protected:
- double price;
- public:
- double bookPrice()
- {
- return this->price;
- }
- string getBookName()
- {
- return this->bookName;
- }
- size_t getBookCount()
- {
- return this->cnt;
- }
- virtual double money()
- {
- return cnt*price;
- }
- virtual void costMoney()
- {
- cout<<money()<<endl;
- }
- };
- class BookBatchItem:public BookItem
- {
- private:
- string bookName;
- size_t cnt;
- public:
- BookBatchItem(const string&s,size_t c,double p,double discountRate):
- BookItem(s,c,p),cnt(c),discount(discountRate)
- {}
- ~BookBatchItem(){}
- private:
- double discount;
- public:
- double money()
- {
- if(cnt>=10)
- return cnt*price*(1.0-discount);
- else
- return cnt*price;
- }
- void costMoney()
- {
- cout<<money()<<endl;
- // cout<<cnt<<endl;
- // cout<<price<<endl;
- // cout<<discount<<endl;
- // cout<<"..."<<endl;
- }
- };
- int main()
- {
- BookItem b1("Uncle Tom's house",11,12.5);
- b1.costMoney();
- BookBatchItem b2("Gone with wind",11,12.5,0.12);
- b2.costMoney();
- BookItem* pb=&b1;
- pb->costMoney();
- pb=&b2;
- pb->costMoney();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2811201514106.html
来源: http://www.codesnippet.cn/detail/2811201514106.html