// A2.7.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, showType, that displays the type of any // class that derives from B. a client uses the function as shown below. // // B should not have to know the names of the derived classes: // // D1 d1; d1.showType(); // shows type of d1; // D2 d2; d2.showType(); // shows type of d2; template class B { public: void showType() { cout << "\n " << typeid(T).name(); // derived type } }; class D1 : public B { }; class D2 : public B { }; class B2 { // must define virtual somewhere to get dynamic typeid public: virtual ~B2() {} // since class has vtble, this points to dynamic type void showType() { cout << "\n my type is " << typeid(*this).name(); }; }; class D3 : public B2 {}; class D4 : public B2 {}; int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Exam A2 - Question #7 " << "\n =======================\n"; D1().showType(); D2().showType(); D3().showType(); D4().showType(); cout << "\n\n"; return 0; }