动物这样叫(2)

*Copyright (c) 2014,烟台大学计算机学院

*All right reserved.

*文件名称:test.cpp

*作    者:韩双志

*完成日期:2016年6月2日

*版本号:v1.0

*

*问题描述:显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p = new Animal();将出错,将此行删除。

输入描述:无

*输出描述:按要求输出

/*

#include "iostream"
#include<string>
using namespace std;
class Animal
{
public:
    virtual void cry() = 0;
};

class Mouse : public Animal
{
private:
    string name;
    char sex;
public:
    Mouse(string nam, char s):name(nam),sex(s) {}
    virtual void cry()
    {
        cout<<"我叫"<<name<<",是一只"<<((sex=='m')?"男":"女")<<"老鼠,我的叫声是:吱吱吱!"<<endl;
    }
};

class Cat : public Animal
{
private:
    string name;
public:
    Cat(string nam):name(nam) {}
    virtual void cry()
    {
        cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!"<<endl;
    }
};

class Dog : public Animal
{
private:
    string name;
public:
    Dog(string nam):name(nam) {}
    virtual void cry()
    {
        cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;
    }
};

class Giraffe : public Animal
{
private:
    string name;
    char sex;
public:
    Giraffe(string nam,char s):name(nam), sex(s) {}
    virtual void cry()
    {
        cout<<"我叫"<<name<<",是"<<((sex=='m')?"男":"女")<<"长颈鹿,我的脖子太长,发不出声音来!"<<endl;
    }
};

int main( )
{
    Animal *p;
//    p = new Animal();
//    p->cry(); //输出: 不知哪种动物,让我如何学叫?
    Mouse m1("Jerry",'m');
    p=&m1;
    p->cry(); //输出: 我叫Jerry,是一只男老鼠,我的叫声是:吱吱吱!
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry(); //输出: 我叫Jemmy,是一只女老鼠,我的叫声是:吱吱吱!
    Cat c1("Tom");
    p=&c1;
    p->cry(); //输出: 我叫Tom,是一只猫猫,我的叫声是:喵喵喵!
    Dog d1("Droopy");
    p=&d1;
    p->cry(); //输出: 我叫Droopy,是一条狗狗,我的叫声是:汪汪汪!
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry(); //输出: 我叫Gill,是男长颈鹿,脖子太长,发不出声音来!
    return 0;
}

 

 */

运行结果:

知识点总结:

     类的继承,指针,指针不能指向抽象类

学习心得

    学会了类的继承,能知道具体知道指针指到什么位置,指针不能指向抽象类

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较完整的菜单程序,可以对动物园的动物进行管理。代码如下(注释已加): ```python # 定义动物类 class Animal: def __init__(self, id, type, color, sex, price, buyDate): self.id = id self.type = type self.color = color self.sex = sex self.price = price self.buyDate = buyDate self.isDead = False # 定义动物园类 class Zoo: def __init__(self): self.animals = [] # 添加动物信息 def addAnimal(self, id, type, color, sex, price, buyDate): # 判断id是否已存在 for animal in self.animals: if animal.id == id: print("该动物已存在!") return # 创建新动物实例 newAnimal = Animal(id, type, color, sex, price, buyDate) # 添加至动物园 self.animals.append(newAnimal) print("添加成功!") # 注销一条动物信息(死亡或卖掉) def cancelAnimal(self, id): # 判断id是否存在 for animal in self.animals: if animal.id == id: # 修改isDead属性 animal.isDead = True print("注销成功!") return print("该动物不存在!") # 查询全部动物信息 def queryAll(self): print("所有动物信息:") for animal in self.animals: # 判断isDead属性 if not animal.isDead: print("ID:{}, {}, {}, {}, {}购买, {}入园".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buyDate)) # 查询某种颜色的动物信息 def queryByColor(self, color): print("颜色为{}的动物信息:".format(color)) for animal in self.animals: # 判断isDead属性和颜色 if not animal.isDead and animal.color == color: print("ID:{}, {}, {}, {}, {}购买, {}入园".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buyDate)) # 統計某种类型的动物信息 def countByType(self, type): count = 0 for animal in self.animals: # 判断isDead属性和类型 if not animal.isDead and animal.type == type: count += 1 print("类型为{}的动物数量为{}".format(type, count)) # 统计某种类型的动物的价值 def countPriceByType(self, type): price = 0 for animal in self.animals: # 判断isDead属性和类型 if not animal.isDead and animal.type == type: price += animal.price print("类型为{}的动物总价值为{}".format(type, price)) # 修改某个动物的基本信息(输入id) def modifyAnimal(self, id): # 判断id是否存在 for animal in self.animals: if animal.id == id: # 修改属性 animal.color = input("请输入新颜色:") animal.price = int(input("请输入新价格:")) print("修改成功!") return print("该动物不存在!") # 菜单程序 def menu(self): while True: print("动物园管理系统") print("1.添加动物信息") print("2.注销动物信息") print("3.查询全部动物信息") print("4.查询某种颜色的动物信息") print("5.统计某种类型的动物信息") print("6.统计某种类型的动物的价值") print("7.修改某个动物的基本信息") print("8.退出") choice = input("请输入要进行的操作:") if choice == "1": id = int(input("请输入动物ID:")) type = input("请输入动物类型:") color = input("请输入动物颜色:") sex = input("请输入动物性别:") price = int(input("请输入动物价格:")) buyDate = input("请输入动物入园时间:") self.addAnimal(id, type, color, sex, price, buyDate) elif choice == "2": id = int(input("请输入动物ID:")) self.cancelAnimal(id) elif choice == "3": self.queryAll() elif choice == "4": color = input("请输入颜色:") self.queryByColor(color) elif choice == "5": type = input("请输入类型:") self.countByType(type) elif choice == "6": type = input("请输入类型:") self.countPriceByType(type) elif choice == "7": id = int(input("请输入动物ID:")) self.modifyAnimal(id) elif choice == "8": print("谢谢使用,再见!") break else: print("输入有误,请重新输入!") # 测试 zoo = Zoo() zoo.menu() ``` 使用时,可通过调用菜单程序来实现对动物动物信息的管理。例如,输入1添加动物信息,输入3查询全部动物信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值