#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(); // 正确,公有继承下,基类的公有成员对派生类对象可见
gir.play(); // _______________
gir.drink(); // _______________
gir.take(); // _______________
gir.StrechNeck(); //正确,该函数本来就是派生类的公有函数 _______________
Animal ani;
ani.eat(); // _______________
ani.play(); // _______________
ani.drink(); // _______________
ani.take(); //错误,派生类的成员对基类对象(不论访问属性)不可见
ani.StrechNeck(); //
return 0;
}
第12周项目1--长颈鹿类对动物类的继承(1)
最新推荐文章于 2014-05-25 21:46:45 发布