#include <iostream>
using namespace std;
class Item
{
private:
string name;
int power;
int durability;
public:
Item(string n, int p, int d);
void Describe() const;
~Item();
};
Item::Item(string n, int p, int d)
{
name = n; power = p; durability = d;
}
I'm also have trouble display this function... How do i call it?
void Item::Describe() const
{
cout << name << " (power=" << power << ", durability=" << durability << ")\n";
}
Item::~Item()
{
cout << "** Item " << name << " is being deallocated." << endl;
}
class Warrior
{
private:
string name;
int level;
string profession;
Item *tool;
public:
Warrior(string n, int l, string p);
Warrior(const Warrior& otherObj);
void GiveTool(string toolName, int toolPower, int toolDurability);
void Describe() const;
};
Warrior::Warrior(string n, int l, string p)
{
name = n;
level = l;
profession = p;
}
Warrior::Warrior(const Warrior& otherObj)
{
if(otherObj.tool != NULL)
this->tool = new Item(*(otherObj.tool));
else
this->tool = NULL;
}
The problem seems to be here i think... So here's what i want to do.
If tool is NULL meaning the warrior has no tool give him a tool. However if he does have a tool, deallocate the tool variable and give him this tool instead.
void Warrior::GiveTool(string toolName, int toolPower, int toolDurability)
{
if(tool == NULL)
this->tool = new Item(toolName,toolPower,toolDurability);
else
{
cout << name << "'s existing tool is being replaced." << endl;
delete tool;
this->tool = new Item(toolName,toolPower,toolDurability);
}
}
Then how would i display that newly allocated tool... would it just be "tool" like how i did here? Because when i run the program it would display the address not the memory.
void Warrior::Describe() const
{
cout << name << " is a level " << level << " " << profession << endl;
if(tool != NULL)
{
cout << "His tool is: ";
cout <<tool;
cout << "....";
}
else
{
cout << "No tool\n";
}
}
int main()
{
Warrior a("Stephen Curry", 30, "NBA Player");
a.GiveTool("Basketball", 50, 20);
a.Describe();
a.GiveTool("Football", 10, 20);
a.Describe();
}
The output should look like this i think:
Stephen Curry is a level 30 NBA Player
His tool is: Bastketball
Stephen Curry's existing tool is being replaced.
Item Basketball is being deallocated.
Stephen Curry is a level 30 NBA Player
His tool is: Football
Thank you in advance! Also ANYTHING will help. I'm very new to this programming world, keep that in mind when reading my code... Once again any help is appreciated Thanks!