Accessing a member of a class from a member function of another class

Jan 10, 2011 at 5:30pm
If I have a class with a public member variable, and another class with a private member function, and I want to use the variable inside the function, how do I access it?

1
2
3
4
5
6
7
8
9
10
11
12
class Firstclass {
  public:
    FirstClass() {}
    int firstclass_variable;
};

class SecondClass {
  public:
    SecondClass() {}
  private:
    void secondclassFunction();
};


firstclass_variable will throughout the program have its value changed so I'll want to either access it directly, or I'll want to copy its value and then return the new value to the original variable (the first option sounds better though).
Last edited on Jan 10, 2011 at 5:31pm
Jan 10, 2011 at 5:54pm
I wonder if you understand the point of classes. You need an instance to modify a non-static member. Depending on how your system is actually set up, you might need to make a Firstclass member inside of SecondClass or something else.
Jan 10, 2011 at 6:02pm
Zhuge: I'm pretty confident I don't. This is a self-made exercise in OOP and classes.

It's a casino console program. Basically, I have a player class and a casino class, and each game in the casino is a subclass of casino, and player has a variable that stores all his money, and I want to be able to alter the value of that variable through functions called within these subclasses.
Jan 10, 2011 at 6:10pm
Your code above defines how to build two new kinds of object. An object type called Firstclass and and object type called SecondClass.

Recall how to make instances of objects; for example, to create an instance of an int object

int x;

To create an instance of a double object

double y;

To create an instance of a Firstclass object

Firstclass z;

To create an instance of a SecondClass object

SecondClass a;

So now you have an object of type Firstclass, named z, and an object of type SecondClass, named a.

To access the public variable inside z, for example to give it a value;

z.firstclass_variable = 8

Now the variable firstclass_variable, which is part of the object called z, has the value of 8.

That's how to access it; first you have to actually create the object, then you can use it.

It is possible to define a class and include "static" parts of it; these static parts are identical for all instances, and you can actually make use of them without creating an instance of the class. When you've got to grips with creating instances, look up static to see the clever things you can do with it.

Last edited on Jan 10, 2011 at 6:21pm
Jan 10, 2011 at 6:43pm
Consider the following code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//How to make objects of the types Player and Casino.
class Player {
  public:
    Player (const int startcapital) { money = startcapital; }
    int money;
};

class Casino {
  public:
    Casino ();
  private:
    void Betting();

//Definition of the member function Betting().
void Casino::Betting() {
  //I want to access the variable money here.
}


The program is supposed to be structured so that a Player object and Casino object will be created inside the main function. The constructor of Casino is supposed to create an object of a class that is a direct subclass of Casino. The object of that class will then call a member function of Casino. (I did not include this in the code snippet).

The problem is, in the definitions of the functions, there is no Player object.
Last edited on Jan 10, 2011 at 6:44pm
Jan 10, 2011 at 6:58pm
"The constructor of Casino is supposed to create an object of a class that is a direct subclass of Casino."

That doesn't make any sense to me. The constructor of Casino (i.e. Casino(); is for making objects of type Casino. If you want to make an object of a difference class, you use the constructor of that different class. If you create a class that inherits from Casino (the "direct subclass" of which you speak) it will have its own constructor.


EDITED TO REMOVE THE RISK OF INFINITY :p

"The problem is, in the definitions of the functions, there is no Player object."

You haven't explained what you want the player to do. What functions do you think should interact with a player object?
Last edited on Jan 10, 2011 at 7:08pm
Jan 10, 2011 at 7:01pm
Actually, you can't have the base class contain things deriving from it because you would then have an infinite number of objects.

That doesn't make any sense to me.


I agree with this.
Jan 10, 2011 at 7:07pm
Oops; that's me. You can't have them. I've corrected my post above.
Last edited on Jan 10, 2011 at 7:07pm
Jan 10, 2011 at 7:22pm
I originally made a program where the whole casino game worked. And to get some practice with OOP and using classes, I decided to try to implement OOP in the program.

The Player class has pretty much one thing to do: Earn money. When a Player object is created, its constructor will give the variable money a value. It then has a member function that adds a value to money:

void MoneyHandler(int moneyBet) { money += moneyBet * 3 }

When a Casino object is created, it's supposed to actually start the game, with the appropriate loop to keep the game going, and a switch statement that, depending on user input, creates an object of the class that represents the game the player wants to play. For instance, if you want to play Guess The Number, your input would be '1' and case '1' would create the object GuessTheNumber newGame.

GuessTheNumber is a subclass to Casino.

At some point, newGame will call a member function Betting() which was declared inside the declaration of the Casino class. The function Betting() is supposed to use the variable money.


Anyway, I'm probably going about this very wrong. Unfortunately, I don't have much time to get the hang of this =/
Last edited on Jan 10, 2011 at 7:22pm
Jan 10, 2011 at 7:24pm
Metallon>
1
2
3
void Casino::Betting() {
  //I want to access the variable money here.
}


There are two possible explanations: either the method does not belong the Casino abstraction or the Casino should contain a number of player objects and access to their money via public interface.
Last edited on Jan 10, 2011 at 7:28pm
Jan 10, 2011 at 7:24pm
I think what you want is to have Betting() take a Player as a parameter; the Player who is doing the betting, and use their money value.
Topic archived. No new replies allowed.