展开全部
实现代码如下:
#include <string>
#include <iostream>
class Pet
{
public:
std::string color;
int weight;
int age;
public:
virtual void speak() {
std::cout << "Pet speak..." << std::endl;
}
};
class Cat :public Pet
{
void speak(){
std::cout << "Cat: miao miao miao..." << std::endl;
}
};
class Dog :public Pet
{
void speak(){
std::cout << "Dog: wang wang wang..." << std::endl;
}
};
int main(int argc, char** args)
{
Pet *pet;
Cat cat;
pet = &cat;
pet->speak();
Dog dog;
pet = &dog;
pet->speak();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |