Implementation inheritance vs. Subtyping Subtyping IS-A relationship superclass defines method interface subclasses override superclass methods subclass objects can be substituted for superclass objects you want subclass methods to be called when invoked through superclass pointer static binding vs. dynamic binding subtyping + dynamic binding = polymorphism Polymorphism in C++ Chess example call Piece Factory ask Piece for legal moves delete Piece By default, C++ uses static binding Virtual methods use dynamic binding How about destructors? same as other methods virtual destructors Pure virtual methods Often times a virtual method can only be defined in the subclass, and it is undesirable to provide a default implementation in the superclass You can make a method "pure virtual" in the superclass Abstract and Interface classes A superclass defines the method interface for all subclasses. For each method, the superclass can choose whether or not to provide a default implementation If a superclass has at least one pure virtual method, it is called an "abstract class" because it cannot be instantiated Examples: Window superclass, Piece superclass If a superclass only has pure virtual methods, it is called an "interface class" (relate to Java interfaces) Example: InputStream in CS240 Utilities V-tables