Write a class called Zombie. It should have an instance (normal) data member of
ID: 3594475 • Letter: W
Question
Write a class called Zombie. It should have an instance (normal) data member of type string to hold the name of a Zombie. It should have a constructor that takes a string parameter and uses it to initialize the name. It should have a method called speak() that prints (if the Zombie is named Kevin), "Kevin says Braaaainss!" (use whatever the Zombie's actual name is). It should have a static data member 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.
Explanation / Answer
Here is your program,
#include <iostream.h>
class Zombie
{
string name;
static int cnt=0; //static variable to count number of instances of class
Zombie(string x)
{
name=x;
cnt++; //increment cnt
}
void Speak(string y)
{
cout<< y <<"says Braaaainss!" << endl;
}
~Zombie()
{
cout<< "zombie is destroyed";
cnt--; //decrement cnt
}
};
int main()
{
int ch;
string x;
Zombie* a=null
while(1)
{
cout<<"***********MENU***********" <<endl;
cout<<"1.create new zombie 2.destroy a zombie 3.print number of zombies 4.tell zombies to speak 5.quit ";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter name of zombie";
cin>>x;
a=new Zombie(x);
cout<<"Zombie created";
break;
case 2:
deletre a[cnt];
cout<<"zombie is deleted";
break;
case 3:
cout<<"Total zombies are"<<Zombie.cnt<<endl;
break;
case 4:
cout<<"Enter name of zombie";
cin>>x;
Zombie.speak(x);
break;
case 5:
exit(0);
default: cout<<" Wrong selection!!! Try again!!!";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.