#include #include using namespace std; //-------------------------------------------- // Base class is abstract //-------------------------------------------- class Base { protected: int x; public: Base() : x(0) {} virtual string toString() = 0; // abstract method }; //-------------------------------------------- // Derived class must either be abstract or // else provide a toString() implementation // (so D1 is an abstract class as well) //-------------------------------------------- class D1 : public Base { protected: int y; public: D1() : y(-10) {} }; //-------------------------------------------- // A D2 can be instantiated (i.e., D2 isn't an // abstract class). //-------------------------------------------- class D2 : public D1 { protected: int z; public: D2(int init) : z(init) {} string toString() override { return "x = " + to_string(x) + " y = " + to_string(y) + " z = " + to_string(z); } }; //-------------------------------------------- // main //-------------------------------------------- int main(int argc, char **argv) { Base b; // error D1 d1; // error D2 d2(100); // OK cout << "d2 = " << d2.toString() << endl; return 0; }