继承后重载:即先继承,然后再对继承的子类进行重载
#include <iostream>
#include <string>
//吃的参数不同,调用对应的函数
class Animal
{
public:
Animal(std::string theName);
void eat();
void sleep();
private:
std::string name;
};
class Pig:public Animal
{
public:
Pig(std::string theName);
void climb();
void eat();
void eat(int eatCount);
};
Animal::Animal(std::string theName)
{
name = theName;
}
void Animal::eat()
{
std::cout<<"(基类)吃(未重载)"<<std::endl;
}
Pig::Pig(std::string theName):Animal(theName)
{
}
void Pig::climb()
{
std::cout<<"小猪爬树"<<std::endl;
}
void Pig::eat()
{
Animal::eat();//基类的吃
std::cout<<"吃(覆盖基类的吃)"<<std::endl;
}
void Pig::eat(int eatCount)
{
std::cout<<"吃(对子类的吃(上面的方法)重载)"<< eatCount <<"碗"<<std::endl;
}
int main()
{
Pig pig("小猪");
pig.climb();
pig.eat();
pig.eat(15);
return 0;
}