I have a programming assignment involving inheritance that I need some assistanc
ID: 3842604 • 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.
Txt 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 type;
} EntityType;
typedef struct {
std::string name;
} EntityName;
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;
}
std::ostream& operator<<(std::ostream& o,EntityType t) {
o << "type:" << t.type << std::endl;
return o;
}
class Actor {
protected:
VitalStruct vitals;
InvBag inventory;
EntityType type;
EntityName name;
public:
VitalStruct get_vitals();
InvBag get_inventory();
EntityType set_type(EntityType& charType);
EntityType get_type();
EntityName set_name(const EntityName&);
EntityName 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;
}
EntityType Actor::get_type() {
return type;
}
EntityType Actor::set_type(EntityType& charType) {
type = charType;
}
EntityName Actor::get_name() {
return name;
}
EntityName Actor::set_name(const EntityName& charName) {
//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;
EntityType type;
EntityName name;
std::string charType;
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") {
charType = type;
actormap[name] = new Hero();
actormap[name]->set_type(charType);
std::cout << actormap[name]->get_type();
//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
Error - At least one public class is required in main file.
In athe above programme there is no any public class.so to execute this programme at least ane public class is required.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.