#include <iostream>
#include <cstring>
using namespace std;
// 设计一个基类 动物类(属性:体重、颜色、年龄 、性别 行为:吃喝拉撒)
// 设计一个猫类 继承动物类(属性:体重、颜色、年龄 、性别、 行为:吃喝拉撒、抓老鼠)
// 定义一个猫对象 ---断尾猫,调用猫的行为
class animal
{
public:
float weight;
string color;
int age;
string sex;
animal(float weight = 1, string color = "yellow", int age = 1, string sex = "nan")
{
this->weight = weight;
this->color = color;
this->age = age;
this->sex = sex;
}
void eat() { cout << "动物会吃" << endl; }
void sleep() { cout << "动物会喝水" << endl; }
void run() { cout << "动物会拉屎" << endl; }
void drink() { cout << "动物会睡觉" << endl; }
protected:
int home;
private:
int id;
};
class cat : public animal
{
public:
string lei = "cat";
cat(float w = 2, string c = "yellow11", int a = 2, string s = "nan")
{
weight = w;
color = c;
age = a;
sex = s;
}
void function()
{
cout << "猫可以吃饭、喝水、拉屎、睡觉" << endl;
}
void zhua()
{
cout << "猫会抓老鼠" << endl;
}
void show(void)
{
cout<<"猫的体重是:"<<weight<<"Kg 猫的颜色是:"<<color<<" 我的猫"<<age<<"岁了"<<"是"<<sex<<"猫"<<endl;
}
};
int main()
{
cat duanweicat;
duanweicat.function();
duanweicat.zhua();
duanweicat.eat();
duanweicat.drink();
duanweicat.run();
duanweicat.sleep();
duanweicat.show();
}
C++ 动物类
最新推荐文章于 2024-11-12 13:55:23 发布