1 // TransformChildToParentToChild.cpp : 定義控制台應用程序的入口點。 2 //王林森 3 4 #include "stdafx.h" 5 #include <iostream> 6 7 using namespace std; 8 9 10 class CParent 11 { 12 public: 13 CParent():nP(1){} 14 int GetP(){return nP;} 15 private: 16 int nP; 17 }; 18 19 class CChild:public CParent 20 { 21 public: 22 CChild():nC(2){} 23 int GetC(){return nC;} 24 private: 25 int nC; 26 }; 27 28 29 30 int _tmain(int argc, _TCHAR* argv[]) 31 { 32 CParent* poParent=(CParent*)new CChild; 33 cout<<poParent->GetP()<<endl; 34 35 CChild* poChild=(CChild*)poParent; 36 cout<<poChild->GetC()<<endl; 37 38 return 0; 39 }