/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 作 者:马广明
* 完成日期:2014 年 5 月 13 日
* 问题介绍: 继承的判断
* 版 本 号: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: public Animal //长颈鹿类
{
public:
Giraffe() {}
void StrechNeck()
{
cout << "Strech neck \n";
}
private:
void take()
{
eat(); // 正确,公有继承下,基类的公有成员对派生类可见
drink(); // 正确,公有继承下,基类的保护成员对派生类可见
play(); // 不正确,公有继承下,基类的私有成员对派生类不可见
}
};
int main()
{
Giraffe gir; //定义派生类的对象
gir.eat(); // 正确,公有继承下,基类的公有成员对mian函数可见
gir.play(); // 不正确,保护成员对mian函数不可见
gir.drink(); // 不正确,私有成员对mian函数不可见
gir.take(); // 不正确,私有成员对mian函数不可见
gir.StrechNeck(); // 正确,公有成员对mian函数可见
Animal ani;
ani.eat(); // 正确,基类的公有函数对mian函数可见
ani.play(); // 不正确,保护成员对mian函数不可见
ani.drink(); // 不正确,私有成员对mian函数不可见
ani.take(); //错误,派生类的成员对基类对象(不论访问属性)不可见
ani.StrechNeck(); // 错误,派生类的成员对基类对象(不论访问属性)不可见
return 0;
}
多重继承--判断
最新推荐文章于 2023-06-18 19:03:54 发布