#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;
}
运行结果
心得体会
通过这个程序 对所学知识进行了更加深刻的理解!