#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: protected Animal
{
public:
Giraffe() {}
void StrechNeck()
{
cout << "Strech neck \n";
}
void take()
{
eat(); //正确,保护继承下,基类中的共有成员在派生类中的访问属性为受保护成员,在派生类中可见
//drink(); //正确,保护继承下,基类中的私有成员在派生类中的访问属性为不可访问
play(); // 正确,保护继承下,基类中受保护成员在派生类的访问属性为受保护成员,在派生类中可见
}
};
int main()
{
Giraffe gir;
//gir.eat(); // 错误,Giraffe的对象不能在类外访问本类中的受保护成员
//gir.play(); // 错误,Giraffe的对象不能在类外访问本类中的受保护成员
//gir.drink(); // 错误,Giraffe的对象不能在类外访问本类的不可访问函数
return 0;
}
长颈鹿类对动物类的继承【protect】
最新推荐文章于 2014-05-27 14:58:23 发布