using c++ language divide program to main.cpp,hunter.h,hunter.cpp need use \"g++
ID: 3742642 • Letter: U
Question
using c++ language
divide program to main.cpp,hunter.h,hunter.cpp
need use "g++ -o count hunter.cpp main.cpp "in terminal to compile
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.
Explanation / Answer
//Animal class not given so just written to test hunter class
//animal.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class animal
{
public:
animal(string name);
void set_name(string str);
string get_name() const;
private:
string name;
};
----------------------------------------------------------------
//animal.cpp
#include"animal.h"
animal::animal(string str)
{
name = str;
}
void animal::set_name(string str)
{
name = str;
}
string animal::get_name() const
{
return name;
}
---------------------------------------------------
//hunter.h
#pragma once
#include"animal.h"
#include<vector>
class hunter : public animal
{
public:
hunter(string aSpecies);
void record_kill(string kill);
int numerOfKills();
vector<string> get_kills();
private:
vector<string> kills;
int noOfKills;
};
-------------------------------------------------
//hunter.cpp
#include"hunter.h"
hunter::hunter(string aSpecies): animal(aSpecies)
{
noOfKills = 0;
}
void hunter::record_kill(string kill)
{
//add to vector of kills this kill passed to method record_kill
kills.push_back(kill);
noOfKills++;
}
int hunter::numerOfKills()
{
return noOfKills;
}
vector<string> hunter::get_kills()
{
return kills;
}
-------------------------------------------------
//main.cpp
#include<iostream>
#include"hunter.h"
using namespace std;
int main()
{
hunter hunter1("Cheetah");
//record kills
hunter1.record_kill("Mouse");
hunter1.record_kill("Gazelle");
hunter1.record_kill("Gazelle");
hunter1.record_kill("Hyena");
hunter1.record_kill("Rabbit");
hunter1.record_kill("Rabbit");
//get the vector for the kills
vector<string> kills = hunter1.get_kills();
//now print all the kills by hunter
cout << "Hunter is : " << hunter1.get_name() << endl;
cout << "Kills by this hunter are : ";
for (int i = 0; i < kills.size(); i++)
{
cout << kills[i] << " ";
}
cout << endl;
}
/*output
Hunter is : Cheetah
Kills by this hunter are : Mouse Gazelle Gazelle Hyena Rabbit Rabbit
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.