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

I have a programming assignment involving inheritance that I need some assistanc

ID: 3842170 • Letter: I

Question

I have a programming assignment involving inheritance that I need some assistance with. It is separated into three parts.

1. Add a member function and member data attribute to the Actor base type that will name the object and also store the type of the object.

2. Add a function to the base Actor type that would be common to all of the subtypes and implement it in the main function.

3. Add sub type functions to the Sub types of the Actor base type.

I need help completing this assignment, as I can't even get my get_name and set_name function to work. Any assistance would be greatly appreciated.


This particular program uses an input file for naming creatures and the main file for executing all of their functions.

Text File:


trundle:Monster
tim:Hero
penelope:Villager

CPP File:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>

void tokenize(const std::string& str,
std::vector<std::string>& tokens,
const std::string& delimiters = " ") {
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);

while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}


class Actor;
class InvItem;

typedef std::map<std::string,Actor*> ActorMap;
typedef std::vector<InvItem> InvBag;
typedef std::vector<std::string> TokenVector;

std::ostream& operator<<(std::ostream& o,TokenVector v) {
for(int i=0; i<v.size(); i++)
o << v[i] << " ";
return o;
}

typedef struct {
int health,manna,stamina,intel;

} VitalStruct;

typedef struct {
std::string name, type;
  
} EntityInfo;

std::ostream& operator<<(std::ostream& o,VitalStruct v) {
o << "health:" << v.health << std::endl;
o << "manna:" << v.manna << std::endl;
o << "stamina:" << v.stamina << std::endl;
o << "intel:" << v.intel << std::endl;
return o;
}

class Actor {
protected:
VitalStruct vitals;
InvBag inventory;
EntityInfo info;
  
public:
VitalStruct get_vitals();
InvBag get_inventory();
  
EntityInfo set_type(const EntityInfo&);
EntityInfo get_type();
EntityInfo set_name(const EntityInfo&);
EntityInfo get_name();
  
void set_inventory(const InvBag&);
virtual void speak(const std::string &message)=0;
};

class InvItem {

};

InvBag& operator<<(InvBag& bag,InvItem iv) {
bag.push_back(iv);
return bag;
}

EntityInfo Actor::get_type() {
return info;
}

EntityInfo Actor::set_type(const EntityInfo& attribute) {
//fill this
}
EntityInfo Actor::get_name() {
return info;
}

EntityInfo Actor::set_name(const EntityInfo& attribute) {
//fill this
}

VitalStruct Actor::get_vitals() {
return vitals;
}

void Actor::set_inventory(const InvBag& ivbag) {
inventory = ivbag;
}

InvBag Actor::get_inventory() {
return inventory;
}

class Monster : public Actor {
public:
void speak(const std::string& message);
};

void Monster::speak(const std::string& message) {
std::cout << "Monster: " << message << std::endl;
}

class Hero : public Actor {
void speak(const std::string& message);
};

void Hero::speak(const std::string& message) {
std::cout << "Hero: " << message << std::endl;
}

class Villager : public Actor {
void speak(const std::string& message);
};

void Villager::speak(const std::string& message) {
std::cout << "Villager: " << message << std::endl;
  
}

int main() {
ActorMap actormap;
InvBag bag;
EntityInfo type;
EntityInfo name;

  
std::ifstream in_file("./characters.txt");
if(in_file.fail()) {
std::cout << "Cannot open file";
exit(1);
}
std::string buf="";
while(!in_file.eof()) {
std::getline(in_file,buf);
if(buf.at(0) == '-') {
break;
}
  
TokenVector tokens;
tokenize(buf,tokens,":");
  
std::string name = tokens[0];
std::string type = tokens[1];
  
if(type == "Hero") {
actormap[name] = new Hero();
//set name
//set type
}
else if(type == "Monster") {
actormap[name] = new Monster();
}
else if(type == "Villager") {
actormap[name] = new Villager();
}
actormap[name]->speak("I speak");
actormap[name]->get_name();
}
/*

actormap["trundle"] = new Monster();
actormap["tim"] = new Hero();
actormap["penelope"] = new Villager();
  
bag << InvItem();
actormap["trundle"]->set_inventory(bag);

std::cout << actormap["trundle"]->get_vitals();

actormap["trundle"]->speak("I am trundle, prepare to meet your maker!");
actormap["penelope"]->speak("Where's my hero?");
actormap["tim"]->speak("Let's roll!");
*/
  
return 0;
}

