麻烦大佬出题目
谁能帮我出个C++题(虚函数、调用、构造析构、2种类和对象、继承)阅读程序写运行结果
附上您的标准答案
我来检查一下
定义一个动物类,有重量、高度、几只脚等属性,有eat和fly等虚函数。鹦鹉和牛继承动物类,并实现eat和fly函数,实现析构函数。在主程序中生成两种对象并调用方法。
#include <iostream>
using namespace std;
class Animal
{
protected:
int weight;
int height;
int nFoot;
public:
Animal(int w,int h,int n)
{
weight = w; height = h;nFoot = n;
}
~Animal(){}
virtual void Eat()=0;
virtual void Fly()=0;
void show()
{
cout << "重量:"<< weight <<",高度:"<<height << ",有"<< nFoot<<"只脚"<<endl;
}
};
class Yingwu : public Animal
{
public:
Yingwu(int w,int h,int n):Animal(w,h,n){cout << "生成一个鹦鹉"<<endl;}
~Yingwu(){cout << "销毁一个鹦鹉"<<endl;}
void Eat()
{
cout << "鹦鹉吃杂粮" << endl;
}
void Fly()
{
cout << "鹦鹉有翅膀,可以飞翔"<<endl;
}
};
class Cow : public Animal
{
public:
Cow(int w,int h,int n):Animal(w,h,n){cout << "生成一个牛"<<endl;}
~Cow(){cout << "销毁一个牛"<<endl;}
void Eat()
{
cout << "牛吃草" << endl;
}
void Fly()
{
cout << "牛没有翅膀,不可以飞翔"<<endl;
}
};
int main()
{
Yingwu yw(1,10,2);
Cow cw(100,150,4);
yw.Eat();
yw.Fly();
yw.show();
cw.Eat();
cw.Fly();
yw.show();
return 0;
}