Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define and implement a class named hunter . A hunter object represents an animal

ID: 3743074 • Letter: D

Question

Define and implement a class named hunter. A hunter object represents an animal that hunts for its food in the wild. The hunter class must be defined by inheriting from the animal class. The hunter class has the following public constructors and behaviours:

Your main program should create a hunter object to represent a "Cheetah" and record kills of 1 "Mouse", 2 "Gazelle", 1 "Hyena" and 2 "Rabbit". The hunter object initially has no kills. Your main program must print all the kills recorded by the hunter object.

I've succeeded using the vectors route, could someone post the code for the pointers route?

Explanation / Answer

Hello, I have answered the previous part of this question (animal class), so I’m making the use of that class to complete implementation of hunter.h and hunter.cpp files. Implemented get_kills() method to return an array pointer since you mentioned that you have completed the vector returning method. Here I’m just returning a pointer to the array containing elements in vector, if you want me to use an array instead of vector, please drop a comment. But it’ll require a bit more work. Thanks.

//hunter.h

#include "animal.h"

#include<vector>

class hunter : public animal{

                             //vector to store kills

                             vector<string> kills;

              public:

                             //constructor

                             hunter(string species);

                             //method to add a kill to the record

                             void record_kill(string kill);

                             //returns the number of kills

                             int numberOfKills();

                             //returns a pointer to the array containing kills

                             string *get_kills();

};

//hunter.cpp

#include "hunter.h"

//constructor, passing species to the super class constructor

hunter::hunter(string species) : animal(species){

             

}

//other method implementations

void hunter::record_kill(string kill){

              kills.push_back(kill);

}

int hunter::numberOfKills(){

              return kills.size();

}

string * hunter::get_kills(){

              //getting a pointer to the contents of kills vector as an array

              string *kills_array_pointer=kills.data();

              return kills_array_pointer;

}

//main.cpp

#include "hunter.h"

int main(){

              //creating a hunter object

              hunter cheetah("Cheetah");

              //recording kills of various animals

              cheetah.record_kill("Mouse");

              cheetah.record_kill("Gazelle");

              cheetah.record_kill("Gazelle");

              cheetah.record_kill("Hyena");

              cheetah.record_kill("Rabbit");

              cheetah.record_kill("Rabbit");

              //displaying number of kills

              cout<<"Number of kills: "<<cheetah.numberOfKills()<<endl;

              cout<<"Kills:"<<endl;

              //getting all kills

              string *kills=cheetah.get_kills();

              //displaying all kills

              for(int i=0;i<cheetah.numberOfKills();i++){

                             cout<<kills[i];

                             if(i!=cheetah.numberOfKills()-1){

                                           cout<<", ";

                             }else{

                                           cout<<endl;

                             }

              }

              return 0;

}

/*OUTPUT*/

Number of kills: 6

Kills:

Mouse, Gazelle, Gazelle, Hyena, Rabbit, Rabbit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote