第十二周项目1-2private继承下的访问权限和继承方式

来源:互联网 发布:降温软件排行第一 编辑:程序博客网 时间:2024/05/19 10:11
/*Copyright (c) 2011, 烟台大学计算机学院* All rights reserved.* 作    者: 石尧* 完成日期:2014 年05 月13日* 版 本 号:v1.0** 问题描述:private继承下的访问权限和继承方式。* 样例输入:略.* 样例输出:略。* 问题分析:略。*/#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:    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