猫和老鼠

/*
作业:
“四支老鼠抬花轿,一支老鼠放鞭炮,前面两只当鼓手,咚隆咚隆真热闹,这时老猫来贺喜,一只一只全吃掉。”。
这首儿歌描述了老鼠婚嫁的场面。完成下面两个问题:

(1)就这段描述,找出所有可能对象,用C++定义相应的类
(2)在main函数中,用你定义的类产生对象,并用这些对象尽可能地模拟出儿歌中描述的场面。
*/


/*main.cpp*/
#include"Mouse.h"
#include"Cat.h"
#define NUMBER_OF_MOUSE 8
#include<iostream>
using namespace std;

int main()
{    
    Mouse *mouse;
    Mouse *mouse=new Mouse[NUMBER_OF_MOUSE];
    Cat cat;
    for (int i=0; i<4;i++){
         mouse[i].Rasing();
   }
    mouse[4].Fire();
    mouse[5].Drum();
    mouse[6].Drum();
    mouse[6].Set();
    for (int i=0; i<NUMBER_OF_MOUSE; i++){
        cat.eatMouse(mouse[i]);
   }
return 0;
}


/*Animal.h*/
#ifndef _ANIMAL_DEAD_H
#define _ANIMAL_DEAD_H
#include <string.h>
#include<iostream>
using namespace std;
class Animal
{
public:
    Animal(const string &name="Animal");
    //~Animal();
protected:
    string name;
    
};
#endif

/*Animal.cpp*/
#include "Animal.h"
Animal::Animal(const string &name){
    this->name=name;
}

/*Cat.h*/
#ifndef CAT_HEAD_H
#define CAT_HEAD_H
#include"Mouse.h"
#include"Animal.h"
#include<iostream>
using namespace std;
class Cat:public Animal
{
public:

       Cat(const string &name="cat");
       void eatMouse(Mouse m);
        string getName();
};
#endif

/*Cat.cpp*/
#include "Cat.h"
Cat::Cat(const string &name){
    this->name=name;
}
void Cat::eatMouse(Mouse m){
    cout<< this->name<<"  eating   "<<m.getName()<<endl;
}
string Cat::getName(){
    return this->name;
}

/*Mouse.h*/
#ifndef _MOUSE_HEAD_H
#define _MOUSE_HEAD_H

#include<iostream>
using namespace std;
#include "Animal.h"
class Mouse:public Animal
{
public:
      Mouse(const string &name="mouse");
       void Rasing();
       void Fire();
       void Drum(); 
       void Set();
       string getName();
};
#endif

/*Mouse.cpp*/
#include"Mouse.h"
Mouse::Mouse(const string &name){
    this->name=name;
}
void Mouse::Rasing(){
cout<<this->getName()<<"  Rasing()"<<endl;
}
void Mouse::Fire(){
    cout<<this->getName()<<"  Fire()"<<endl;
}
void Mouse::Drum(){
    cout<<this->getName()<<"  Drum()"<<endl;
}
void Mouse::Set(){
    cout<<this->getName()<<"  Set()"<<endl;
}
string Mouse::getName(){
    return this->name;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值