1 #include <iostream> 2 using namespace std; 3 4 class myclass 5 { 6 public: 7 int i = 10; 8 void show() 9 { 10 cout << "myclass" << i << endl; 11 } 12 }; 13 14 class mynewclass :public myclass 15 { 16 public: 17 int i = 100; 18 void show() 19 { 20 cout << "mynewclass" << i << endl; 21 } 22 }; 23 24 void main() 25 { 26 myclass *p1 = new myclass; 27 //父类指针可以存储子类地址 28 myclass *p2 = new mynewclass; 29 p1->show(); 30 p2->show(); 31 cin.get(); 32 }