Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Abstract class

 

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.

 

 

 

 

 

Advice

 

 

 

 

 

 

Reference

 

  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'

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict