#include <iostream>
#include <string>
class Pet
{
public:
Pet(std::string theName);
virtual void eat();
protected:
std::string name;
};
class Cat : public Pet
{
public:
Cat(std::string theName);
void eat();
};
Pet::Pet(std::string theName)
{
name = theName;
}
void Pet::eat()
{
std::cout << name << " 正在吃东西!\n\n";
}
Cat::Cat(std::string theName):Pet(theName)
{
}
void Cat::eat()
{
Pet::eat();
std::cout << name << "正在吃东西!玩毛线球!\n\n";
}
int main()
{
Pet *cat = new Cat("加菲");//这里的是Pet类,会先访问Pet,在去Cat中
cat->eat();
return 0;
}
#include <string>
class Pet
{
public:
Pet(std::string theName);
virtual void eat();
protected:
std::string name;
};
class Cat : public Pet
{
public:
Cat(std::string theName);
void eat();
};
Pet::Pet(std::string theName)
{
name = theName;
}
void Pet::eat()
{
std::cout << name << " 正在吃东西!\n\n";
}
Cat::Cat(std::string theName):Pet(theName)
{
}
void Cat::eat()
{
Pet::eat();
std::cout << name << "正在吃东西!玩毛线球!\n\n";
}
int main()
{
Pet *cat = new Cat("加菲");//这里的是Pet类,会先访问Pet,在去Cat中
cat->eat();
return 0;
}