动物这样叫-(1)

  1. #ifndef ANIMAL_H_INCLUDED  
  2. #define ANIMAL_H_INCLUDED  
  3. #include <iostream>  
  4. class Animal  
  5. {  
  6. public:  
  7.     virtual void cry()  
  8.     {  
  9.         std::cout << " 不知哪种动物,让我如何学叫?" << std::endl;  
  10.     }  
  11. };  
  12.   
  13. class Mouse: public Animal  
  14. {  
  15. public:  
  16.     Mouse(std::string nam,char se):name(nam),sex(se){}  
  17.     void cry();  
  18. private:  
  19.     std::string name;  
  20.     char sex;  
  21. };  
  22.   
  23. class Cat:public Animal  
  24. {  
  25. public:  
  26.     Cat(std::string nam):name(nam){}  
  27.     void cry();  
  28. private:  
  29.     std::string name;  
  30. };  
  31.   
  32. class Dog:public Animal  
  33. {  
  34. public:  
  35.     Dog(std::string nam):name(nam){}  
  36.     void cry();  
  37. private:  
  38.     std::string name;  
  39. };  
  40.   
  41. class Giraffe:public Animal  
  42. {  
  43. public:  
  44.     Giraffe(std::string nam,char se):name(nam),sex(se){}  
  45.     void cry();  
  46. private:  
  47.     std::string name;  
  48.     char sex;  
  49. };  
  50.   
  51. #endif // ANIMAL_H_INCLUDED  

Animal.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "Animal.h"  
  2.   
  3. void Mouse::cry()  
  4. {  
  5.     std::cout << " 我叫" << name << ",是一只";  
  6.     if(sex == 'm')  
  7.         std::cout << " 男";  
  8.     else  
  9.         std::cout << " 女";  
  10.     std::cout << " 老鼠,我的叫声是吱吱吱!" << std::endl;  
  11. }  
  12.   
  13. void Cat::cry()  
  14. {  
  15.     std::cout << " 我叫" << name << ",是一只猫,我的叫声是:喵喵喵!" << std::endl;  
  16. }  
  17.   
  18. void Dog::cry()  
  19. {  
  20.     std::cout << " 我叫" << name << ",是一条狗,我的叫声是:汪汪汪!" << std::endl;  
  21. }  
  22.   
  23. void Giraffe::cry()  
  24. {  
  25.     std::cout << " 我叫" << name << ",是";  
  26.     if(sex == 'm')  
  27.         std::cout << " 男";  
  28.     else  
  29.         std::cout << " 女";  
  30.     std::cout << " 长颈鹿,我的脖子太长,发不出声音来!" << std::endl;  
  31. }  
main.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "Animal.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     Animal *p;  
  8.     p = new Animal();  
  9.     p->cry();  
  10.     Mouse m1("Jerry",'m');  
  11.     p = &m1;  
  12.     p->cry();  
  13.     Mouse m2("Jemmy",'f');  
  14.     p = &m2;  
  15.     p->cry();  
  16.     Cat c1("Tom");  
  17.     p = &c1;  
  18.     p->cry();  
  19.     Dog d1("Droopy");  
  20.     p = &d1;  
  21.     p->cry();  
  22.     Giraffe g1("Gill",'m');  
  23.     p = &g1;  
  24.     p->cry();  
  25.     return 0;  
  26. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的菜单程序,可以对动物园的动物进行管理。代码如下: ```python class Animal: def __init__(self, id, animal_type, color, sex, price, buy_date, is_dead=False): self.id = id self.type = animal_type self.color = color self.sex = sex self.price = price self.buy_date = buy_date self.is_dead = is_dead animal_list = [] def add_animal(): id = int(input("请输入动物的ID:")) for animal in animal_list: if animal.id == id: print("该ID已存在,请重新输入!") return animal_type = input("请输入动物的种类:") color = input("请输入动物的颜色:") sex = input("请输入动物的性别:") price = float(input("请输入动物的价格:")) buy_date = input("请输入动物的入园时间(格式:YYYY-MM-DD):") animal = Animal(id, animal_type, color, sex, price, buy_date) animal_list.append(animal) print("添加成功!") def delete_animal(): id = int(input("请输入要删除动物的ID:")) for animal in animal_list: if animal.id == id: animal.is_dead = True print("删除成功!") return print("未找到该动物!") def show_all_animals(): print("ID\t种类\t颜色\t性别\t价格\t入园时间") for animal in animal_list: if animal.is_dead == False: print("{}\t{}\t{}\t{}\t{}\t{}".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buy_date)) def show_animals_by_color(): color = input("请输入要查询的动物的颜色:") print("ID\t种类\t颜色\t性别\t价格\t入园时间") for animal in animal_list: if animal.color == color and animal.is_dead == False: print("{}\t{}\t{}\t{}\t{}\t{}".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buy_date)) def count_animals_by_type(): animal_type = input("请输入要统计的动物种类:") count = 0 for animal in animal_list: if animal.type == animal_type and animal.is_dead == False: count += 1 print("共有{}只{}。".format(count, animal_type)) def count_animals_value_by_type(): animal_type = input("请输入要统计的动物种类:") value = 0 for animal in animal_list: if animal.type == animal_type and animal.is_dead == False: value += animal.price print("{}的总价值为{}元。".format(animal_type, value)) def modify_animal_info(): id = int(input("请输入要修改的动物的ID:")) for animal in animal_list: if animal.id == id: animal_type = input("请输入动物的种类:") color = input("请输入动物的颜色:") sex = input("请输入动物的性别:") price = float(input("请输入动物的价格:")) buy_date = input("请输入动物的入园时间(格式:YYYY-MM-DD):") animal.type = animal_type animal.color = color animal.sex = sex animal.price = price animal.buy_date = buy_date print("修改成功!") return print("未找到该动物!") while True: print("1、添加动物信息进动物园") print("2、注销一条动物信息(死亡或卖掉)") print("3、查询全部动物信息") print("4、查询某种颜色的动物信息") print("5、统计某种类型的动物信息") print("6、统计某种类型的动物的价值") print("7、修改某个动物的基本信息(输入id)") print("8、退出") choice = input("请输入要执行的操作:") if choice == "1": add_animal() elif choice == "2": delete_animal() elif choice == "3": show_all_animals() elif choice == "4": show_animals_by_color() elif choice == "5": count_animals_by_type() elif choice == "6": count_animals_value_by_type() elif choice == "7": modify_animal_info() elif choice == "8": break else: print("输入有误,请重新输入!") ``` 这个程序可以实现菜单中的所有功能,你可以根据需要进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值