double print_total(std::ostream& os, const Quote& item, size_t n) { double ret = item.net_price(n); os << "ISBN:" << item.isbn() << "# sold: " << n << "total due: " <<ret << std::endl; return ret; }
// for test int main(int argc, char **argv) { Quote q("asd", 10.5); Bulk_Quote b_q("f**k", 10.5, 20, 0.2); Quote *p; p = &q; cout << "20本asd的总价格为:" << p->net_price(20) << endl; p->debug(); p = &b_q; cout << "20本f**k的总价格为:" << p->net_price(20) << endl; p->debug(); system("pause"); return 0; }
int main() { Bulk_quote bulk_quote("bulk_quote_1", 10.10, 10, 0.5); // The pointer is of static type Quote, but it's dynamic type is Bulk Quote // Because of polymorphism the Bulk Quote implementation of the net_price() // method gets called. Quote *quote_pointer = &bulk_quote; quote_pointer->net_price(5); // The reference is of static type Quote, but it's dynamic type is Bulk Quote // Like with the pointer, the Bulk Quote implementation of the net_price() // method gets called. Quote "e_reference = bulk_quote; quote_reference.net_price(5); // The static type of this variable is Quote. Here the Bulk Quote object // gets upcasted. The Quote part of bulk_quote gets copied into quote, but // the rest is not handled. Because of the cast the Quote implementation of // the net_price() method gets called. Quote quote = bulk_quote; quote.net_price(5); return 0; }
double print_total(ostream& os, const Quote& item, size_t number){ double ret = item.net_price(number); os << "ISBN: " << item.isbn() << " # sold: " << number << " total due: " << ret << endl; return ret; }
double print_total(std::ostream& out, const Quote& item, std::size_t n) { // depending on the type of the object bound to the item parameter // call corresponding functuon either Quote::net_price or Bulk_quote::net_price double ret = item.net_price(n); out << "IBSN: " << item.isbn() << " # sold: " << n << " total due: " << ret << std::endl; return ret; }
// non-member double print_total(std::ostream &os, const Quote &item, size_t n) { // depending on the type of the object bound to the item parameter // calls either Quote::net_price or Bulk_quote::net_price double ret = item.net_price(n); os << "ISBN: " << item.isbn() // calls Quote::isbn << " # sold: " << n << " total due: " << ret << std::endl; return ret; }
double print_total(ostream& os, Quote const& item, size_t sold) { double net_price = item.net_price(sold); os << "isbn: " << item.isbn() << ", sold: " << sold << ", total due: " << net_price << endl; return net_price; }
int main() { Quote base("000111", 50); print_total(cout, base, 10); //calls Quote::net_price Bulk_quote bulk("000111", 50, 5, 0.19); print_total(cout, bulk, 10); //calls Bulk_quote::net_price Quote *baseP = &bulk; cout << baseP->net_price(10) << endl; //calls Bulk_quote::net_price cout << baseP->Quote::net_price(10) << endl; //calls Quote::net_price system("pause"); }
void printTotal(ostream& os, const Quote& book, const size_t bookNum) { os << book.isbn() <<":" <<book.net_price(bookNum); }