C Write a class called Zombie It should have a method called
C++
Write a class called Zombie. It should have a method called speak() that prints \"Braaaainss!\". It should have a static variable that keeps track of how many Zombie objects have been created. When a Zombie is created, its constructor will need to increment the value in the static variable. In order to keep the count accurate when Zombies are destroyed, you will need a destructor to decrement the value in the static variable.
In your main function you should declare a vector of Zombie-pointers. You should give the user a menu in a loop. The options should be to: 1) create a new Zombie; 2) destroy a Zombie; 3) print the number of existing Zombies; 4) tell all existing Zombies to speak; or 5) quit. When a user chooses to create a new Zombie, you should dynamically allocate the new object and push its pointer onto the vector. When a user chooses to destroy a Zombie, you should pop the last Zombie pointer off the vector and deallocate the memory for that Zombie.
Solution
Zombie.hpp
#include<iostream>
#ifndef Z_HPP
#define Z_HPP
using namespace std;
class Zombie
{
private:
static int total_zombie;
public:
Zombie(); //Constructor, adds to static variable
~Zombie(); //Destructor, subtracts from the static variable
void speak(); //Repeats what zombies say.
void add();
void del();
int total();
};
#endif
int Zombie::total_zombie = 0;
Zombie::Zombie()
{
add();
}
Zombie::~Zombie()
{
del();
}
void Zombie::speak()
{
cout <<\"Braaaainss!\" <<endl;
}
void Zombie::add()
{
total_zombie++;
}
void Zombie::del()
{
total_zombie--;
}
int Zombie::total()
{
return total_zombie;
}
Zombie.cpp
#include<iostream>
#include <vector>
#include \"Zombie.hpp\"
using namespace std;
void main_menu();
int main()
{
int ch, size;
vector<Zombie*>eater; //Creates a vector of Zombie objects with name eater.
do
{
main_menu();
cin >>ch;
switch(ch)
{
case 1: cout <<\"\ ZOMBIE CREATED!!!!!!\" <<endl;
eater.push_back(new Zombie); //creates new zombie & puts in vector
break;
case 2: if(eater.empty())
cout <<\"\ There is no zombies to kill \" <<endl;
else
{
size =eater.size();
delete eater[size-1];
eater.pop_back();
cout <<\"\ KILLED\" <<endl;
}
break;
case 3: size =eater.size();
cout <<\"\ The number of ZOMBIE: \" <<eater[size-1]->getTotal()<<endl;
break;
case 4: cout <<\"\ CAN YOU HEAR\" <<endl;
size =eater.size();
for (int i = 0; i<size; i++)
eater[i] ->speak();
break;
}
}while (ch !=5);
cout <<\"\ GAME OVER\" <<endl;
return 0;
}
void main_menu()
{
cout <<\"\ MAIN MENU: \" <<endl;
cout <<\"1. Create a Zombie!\" <<endl;
cout <<\"2. Destroy a Zombie!\" <<endl;
cout <<\"3. See total number of Zombies!\" <<endl;
cout <<\"4. Hear Zombies\" <<endl;
cout <<\"5. Quit.\" <<endl;
cout <<\"ch: \";
}



