-2
Galera,estou precisando criar umavariáveldo tipo object quepoderáinstanciar outros tipos queàherdam。C++类铸造
家伙我试图创建一个对象,可以实例化的其他类型的继承它:
#include <iostream>
#include <cstdlib>
class Animal {
public:
char *nome;
Animal (char *nome) {
this->nome = nome;
}
};
class Cachorro : public Animal {
public:
bool enterraOsso;
Cachorro (char* nome, bool enterraOsso) : Animal(nome) {
this->enterraOsso = enterraOsso;
}
};
class Passaro : public Animal {
public:
bool voar;
Passaro (char* nome, bool voar) : Animal(nome) {
this->voar = voar;
}
};
int main() {
Animal *animal;
animal = new Cachorro("Scooby", true);
std::cout << animal->nome << ", " << animal->enterraOsso << std::endl;
animal = new Passaro("Piopio", false);
std::cout << animal->nome << ", " << animal->voar << std::endl;
return 0;
}
的想法是访问子类从超还属性。
我不知道这是一个演员还是多态,在Java中我知道这是可能的,但不能用C++来完成。
谢谢你的一切帮助。
请用英文提问,或邮寄到[pt.so]代替。 – JJJ
我投票结束这个问题作为题外话,因为它不是英文 - 这是一个**仅英文**网站 - 请尊重网站的规则! –