12程序阅读3

来源:互联网 发布:加布.纽维尔 知乎 编辑:程序博客网 时间:2024/06/05 21:53
程序:
//长颈鹿类对动物类的继承(3)protected继承:基类中的私有不可用,其他在派生类中变为保护#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();派生的保护,类外不可用   // gir.play();基类的保护,类外不可用   //gir.drink();基类的私有,类外不可用    return 0;}
错误图示:
0 0
原创粉丝点击