#include <iostream>
using namespace std;
class Animal
{
public:
Animal() {}
void eat()
{
cout << "eat\n";
}
protected:
void play()
{
cout << "play\n";
}
private:
void drink()
{
cout << "drink\n";
}
};
class Giraffe: private Animal //私有继承,基类的public和protected变成private
{
public:
Giraffe() {}
void StrechNeck()
{
cout << "Strech neck \n";
}
void take()
{
eat(); // 正确,私有继承下,基类的公有成员对派生类可见
//drink(); // 错误,私有继承下,基类的私有成员对派生类不可见
play(); // 正确,私有继承下,基类的保护成员对派生类可见
}
};
int main()
{
Giraffe gir;
//gir.eat(); //错误,私有继承下,基类的公有成员对派生类不可见
//gir.play(); //错误,私有继承下,基类的保护成员对派生类不可见
//gir.drink(); //错误,私有继承下,基类的私有成员对派生类不可见
return 0;
}
长颈鹿类对动物类的private继承
最新推荐文章于 2014-06-14 16:37:51 发布