Question about Accessors and mutators (edited)

I am writing a horse race game. I have done a lot of research on accessors and mutators, and I am still confused why I am not getting a number other than the value of constructor I created which is zero. Where am I going wrong? Note distanceTraveled and maxRunningDistPerSecond are private member variables.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Horse.h"
using namespace std;

int Horse::getMaxRun()
{ return maxRunningDistPerSecond; }

int Horse::getRunASecond()
{ return distanceTraveled; }

void Horse::runASecond(int distRun)
{
	srand(time(0));
	distRun = rand() % (2);
	distanceTraveled = distRun;
}
Last edited on
Hi,

There is not enough context for your question. Ideally post some code we can compile.
Here is my Horse class and int main(). Also this project has a header file, and two .cpp files.

my class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class Horse
{
private:
	string name;
	string rider;
	int maxRunningDistPerSecond;
	int distanceTraveled;
	int racesWon;

public:
	Horse()
	{	srand(time(0));
		maxRunningDistPerSecond = rand() % (100) + 1;
		distanceTraveled = 0;
		racesWon = 0; }

	int getMaxRun();
	int getRunASecond();
	void runASecond(int distRun);



};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Horse.h"
using namespace std;

int main()
{
	Horse dist;

	cout << dist.getRunASecond();

	return 0;
}

Last edited on
So with your current code why were you expecting anything other than zero?
Ideally I would like to mutate the private member distanceTraveled by adding a number from 0 to number of maxRunningDistPerSecond to distanceTraveled and setting equal it to distanceTraveled.
Sounds good.
How should introduce a function to change the value of distanceTraveled because I cannot seem to find way do this?
You already have a runASecond function.
So it must be a problem with my getter, or I must be missing something. I wonder what it could be. I am not sure.
... or I must be missing something. I wonder what it could be. I am not sure.


The problem is that you are trolling. I don't know, maybe I am wrong or not sure whether you are taking the piss, or just generally being a pain.
Just need clarification, I am new to the concept of classes. I would never try to troll you, I appreciate your help. My apologies if you took any offense to what I said, it was not intended to be malicious.
Last edited on
Will take a look at my code and determine my problem and get back to all of you if I have any trouble. Thanks for the help.
Topic archived. No new replies allowed.