长颈鹿类对动物类的继承【protect】

来源:互联网 发布:西门子plc300编程 编辑:程序博客网 时间:2024/04/28 12:41
#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;}

0 0
原创粉丝点击