// B2.4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // Define a base class B and two derived classes D1 and D2 so that // B provides a function that a client can use to create an instance // of either derived class, as shown below. // // B should not have to know the names of the derived classes: // // D1 d1; D1 *pD1 = D1::create(); // D2 d2; D2 *pD2 = D2::create(); // // To build this application you need to enable RTTI using project // properties. Get to that by right clicking on the project name. //// a complete answer //////////////////////// template class B { public: virtual ~B() {} static T* create() { return new T; } virtual void showSelf() { cout << "\n " << typeid(*this).name(); } }; class D1 : public B { }; class D2 : public B { }; /////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Exam B2 - Question #4 " << "\n =======================\n"; D1 d1; D1 *pD1 = D1::create(); pD1->showSelf(); delete pD1; D2 d2; D2 *pD2 = D2::create(); pD2->showSelf(); delete pD2; cout << "\n\n"; return 0; }