cout<<"************** father1 end *****************"<<endl<<endl;
Father *father2 = new Child();
cout<<"father2 count = "<<father2->getCount()<<endl; // father2 don't have getAge() method
delete father2;
cout<<"************** father2 end *****************"<<endl<<endl;
Child *child = new Child();
cout<<"child count = "<<child->getCount()<<endl;
cout<<"child age = "<<child->getAge()<<endl;
delete child;
cout<<"************** child end *****************"<<endl<<endl;
getchar();
return 0;
}
4.输出结果:
Father is called. count = 1 Father::getCount() count = 1 father1 count = 1 ~Father is called. ************** father1 end *****************
Father is called. count = 1 Child is called. count = 2, age = 20 Child::getCount() count = 2 father2 count = 2 ~Child is called. ~Father is called. ************** father2 end *****************
Father is called. count = 1 Child is called. count = 2, age = 20 Child::getCount() count = 2 child count = 2 Child::getAge() age = 20 child age = 20 ~Child is called. ~Father is called.