/*
* 程序的版权和版本声明部分:
* 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: private Animal
{
public:
Giraffe() {}
void StrechNeck()
{
cout << "Strech neck \n";
}
void take()
{
eat(); //正确,公有继承下,基类的公有成员对派生类可见
//drink(); // 错误,drink为Animal的私有成员,类外不能访问
play(); // 正确
}
};
int main()
{
//Giraffe gir;
//gir.eat(); // 错误,私有继承下,基类的公有成员成为派生类的私有成员,派生类外不可访问
//gir.play(); // 错误,私有继承下,基类的保护成员成为派生类的私有成员,派生类外不可访问
//gir.drink(); //错误,drink为基类的私有成员,基类外不可访问
cout<<"hello world!";
return 0;
}
运行结果