Accessing private Data from one class in another class?

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.

here's my different codes


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//knight.cpp
#include"Knight.h"
#include<iostream>

using namespace std;

//Knight1 name
Knight::Knight(string Knightname, int KnightStamina, string type, int HitChance, int StaminaRequired)
: stats(type, HitChance, StaminaRequired), KnightName(Knightname), Knight_Stamina(KnightStamina), on_horse(true)
{
	
}

void Knight::unHorse_yourself()
{

}

void Knight::joust()
{
Knight_Stamina = Knight_Stamina - StaminaRequired;
}

void Knight::display()
{

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//knight.h
#include<string>
#include"weapon.h"

using namespace 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>

using namespace 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>

using namespace std;

class Weapon
{
	public:
		Weapon(string type, int HitChance, int StaminaRequired);
	private:
		string Type;
		int Hit_Chance;
		int Stamina_Required;

};
Last edited on
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).
Getter functions?

i.e
1
2
3
4
5
6

int Weapon::getStamina_Required()
{
    return Stamina_Required;
}


That will return a copy, so you still won't be able to use it to actually change Stamina_Required

Or maybe he can create a mutator function

1
2
3
4
int Weapon::setStamina_Required(int change)
{
    Stamina_Required += change;
}



And sorry liquifiednate, I made a mistake in my previous answer, You didn't inherit Weapon so access specifier will not work i think.
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...
so I figured out my problem and fixed it. This is what I did

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool Knight::Attack()
{
	if (on_horse == true)
	{
			Knight_Stamina = Knight_Stamina - stats.GetStamina();
	}

	if(stats.did_you_hit() == true)
	{
		on_horse = false;
	}
	
	
}
Topic archived. No new replies allowed.