#include using namespace std; //------------------------------------------------------------------------------ // An abstract base class. //------------------------------------------------------------------------------ class AbstractBase { public: AbstractBase() {} virtual void pureMethod() const = 0; }; //------------------------------------------------------------------------------ // Derived class from AbstractBase. //------------------------------------------------------------------------------ class DerivedClass: public AbstractBase { public: DerivedClass(): AbstractBase() {} virtual void pureMethod() const { cout << "Derived instance at " << this << endl; } }; //------------------------------------------------------------------------------ // A class which takes an instance of the abstract base class //------------------------------------------------------------------------------ class UseAbstractType { public: UseAbstractType(const AbstractBase& base): mBasePtr(&base) {} void callPureMethod() const { mBasePtr->pureMethod(); } private: const AbstractBase* mBasePtr; }; //------------------------------------------------------------------------------ // A non-abstract (definite?) base. //------------------------------------------------------------------------------ class DefiniteBase { public: DefiniteBase() {} virtual void method() const { cout << "DefiniteBase instance at " << this << endl; } }; //------------------------------------------------------------------------------ // Derived class from DefiniteBase. //------------------------------------------------------------------------------ class AnotherDerivedClass: public DefiniteBase { public: AnotherDerivedClass(): DefiniteBase() {} virtual void method() const { cout << "AnotherDerived instance at " << this << endl; } }; //------------------------------------------------------------------------------ // A class which takes an instance of the definite base class //------------------------------------------------------------------------------ class UseDefiniteType { public: UseDefiniteType(const DefiniteBase& base): mBasePtr(&base) {} void callMethod() const { mBasePtr->method(); } private: const DefiniteBase* mBasePtr; };