Trying to understand how to access objects members from functions

Jul 23, 2018 at 5:38pm
Hi all, my first post here!

So, I'm pretty new to c++ and still getting to grips with some of the basics. I've been trying to read as much stuff before asking on a forum, but I think I'm missing something - probably simple.

My problem at the moment is figuring out how to access class members of one object, that is part of another object.
I'm writing a very simple rpg style text game. I've made a Player class and a Weapon class, and have made one of the Player members a Weapon object.
I then create a few Weapon objects that the player could choose from.

This is where I'm getting stuck. I want to calculate the total player damage using the Player's strength member as a modifier, and the player's current Weapon damage member, regardless of which weapon the player chooses, but I don't know how to bring all the data to same place to do it. Am I making it more complicated than it needs to be?

Thanks in advance! :D

Here's what I've got so far
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

#include "stdafx.h"
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;


class Weapon
{
	friend class Player;

	string m_name;
	int m_damage;

public:
	Weapon(string, int);

	string GetName() { return m_name; };
	int GetDamage() { return m_damage; };

	void SetName(string name) { m_name = name; };
	void SetDamage(int damage) { m_damage = damage; };
};

Weapon::Weapon(
	string name = "",
	int damage = 0)
{
	SetName(name);
	SetDamage(damage);
}

class Player
{

	string m_name;
	Weapon m_weapon;
	int m_strength;
	
public:

	friend class Weapon;

	Player(string,			//name
		Weapon,				//weapon
		int);				//strength
	
	string GetName() { return m_name; };
	Weapon GetWeapon() { return m_weapon; };
	int GetStrength() { return m_strength; };
	
	void SetName(string name) { m_name = name; };
	void SetWeapon(Weapon weapon) { m_weapon = weapon; };
	void SetStrength(int strength) { m_strength = strength; };

	int Attack()
	{					//Better to calc and return damage here?
		return 0;
	}
};

Player::Player(
	string name,
	Weapon weapon,
	int strength)
{
	SetName(name);
	SetWeapon(weapon);
	SetStrength(strength);
}


void Battle(Player);

int main()
{
	Weapon none("None", 0);       //(name, damage)
	Weapon sword("Sword", 10);
	Weapon axe("Axe", 20);
	Player playerOne("Conan", none, 2);         //(name, weapon, strength)

	cout << "Choose weapon\n1 Sword\n2 Axe" << endl;
	int choice;
	cin >> choice;
	switch (choice)
	{
	case 1:
		cout << "You take the Sword" << endl;
		playerOne.SetWeapon(sword);
		Battle(playerOne);
		break;
	case 2:
		cout << "You take the Axe" << endl;
		playerOne.SetWeapon(axe);
		Battle(playerOne);
		break;
	default:
		cout << "Choose 1 or 2" << endl;
		break;
	}

	return 0;
}

void Battle(Player)
{
	bool playerAlive = true;
	cout << "You are fighting a skeleton\n" << endl;;
	do
	{
		cout << "1 Attack\n2 Run\n" << endl;
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1:
                cout << "You attack!\n" << endl;
               //function to go here		
//how to access both classes and call their functions from here?
//Needs to get damage member of Weapon and player strength
//to calculate total damage, something like
//(((PlayerStrength()*10)+100)*WeaponDamage())/100;

                        int pause;
			cin >> pause;
			break;
		case 2:
			break;
		default:
			break;
		}
	} while (playerAlive = true);
}


Edit: Typo
Last edited on Jul 23, 2018 at 5:52pm
Jul 23, 2018 at 8:31pm
1
2
3
4
void Battle(Player foo)
{
  int bar = foo.GetWeapon().GetDamage();
}

Jul 23, 2018 at 10:10pm
Ah ha! I can't believe it was that simple! Thanks very much :)
Topic archived. No new replies allowed.