106.多态与虚函数
1 #include <iostream> 2 using namespace std; 3 4 class Aobject 5 { 6 public: 7 //虚函数提供一个接口,自动选择子类的接口 8 //存储某个对象的地址,调用对应的方法 9 virtual void show() 10 { 11 cout << "Aobject" << endl; 12 } 13 14 }; 15 16 class Bobject :public Aobject 17 { 18 public: 19 void show() 20 { 21 cout << "Bobject" << endl; 22 } 23 }; 24 25 void main() 26 { 27 Aobject *p = new Bobject; 28 p->show(); 29 cin.get(); 30 }