Quick Classes question..

Hi,

I'm new to C++ and I have a question about classes. I'm trying to make a program from a book I'm working through. It is a Greyhound track where you can bet on the dogs and win money. Anyways...

I have 3 classes, Guy, Greyhound and Bet.

I have an array of Greyhounds called Dogs.

Guy has the following Fields:
public string Name;
public Bet MyBet;
public int Cash;

Greyhound has the following Fields:
public string DogName;
public int StartingPosition;
public int RacetrackLength;
public PictureBox MyPictureBox = null;
public int Location = 0;
public Random Randomiser;

Bet has the following Fields:
public int Amount;
public int Dog;
public Guy Bettor;

At the end of the race I want to output the DogName of the winning dog but I'm unsure how I access the Greyhound class from within the Bet class.

My method is:
public string GetDescription()
{
// Return a string that says who placed the bet, how much
// cash was bet, and which dog he bet on ("Joe bets £8 on dog 4")
// If the amount is zero, no bet was placed ("Joe hasn't placed a bet")
Dog += 1;
return Bettor.Name + " put " + Amount + " on dog No." + Dog;
}

Dog is an INT. I have an array of Greyhounds called Dogs and what I'd like to do is the following:

public string GetDescription()
{
// Return a string that says who placed the bet, how much
// cash was bet, and which dog he bet on ("Joe bets £8 on dog 4")
// If the amount is zero, no bet was placed ("Joe hasn't placed a bet")
Dog += 1;
return Bettor.Name + " put " + Amount + " on " + Dogs[Dog].DogName;
}

But I get the following error:
The name 'Dogs' does not exist in the current context

I understand this because Dogs isn't declared within the class of Bet. Is there any way of getting Dogs to be accessible by other classes? Or is there a better way of achieving this??

Many thanks for any help you can give me...

Cheers

John ;-)
Try to call the constructor of each Betclass with one more array - the array of all dogs. So you can save and use this array in each Bet.

Much better: you call each Bet-constructor with a pointer or a reference to/of the Dogs array. This way you still can use it in Bet after changing it in the Mainclass.
Last edited on
Hi,

Thanks for the reply. I'm not sure I understand fully (please excuse my ignorance as I've just started learning).

Do you mean add a Field to the Bet class like this:
public Array lineUp;

And then in the constructor add this:
MyBet = new Bet() { Bettor = this, Amount = Amount, Dog = Dog, lineUp = Dogs };

???

I'm getting an error when I do that because Dogs doesn't exist in the Guy class (where the MyBet = new Bet() line is...) so would I need to pass the Dogs array into the Guy class as well from the Form that I'm working from?

Sorry if I've completely missed the point.

Thanks again for your help and patience!

Cheers

John ;-)
So you have a Main form in which you've got the array Dogs and you call myGuy = new Guy() or something like that?

And in the Guy class you call myBet = new Bet()?

-------- edit ---------

If yes: you have to forward the array Dogs from Main class to class Bet like adding this array into the cintructors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// normal constructur call with one more variable
myGuy = new Guy(..., Dogs)

...

// contructor of Guy
public Guy(..., Greyhound[] Dogs)
{
  ...
  myBet = new Bet(.., Dogs);
  ...
}

// constructor of Bet
public Bet(..., Greyhound[] Dogs)
{
  ...
}


Maybe it's also possible to handle this with a pointer or a refernce to Dogs but I'm not perfectly sure how to do this. Maybe like
1
2
3
4
5
// contructor of Guy
public Guy(..., Greyhound[] (&Dogs))

// constructor of Bet
public Bet(..., Greyhound[] (&Dogs))
Last edited on
I think you need an encapsulating object that pulls everything together. Let's call it Race:

Fields might be:
Array of Dogs called Dogs
Winning dog called Dog
Array of Bets called Bets

You could even move out some stuff from Dog which really belongs in Race like:
int RacetrackLength;
Random Randomiser;

Race would have the GetDescription() method.

Does rearranging things like this help you?
Thanks for your replies. MaikCAE's method makes more sense to me from a 'scripting' background. I'm used to passing variables into functions and I can get my head around passing Dogs into Guy and then forwarding the same info into Bet.

I'm not sure how I'd implement a whole new class (as per your method kbw) but I'll have a think about it and see where it would fit in my project.

I've actually finished it now (just using the Dog's number, not its name) and I'm really pleased with it as my first program...

I'll go back now and see if I can get the names to work.

Thanks again for all your help

John ;-)
Topic archived. No new replies allowed.