An abstract class is a type of class that can not be instanciated itself: only its derivatived classes can.
An Abstract base class is an abstract class that is also a base class.
An Abstract can be recognized by a member function starting with the keyword virtual and ending with =0;. And because all base classes must have a virtual destructor, an abstract class must also have one.
struct ABC { virtual ~ABC() {} //Empty virtual destructor virtual void whatMakesMeAbstract() = 0; };
Many Design Patterns rely on abstract classes, for example the Strategy.
- Use abstract classes to focus design on the provision of clean interfaces [1]
- An abstract class should have a virtual destructor [2,3] to ensure proper cleanup [6]
- An abstract class typically doesn't need a constructor [4]
- Avoid member variables in abstract classes [5]
- [1] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 20.7. Advice. page 611: '[3] Use abstract classes to focus design on the provision of clean interfaces'
- [2] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[4] If a class has a virtual function, it needs a virtual destructor'
- [3] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 20.7. Advice. page 611: '[8] A class with a virtual function should have a virtual destructor'
- [4] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 20.7. Advice. page 611: '[9] An abstract class typically doesn't need a constructor'
- [5] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 21.4. Advice. page 640: '[2] Avoid data members in base classes intended as interfaces'
- [6] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 21.4. Advice. page 640: '[4] Give an abstract class a virtual destructor to ensure proper cleanup'