Classes with Pure Virtuals Are Abstract Base Classes
A class containing (or inheriting without overridding) a pure virtual function is an abstract base class. An abstract base class defines an interface for subsequent classes to override. We cannot (directly) create objects of a type that is an abstract base class. Because Disc_quote
defines net_price
as a pure virtual, we cannot define objects of type Disc_quote
. We can define objects of classes that inherit from Disc_quote
, so long as those classes override net_price
:
// Disc_quote declares pure virtual functions, which Bulk_quote will overrideDisc_quote discounted; // error: can't define a Disc_quote objectBulk_quote bulk; // ok: Bulk_quote has no pure virtual functions
Classes that ...
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.