“四支老鼠抬花轿,一支老鼠放鞭炮,前面两只当鼓手,咚隆咚隆真热闹,这时老猫来贺喜,一只一只全吃掉。”。
这首儿歌描述了老鼠婚嫁的场面。完成下面两个问题:
(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;
}