#include <iostream>;
using namespace std;
/*
类的继承
*/
class Animal
{
public:
Animal()
{
cout << "Animal construct" << endl;
}
~Animal()
{
cout << "Animal deconstruct" << endl;
}
void eat()
{
cout << "Animal eat" << endl;
}
protected :
void sleep()
{
cout << "Animal sleep" << endl;
}
private :
void breathe()
{
cout << "Animal breathe" << endl;
}
};
class Fish : public Animal
{
public :
Fish()
{
cout << "Fish construct" << endl;
}
~Fish()
{
cout << "Fish deconstruct" << endl;
}
void test()
{
eat();
sleep();
//breathe(); 不可调用
}
};
int main(){
Animal animal;
animal.eat();
//animal.sleep(); 无法调用
//animal.breathe(); 无法调用
Fish fish;
fish.test();
//fish.eat(); 无法调用
return 0 ;
}
从0学习C++ (八) 继承
最新推荐文章于 2024-11-02 20:20:37 发布