从0学习C++ (八) 继承

#include ;
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 ;
}

你可能感兴趣的:(C++,c++)