Dynamic Binding

Through dynamic binding, we can use the same code to process objects of either type Quote or Bulk_quote interchangeably. For example, the following function prints the total price for purchasing the given number of copies of a given book:

// calculate and print the price for the given number of copies, applying any discountsdouble print_total(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 << endl;     return ret;}

This function is pretty simple—it ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.