A question about C++# Computation - 科学计算
w*n
1 楼
An Animal class, a subclass Cat, both of them can speak(). There is a cage
class which can hold an animal. I shake() the cage and want the animal in it
to speak. Here is my code. The problem is, if i put the cat in the cage, it
won't speak "miao" but "animal". Any way to solve this?
class Animal {
public :
virtual void speak() {cout << "Animal"< };
class Cat : public Animal {
public :
void speak() {cout << "Miao" << endl;}
};
class Cage {
Animal aa;
public :
Cage(Animal& a):
class which can hold an animal. I shake() the cage and want the animal in it
to speak. Here is my code. The problem is, if i put the cat in the cage, it
won't speak "miao" but "animal". Any way to solve this?
class Animal {
public :
virtual void speak() {cout << "Animal"<
class Cat : public Animal {
public :
void speak() {cout << "Miao" << endl;}
};
class Cage {
Animal aa;
public :
Cage(Animal& a):