第二学期第12周项目1--长颈鹿类对动物类的继承

来源:互联网 发布:mac qq怎么发送文件 编辑:程序博客网 时间:2024/05/16 11:51
#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
原创粉丝点击