user3896213
user3896213

Reputation: 3

c++ set value of a class with a pointer of another class

I am trying to implement the game Deal or no deal, i have two classes, Box & Player. in box is stored the vector game_box and in Player I want to store the box and the money that the player has to keep till the end of the game.

I have tried to implement it in this way. it runs correctly, without no errors, but when i try to verify if the values were stored into the setter, it just give me that the setter is empty. I really dont understand why! does anybody knows why?

class Box 
{
vector<Box> game_box;
float pound_contained;
int box_number;

public:

Box(int box_number, float pound_contained);
Box();

int getbox_number();
float getpound_contained();

};

class Player :public Box
{
 int player_box;
 float player_money;

public:
Player();
~Player();

float getplayer_money();
int getplayer_box();

void setplayer_money(float);
void setplayer_box(int);
};

void Player::setplayer_money(float)
{ 
player_money = player_money;
}
 void Player::setplayer_box(int)
{
player_box = player_box;
}

 float Player::getplayer_money()
{
return this->player_money;
}
 int Player::getplayer_box()
{
return this->player_box;
}

  int main()
  {
    vector<Box> game_box;
    srand(time(0));
     int n;
     Player money;
    Box oper;

float myArray [22][2] = {   { 0.01, 0 },    { 0.10, 0 },    { 0.50, 0 },
                            { 1, 0 },       { 5, 0 },       { 10, 0 },
                            { 50, 0 },      { 100, 0 },     { 250, 0 },
                            { 500, 0 },     { 750, 0 },     { 1000, 0 },
                            { 3000, 0 },    { 5000, 0 },    { 10000, 0 },
                            { 15000, 0 },   { 20000, 0 },   { 35000, 0 },
                            { 50000, 0 },   { 75000, 0 },   { 100000, 0 },
                            { 250000, 0 }   
                        };

//random assignation of pound value to the 22 boxes
for (int e = 1; e <22; e++) 
{
    int pos;
    bool op = true;
    while (op)
    {
        pos = rand() % 22 + 1;  
            if (myArray[pos][1] == 0)
            {
                myArray[pos][1] = 1;
                op = false;
            }
    }
    Box b(e, myArray[pos][0]);  //creating the class game
    game_box.push_back(b);  //function of the vector to insert a data in it 
}

// random assignment of a box to the player
int i = rand() % 22 + 1;
Box* boxes = &game_box[i];
cout << "Your box is going to be the box number: "<< boxes->getbox_number()<<endl;

////////////////////////////////////////////////////////////////////setter not working
money.setplayer_money(boxes->getpound_contained());
money.setplayer_box(oper.getbox_number());
cout << money.getplayer_box() << " " << money.getplayer_money() << endl << endl;
game_box.erase(game_box.begin()+i);
return 0;
}

Upvotes: 0

Views: 1786

Answers (2)

There are not "setters" or "getters" in C++ that are distinct from other method calls. So you write them just as you would any other method.

To elaborate on the problem pointed out by @maniek, you wrote:

void Player::setplayer_money(float)
{ 
    player_money = player_money;
}

C and C++ have the ability to specify method and function arguments without names--just types. This might seem a little strange as there is no way to access that parameter (at least not a way that is guaranteed to work in all compilers or optimization levels, you can possibly do it by messing with the stack). So what you are doing here is just setting the member player_money to itself.

(Note: If you are wondering why it allows you to specify a method argument without a name, it has a few uses...one is that not naming it suppresses warning messages that you aren't using that parameter. So it is a way of marking something as not being used at the current time, yet still required--perhaps for legacy reasons or perhaps because you might use it in the future.)

You can give a new name to the parameter that doesn't overlap with the name of the member variable:

void Player::setplayer_money(float new_player_money)
{ 
    player_money = new_player_money;
}

That's one way of avoiding ambiguity. Because in terms of what value is in scope, the parameter will win over the member variable. So this would be another do-nothing operation, that would assign the parameter value to itself:

void Player::setplayer_money(float player_money)
{ 
    player_money = player_money;
}

(Note: Since player_money is passed by value and not by reference, that wouldn't change the parameter's value at the calling site. In particular, how could it change the value, if you passed in a constant like 10.20.)

What @maniek suggested is that a way to disambiguate in that case is to use this->player_money when you mean the member variable and player_money when you mean the argument to the method. Another thing some people do is name their member variables specially--like start them with m_ as in m_player_money:

void Player::setplayer_money(float player_money)
{ 
    m_player_money = player_money;
}

(Note: You can also just use an underscore prefix with no m as long as the next character is lowercase, but some people consider that too dangerous as underscores followed by capital letters are reserved for compiler internal usages.)

As a final thought--if the class name is Player then it's already implicit whose money you are setting (the player's) so you could just call it set_money. Furthermore, I'm not a fan of underscores in names (more common in C than C++) so I'd probably call it setMoney.

Upvotes: 3

maniek
maniek

Reputation: 7307

How about:

void Player::setplayer_money(float player_money)
{ 
this->player_money = player_money;
}

?

Upvotes: 1

Related Questions