int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; Animal *animal = new Animal(); animal->run(); Animal *dog = new Dog(); dog->run(); #if 0 // can not cast 'Cat' to its private base class 'Animal' \ 私有继承的时候不能转换成基类,同样,保护继承也不能转换,只有共有继承可以。\
int main() { Animal *obj; cout<<"Dog Object\n"; obj = new Dog; obj -> sleep(); obj -> run(); obj -> eat(); delete obj; cout<<"Horse Object \n"; obj = new Horse; obj->sleep(); obj->run(); obj -> eat(); delete obj; return 0; }
void test(Animal &a) { a.run(); }
/* 类型通用 损失个性 */ void testAnimal(Animal& a){ a.run(); // 恢复个性 }