///////////////////////////////////////////////////////////////////// // MT1Q3.cpp #include #include #include class B { public: virtual ~B() {} virtual const char* say() { return "B instance"; } }; class D : public B { virtual const char* say() { return "D instance"; } }; B* f() { return new D; } B* f(const D& d) { return new D(d); } void main() { std::cout << "\n Creating a copy of an instance of D, then sliced to a B:"; B* pB; D d; B b = *(pB = f(d)); std::cout << "\n " << b.say(); delete pB; std::cout << "\n Creating an instance of D, bound to a B&:"; B& rb = *f(); std::cout << "\n " << rb.say(); delete &rb; std::cout << "\n\n"; } Creating a copy of an instance of D, then sliced to a B: B instance Creating an instance of D, bound to a B&: D instance