12.1.3

来源:互联网 发布:怎么用cmd运行java文件 编辑:程序博客网 时间:2024/06/06 05:20
#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
原创粉丝点击