Explanation / Answer

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>

void tokenize(const std::string& str,
std::vector<std::string>& tokens,
const std::string& delimiters = " ") {
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimiters, lastPos);

while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}


class Actor;
class InvItem;

typedef std::map<std::string,Actor*> ActorMap;
typedef std::vector<InvItem> InvBag;
typedef std::vector<std::string> TokenVector;

std::ostream& operator<<(std::ostream& o,TokenVector v) {
for(int i=0; i<v.size(); i++)
o << v[i] << " ";
return o;
}

typedef struct {
int health,manna,stamina,intel;

} VitalStruct;

typedef struct {
std::string name, type;
  
} EntityInfo;

std::ostream& operator<<(std::ostream& o,VitalStruct v) {
o << "health:" << v.health << std::endl;
o << "manna:" << v.manna << std::endl;
o << "stamina:" << v.stamina << std::endl;
o << "intel:" << v.intel << std::endl;
return o;
}

class Actor {
protected:
VitalStruct vitals;
InvBag inventory;
EntityInfo info;
  
public:
VitalStruct get_vitals();
InvBag get_inventory();
  
EntityInfo set_type(const EntityInfo&);
EntityInfo get_type();
EntityInfo set_name(const EntityInfo&);
EntityInfo get_name();
  
void set_inventory(const InvBag&);
virtual void speak(const std::string &message)=0;
};

class InvItem {

};

InvBag& operator<<(InvBag& bag,InvItem iv) {
bag.push_back(iv);
return bag;
}

EntityInfo Actor::get_type() {
return info;
}

EntityInfo Actor::set_type(const EntityInfo& attribute) {
Actor.info = attribute;
}
EntityInfo Actor::get_name() {
return info;
}

EntityInfo Actor::set_name(const EntityInfo& attribute) {
Actor.entity info -> name = attribute -> name;
}

VitalStruct Actor::get_vitals() {
return vitals;
}

void Actor::set_inventory(const InvBag& ivbag) {
inventory = ivbag;
}

InvBag Actor::get_inventory() {
return inventory;
}

class Monster : public Actor {
public:
void speak(const std::string& message);
};

void Monster::speak(const std::string& message) {
std::cout << "Monster: " << message << std::endl;
}

class Hero : public Actor {
void speak(const std::string& message);
};

void Hero::speak(const std::string& message) {
std::cout << "Hero: " << message << std::endl;
}

class Villager : public Actor {
void speak(const std::string& message);
};

void Villager::speak(const std::string& message) {
std::cout << "Villager: " << message << std::endl;
  
}

int main() {
ActorMap actormap;
InvBag bag;
EntityInfo type;
EntityInfo name;

  
std::ifstream in_file("./characters.txt");
if(in_file.fail()) {
std::cout << "Cannot open file";
exit(1);
}
std::string buf="";
while(!in_file.eof()) {
std::getline(in_file,buf);
if(buf.at(0) == '-') {
break;
}
  
TokenVector tokens;
tokenize(buf,tokens,":");
  
std::string name = tokens[0];
std::string type = tokens[1];
  
if(type == "Hero") {
actormap[name] = new Hero();
//set name
//set type
}
else if(type == "Monster") {
actormap[name] = new Monster();
}
else if(type == "Villager") {
actormap[name] = new Villager();
}
actormap[name]->speak("I speak");
actormap[name]->get_name();
}
/*

actormap["trundle"] = new Monster();
actormap["tim"] = new Hero();
actormap["penelope"] = new Villager();
  
bag << InvItem();
actormap["trundle"]->set_inventory(bag);

std::cout << actormap["trundle"]->get_vitals();

actormap["trundle"]->speak("I am trundle, prepare to meet your maker!");
actormap["penelope"]->speak("Where's my hero?");
actormap["tim"]->speak("Let's roll!");
*/
  
return 0;
}

// I have corrected the mistakes now it should work