How to return a loop from a function.

Hello

I have this code:



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
#include <iostream>

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

class CPlayerStats
{

	public:

		// Constructor
		explicit CPlayerStats(double mh = 1, double mm = 1, double ms = 1): m_health(mh), m_magic(mm), m_stamina(ms){}
		
		// Volume Function to count volume of stats.
		double Volume()
		{
		return m_health*m_magic*m_stamina;
		}

	private:
		double m_health;
		double m_magic;
		double m_stamina;

};


int main()
{

	CPlayerStats Party[5];
	CPlayerStats Group(3312.0, 1455.0, 2244.0);

		
		
		for(int i = 0;i < 5;i++) cout << endl << "Array current number: " << i << " . Health: " << Party[i].m_health << ", Magic: " << Party[i].m_magic << ", Stamina: " << Party[i].m_stamina << endl;
		
	
	cin.get();
	return 0;

}


Now it does not work because the members in class are private. If i made them public this would work. I can fix this by making a inline function inside the class and use that in the main to call for the for loop.

The problem is i dont know how to "return" the for loop result from a function or is it even possible. So please could someone explain this to me?

Thank You
Public member functions can return the value of private members of a class. So you would have a function like "CPlayerStats::GetHealth()" which would return the value of the 'm_health' data for that object.


#include <iostream>

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

class CPlayerStats
{

public:

// Constructor
explicit CPlayerStats(double mh = 1, double mm = 1, double ms = 1): m_health(mh), m_magic(mm), m_stamina(ms){}

// Volume Function to count volume of stats.
double Volume()
{
return m_health*m_magic*m_stamina;
}

double get_health() const
{
return m_health;
}

double get_magic() const
{
return m_magic;
}

double get_stamina() const
{
return m_stamina;
}

private:
double m_health;
double m_magic;
double m_stamina;

};


int main()
{

CPlayerStats Party[5];
CPlayerStats Group(3312.0, 1455.0, 2244.0);



for(int i = 0;i < 5;i++) cout << endl << "Array current number: " << i << " . Health: " << Party[i].get_health() << ", Magic: " << Party[i].get_magic() << ", Stamina: " << Party[i].get_stamina() << endl;


cin.get();
return 0;

}

UPDATE!! This works =)

Thank you
health, magic, and stamina are integers :)

And what about the max values? :)
Last edited on
Topic archived. No new replies allowed.