I'm in computer science and my teacher want's us to make a Knight jousting game but he wants us to have a weapon class and a knight class. In the knight class we'll have in the public a Knight which takes five arguments; name, stamina, weapon type, hit chance, and stamina required for the weapon. The last three though aren't private data in the Knight class. They're from the private data of the weapon class and I have everything that has to do with taking in the input from the user about all of this just fine. But now I have to make it so when the knight's joust it reduces the knights stamina because of the weapon, and if the knight gets hit he falls off his horse. Everything was going fine for me but then I noticed how do I subtract the StaminaRequired from the Knight_Stamina. Because it's private Data in the weapon class I have.
//knight.h
#include<string>
#include"weapon.h"
usingnamespace std;
class Knight
{
public:
Knight(string Knightname, int KnightStamina, string type, int HitChance, int StaminaRequired);
void display();
void unHorse_yourself();
void joust();
private:
string KnightName;
int Knight_Stamina;
bool on_horse;
Weapon stats;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//weapon.cpp
#include"weapon.h"
#include<iostream>
#include<string>
usingnamespace std;
Weapon::Weapon(string type, int HitChance, int StaminaRequired)
{
Type = type;
Hit_Chance = HitChance;
Stamina_Required = StaminaRequired;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//weapon.h
#include<string>
usingnamespace std;
class Weapon
{
public:
Weapon(string type, int HitChance, int StaminaRequired);
private:
string Type;
int Hit_Chance;
int Stamina_Required;
};
So I guess the main problem is that you can't modify Stamina_Required directly from Knight's object? In that case you might wish to change the accessibility of Stamina_Required in Knight class by either access declaration of Stamina_Required to make it public again (Weapon::Stamina_Required ) or use using Weapon::Stamina_Required to restore access (The 2nd method is preferred).
It's okay The illusionist mirage, I'll give both of your answers a try and see if they work! I can't create though a stamina_required member function though, my teacher said he wants just one member function in the weapon class with the 3 arguments.
Apart from remaking your class structure to accommodate this (the best way), you can also delcare that Knight is a friend of Weapon. Alternately, you say that you are allowed one member function. Is a constructor a member function (according to your teacher)? Often it isn't, and this makes sense in this concept because something that is just a constructor with three variables is fairly pointless...