In C++ a pure abstract base class is a contractThis tip submitted by Marty Corbett on 2012-01-31 17:00:19. It has been viewed 19955 times.
Rating of 5.1 with 110 votes
A pure abstract base class is one in which all of its member functions are declared as virtual and have no body (=0). This can be thought of as a contract between the user and the implementer of a class.
Any class that derives from the pure abstract base class must provide the behavior for all of the member functions (in the example below this is class B) in the exact form as specified by the "contract".
From a usage perspective the details of what will be provided by the derived class are specified, but not how it will be provided.
Therefore, we can have one person implement the behavior in the derived class and another person implement the usage of that behavior.
The main code knows that the rules of the contract have been adhered to so it can use class C in the same way as it used class B by changing the allocation in main as follows:
// Use the contract Contract *pContract = new C;
This structure provides a mechanism to specify an interface, "contract", between two entities prior to any code ever being written. Another benefit is that the contract allows testing of the interface without any of the final code on either side of the interface.
Help your fellow programmers! Add a tip!