Pulling values from objects.

Hello, I've got three objects that I've overloaded an operator(+) to add together. I've got two horses, their scores, and their times in minute and seconds. I'm wondering once I add these into one object, how do I pull the values out to average them, and then show who has the highest score/lowest time?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	//Horse1's objects
	RaceData horse1Data1("Peter Pan", 91, 7, 25);
	RaceData horse1Data2("Peter Pan", 109, 6, 41);
	RaceData horse1Data3("Peter Pan", 75, 11, 21);

	RaceData horse1DataX = horse1Data1 + horse1Data2 + horse1Data3;

	//What do I put here to get the average scores, average time?

	//Horse2's objects
	RaceData horse2Data1("Tinkerbell", 145, 9, 11);
	RaceData horse2Data2("Tinkerbell", 119, 10, 15);
	RaceData horse2Data3("Tinkerbell", 46, 11, 55);

	RaceData horse2DataX = horse2Data1 + horse2Data2 + horse2Data3;

	//What do I put here to get a separate average score/average time
        //that I can compare with the other one?


	return 0;
}


I'm pretty sure I know how to do everything else, I just do not know how to get the values out of the objects the correct way.
Last edited on
Do you just overlook the name of the horse in operator+ ?
1
2
3
4
5
6
struct RaceData{
  int score, h, min;
//...
};
std::map<std::string, RaceData> gran_prix;
granprix["Peter Pan"] += lap1;
Then you could overload the operator< to find the highest score.
Maybe you want to store the information of every lap, instead of summing everything. Maybe it should be a responsibility of RaceData to output its average.

Or you can just make all your variables public, let the program (not the class) to handle the information and make decision in base at the state of your objects.
some things to consider ne555, thank you.

I am making progress, I was able to get the values I wanted by simplifying the getter statements I was using, I'm trying to do this in the simplest way possible. Now though I'm thinking about adding the laps like you suggested. Also, yes, I was overlooking the name because it stays the same, I figured if it got added it would look like "Peter PanPeter PanPeter Pan" when it came out =)

New question if I wanted to contain the average score calc in its own function, like:

1
2
3
4
void RaceData::printAvgScore()
{
	cout << fixed << setprecision(2) << horse1DataX.getScore()/3;
}


what would I have to do to get rid of the error it gives me that horse1DataX is undefined?

"Error: identifier horse1DataX is undefined"

Last edited on
That's an issue of scope. horse1DataX is created in main(), and doesn't exist in that function. Even if you created an object called horse1DataX in that function, it would be another one separate from the one in main.

If you write a class function RacingData::printAvgScore(), you're saying that every RacingData object can call that function. It doesn't really make sense for individual horses to be able to get an 'average' score. If you've overloaded the operators to add their scores, you can just write horse1DataX.getScore()/3 in main().

I don't think it's a good idea to use an operator overload like this, though. It would be more straightforward to just write something like float avgtime = ( horse1.getTime() + horse2.getTime() + horse3.getTime() ) / 3.0;
Yes, what I had in originally was the horse1DataX.getScore()/3, and that worked good, was only trying to modulate a little bit more. Thank you very much for the input.
Sorry, I though that you want the average of every horse to complete a lap.
1
2
3
4
void RacingData::printAvgScore()
{
	cout << fixed << setprecision(2) << this->getScore()/3.0; //the this could be omited
}

Also, yes, I was overlooking the name because it stays the same, I figured if it got added it would look like "Peter PanPeter PanPeter Pan" when it came out =)
I meant this
1
2
3
RaceData PP("Peter Pan", 91, 7, 25);
RaceData TB("Tinkerbell", 145, 9, 11);
RaceData z = PP + TB; //what will be the name of z ? 
Ohhh no, I'm adding up only their 3 times and keeping each horse separate.
Last thing, I can't figure out how to get the seconds when I average the time. The minutes are fairly easy, as the int type will truncate the number for me, I have no idea how to get the seconds though, I have a feeling I need to use a % but idk how. This is how I got the average minutes:

1
2
3
int tempminutes =horse1DataX.getMinutes();
int tempseconds = (tempminutes * 60) + horse1DataX.getSeconds();
int avgTime = (tempseconds/3)/60;


Is there a better way to do it other than all these lines?
ahhh it was as simple as

newseconds = tempseconds % 60;

I also came up with a longer way of using a loop to chop seconds off until it was < 60, but I won't get into that.

thanks for the help anyway, I'm going to mark this solved.
Topic archived. No new replies allowed.