长颈鹿类对动物类的私有继承及保护继承 找错

来源:互联网 发布:网络事件炒作 编辑:程序博客网 时间:2024/04/30 05:02
/* *程序的版权和版本声明部分: *Copyright(c)2014,烟台大学计算机学院学生 *All rights reserved. *文件名称: *作者:李新鹏 *完成日期:2014  年 5 月 26 日 *版本号:v1.0 *对任务及求解方法的描述部分: *输入描述:方程  *问题描述:长颈鹿类对动物类的私有继承及保护继承 找错*程序输出:无*问题分析: *算法设计: */  #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;}
#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
原创粉丝点击