- Encapsulation and Extensibility of Types
- Inheritance
- Polymorphism
- Overload
- Virtual Functions
- Static Members of a Class
- Function templates
- Class templates
- Abstract Classes
Abstract Classes and Pure Virtual Functions
Abstract classes are used for creating generic entities, that you expect to use for creating more specific derived classes. An abstract class can only be used as the base class for some other class, that is why it is impossible to create an object of the abstract class type.
A class which contains at least one pure virtual function in it is abstract. Therefore, classes derived from the abstract class must implement all its pure virtual functions, otherwise they will also be abstract classes.
A virtual function is declared as "pure" by using the pure-specifier syntax. Consider the example of the CAnimal class, which is only created to provide common functions – the objects of the CAnimal type are too general for practical use. Thus, CAnimal is a good example for an abstract class:
class CAnimal |
Here Sound() is a pure virtual function, because it is declared with the specifier of the pure virtual function PURE (=0).
Pure virtual functions are only the virtual functions for which the PURE specifier is set: (=NULL) or (=0). Example of abstract class declaration and use:
class CAnimal
|
Restrictions on abstract classes
If the constructor for an abstract class calls a pure virtual function (either directly or indirectly), the result is undefined.
//+------------------------------------------------------------------+
|
However, constructors and destructors for abstract classes can call other member functions.