#include #include 〈 vector #include 〈 string using namespace std; class PaymentMethod { public: virtual void pay( int cents ) = 0; } ; /* Cash、DebitCard和Item实现略,Item中getPrice()获取当前物品对象的价格 */ class Card : public PaymentMethod { private: string name, num; public: Card( string name, string num ) { this->name = name; this->num = num; } string toString() { return this->getType() + " card[name = " + name + ",num = " + num + "]" ;) } void pay( int cents ) { cout << "Payed " < () < this->executeTransaction( cents ); } protected: virtual string getType() = 0; virtual void (1) = 0 : }; class CreditCard(2) { public: CreditCard( stringname, stringnum ) (3) { } protected: string getType() { return " CREDIT " ; } void executeTransaction( int cents ) { cout < () << " Card." << end1; } }; class Bill { /* 包含所有购买商品的账单 */ private: vector< Item*> items; /* 包含物品的 vector */ public: void add( Item* item ) { items.push_back( item ); } int getTotalPrice() /*计算所有item的总价格,代码略*/ { } void pay( PaymentMethod* paymentMethod ) /* 用指定的支付方式完成支付 */ { (4)( getTotalPrice() ); } }; class PaymentSystem { public: void pay() { Bill * bill = new Bill(); Item * item1 = new Item "1234" 10); Item* item2 = new Item( "5678", 40 ); bill->add( item1 ); bill->add( item2 ); /* 将物品添加到账单中 */ (5)( new CreditCard( "LI SI", "98765432101" ) ); /* 信用卡支付 */ } } ; Intmain() { (6) = new PaymentSystem(); payment->pay() ; return(0); }