#include #include using namespace std; class Base { public: Base(int x) : x_(x) { } virtual string toString() { return string("Base: x_ = ") + std::to_string(x_); } int x_; }; class Subclass : public Base { public: Subclass(int y) : Base(16), y_(y) { } string toString() override { return string("Subclass: y_ = ") + std::to_string(y_) + " x_ = " + std::to_string(x_); } int y_; }; int main(int argc, char *argv[]) { Base b(1); cout << "b = " << b.toString() << endl; Subclass d(2); cout << "d = " << d.toString() << endl; b = d; // what happens to y_? // d = b; // would be a compiler error. Why? cout << "b = " << b.toString() << endl; }