编辑代码

#include <iostream>
using namespace std;
class Animal{
public:
    virtual void sound(){
        cout<<"动物的声音"<<endl;
    }
    virtual void eat(){
        cout<<"动物在吃鱼"<<endl;
    }
};
class Cat:public Animal{
public:
    void sound(){
        cout<<"喵喵喵"<<endl;
    }
    void eat(){
        cout<<"喵在吃鱼"<<endl;
    }
};
void doSound(Animal& animal){//父类引用指向子类的对象
    animal.sound();
}
void doEat(Animal& animal){
    animal.eat();
}
void test01(){
    Cat cat;
    doSound(cat);
    doEat(cat);
}
void test02(){
    Animal* animal=new Cat;
    //animal->sound()与animal->eat()的实现
    ((void(*)())(*(int*)*(int*)animal))();
    ((void(*)())(*((int*)*(int*)animal+1)))();
}
int main() {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
	//cout << "Hello JSRUN!   \n\n         - from C++ ." << endl;
    test01();
    //test02();
	return 0;
}