aRandomStudent
aRandomStudent

Reputation: 101

How to pass an object of private member to another class's method?

I have two classes called PowerballLottery and PowerballTicket. Inside the PowerballLottery there is a member function called quickPick that is supposed to pick 6 random valid ints and store them in PowerballTicket. 6 ints mBall1, mBall2, mBall3, mBall4, mBall5 and mPowerball are initialized within the PowerballTicket and the class PowerballTicket should store the value of the 6 random ints generated by quickPick. How do I pass an object of the 6 ints inside PowerballTicket to PowerballLottery so that quickPick can modify them?

Upvotes: 0

Views: 227

Answers (5)

jonezq
jonezq

Reputation: 354

A simple implementation looks like:

  1. Create a struct with 6 fields, or use container (such as vector, for example).
  2. Add public methods to PowerballTicket (setter/getter idioms) void setData(const Cont &data), or keep container such as std::vector<int> and add method which will call push_back(someint), and call it 6 times from PowerballLottery.
  3. Add public method to PowerballLottery, which will keep pointer to PowerballTicket void (PowerballTicket *pbt) {p_pbt = pbt;} or pass it as a reference (PowerballTicket &pbt).
  4. Then modify it: p_pbt->setData (pbt.setData if you used a reference).

    #include <cstdlib> // for rand
    
    struct Numbers
    {
        int field1;
        int field6;
    };
    
    class Ticket
    {
    public:
        void setData(Numbers num) { _num = num; } //movement semantic for C++11+
        Numbers getData() const { return _num; }
    private:
        Numbers _num;
    };
    
    class Lottery
    {
    public:
        void fillTicket(Ticket &ticket) {
            Numbers num;
            num.field1 = quickPick();
            num.field6 = quickPick();
            ticket.setData(num);
        }
    
    private:
           int quickPick() { return std::rand() % 100; }
    };
    

Upvotes: 0

rozina
rozina

Reputation: 4232

No need to pass numbers to quickPick(). The function can just generate a PowerballTicket object and return it. You then assign the new value to the old licket object.

struct PowerballTicket
{
    int mBall1 = 0;
    int mBall2 = 0;
    int mBall3 = 0;
    int mBall4 = 0;
    int mBall5 = 0;
    int mPowerBall = 0;

    PowerballTicket(int b1, int b2, int b3, int b4, int b5, int pb)
    : mBall1(b1),
      mBall2(b2),
      mBall3(b3),
      mBall4(b4),
      mBall5(b5),
      mPowerBall(pb)
    {
    }
};

class PowerballLottery
{
public:
    PowerballTicket quickPick()
    {
        // generate numbers
        // then create a PowerballTicket object and return it
        return PowerballTicket(1, 2, 3, 4, 5, 1000);
    }
};

When you want to change your PowerballTicket object you just do the following:

PowerballTicket ticket;
PowerballLottery lottery;

ticket = lottery.quickPick();

Upvotes: 1

pcodex
pcodex

Reputation: 1940

make quickPick() a friend function of PowerBallTicket so it can just directly access the 6 members and set them

Upvotes: 0

Tommylee2k
Tommylee2k

Reputation: 2731

Usually you don't pass variables to other classes, tho it's possible

if you declare Lottery as a "friend" of Ticket, it can access it's members, even if they're private

look at this example:

#include <iostream>

class changer;

class fixed {
    friend class changer;
public:
    fixed(int x) : m_x(x) { } ;
    int get() { return m_x; };
private:
    int m_x;
};

class changer 
{
public: 
    void change(fixed& item, int new_value);
};

void changer::change(fixed& item, int new_value)
{
    item.m_x = new_value;
}


int main() {
    fixed one(1);
    changer two;

    std::cout << one.get() << "\n";
    two.change (one, 2);
    std::cout << one.get() << "\n";

    return 0;
}

the changer class is declared as a friend, so it can be used to change its members;

a lot easier in your case (if I unterstand what you want) is, if you add a setter function to Ticket, and call it for the generated numbers

PowerballTicket::setBalls(int b1, int b2, int b3, int b4, int b5, int pb) 
{
    mBall1 = b1;
    mBall2 = b2;
    mBall3 = b3;
    mBall4 = b4;
    mBall5 = b5;
    mPowerball = pb;
}

Upvotes: 0

ztdep
ztdep

Reputation: 381

make PowerballLottery a member of PowerballTicket

Upvotes: 0

Related Questions