Add objects to an array by reference?

I have 2 classes Business and Customer. Each Business holds an array of Customer objects m_curPatrons[], which represents the patrons of that particular business. I have a method that sells every member of that array an item, and increments Customer member variable m_purchases that shows how many items he's bought.

What's happening is the array m_curPatrons[] is a set of seperate objects outside of the original Customer objects, and Customers inside that array are getting their member variables modified. I'm wondering if I can construct the array so that the Customers in the array are indistinguishable from the original Customer objects. Code follows:

Header files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Customer
{
private:
  string m_purchases[MAX_PURCH];
  short m_numPurchases; //how many purchases are held in m_purchases
  float m_money;
  string m_name;

public:
  // Constructors
  Customer();
  Customer(const string& name, const float& money);
  
  // Member Functions
  
  //Purpose: Add an item to the array of purchases
  //Post: item is either added to m_purchases[] or false is returned
  //Return: True if item successfully added, false otherwise
  bool purchase(const string& item);
  
  //Purpose: Modify the current amount of customer's money
  //Post: Either amt is added to m_money or False is returned
  //Return: True if successful, false if amt would reduce m_money below 0
  bool addMoney(const float& amt);

};

class Business
{
  private:
    string m_name;
    float m_till;
    string m_merchandise[MAX_MERCH];
    Customer m_curPatrons[MAX_PATRONS];
    short m_numPatrons;
    short m_numMerch;

  public:
    // Constructors
    Business() : m_name(""), m_till(0), m_numPatrons(0), m_numMerch(0) {};
    Business(const string& name, const float& till);

    // Member Functions

    //Purpose: Add a customer to the business's list of current patrons
    //Post: The customer to be added is appended to m_curPatrons or an error
    // message is printed
    void newPatron(const Customer& name);

    //Purpose: Sells random items from inventory to all current patrons
    //Post: Each patron who can afford an item is charged and are given
    // an item at random
    void make_a_sale();
};


Business Constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

void Business::newPatron(const Customer& patron)
{
  if (m_numPatrons < MAX_PATRONS) //prevent exceeding size of m_curPatrons
  {
    m_curPatrons[m_numPatrons] = patron;
    m_numPatrons++;
    return;
  }
  else //print error message
  {
    cout << "Business " << m_name << " already full. " << patron.getName()
      << " was not added." << endl;
  }
}

void Business::make_a_sale()
{
  int randItem;
  for (int i=0; i<m_numPatrons; i++)
  {
    randItem = rand()%m_numMerch;
    // check if transaction can be completed before adding money to m_till
    if (m_curPatrons[i].purchase(m_merchandise[randItem]))
      m_till += COST;
  }
  
  return;
}



Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

void Business::make_a_sale()
{
  int randItem;
  for (int i=0; i<m_numPatrons; i++)
  {
    randItem = rand()%m_numMerch;
    // check if transaction can be completed before adding money to m_till
    if (m_curPatrons[i].purchase(m_merchandise[randItem]))
      m_till += COST;
  }
  
  return;
}

bool Customer::purchase(const string& item)
{
  bool success = false;
  if (m_numPurchases < MAX_PURCH) //prevent purchasing too many items
  {
    if (addMoney(-COST)) //only do if the cost can be taken from m_money
    {
      m_purchases[m_numPurchases] = item;
      m_numPurchases++;
      success = true;
    }
  }
  return success;
}

bool Customer::addMoney(const float& amt)
{
  bool success = false;
  if (m_money+amt >= 0) //can't take more money than customer has
  {
    m_money += amt;
    success = true;
  }
  return success;
}


Right now when I call someBusiness.make_a_sale(), it'll increment m_numPurchases for each customer in someBusiness.m_curPatrons[]. But if someGuy is a Customer object that's been
added to that array, I want the the original someGuy.m_numPurchases to get incremented also.

Thank you for the help.
What's happening is the array m_curPatrons[] is a set of seperate objects outside of the original Customer objects, and Customers inside that array are getting their member variables modified. I'm wondering if I can construct the array so that the Customers in the array are indistinguishable from the original Customer objects.

But if someGuy is a Customer object that's been
added to that array, I want the the original someGuy.m_numPurchases to get incremented also.


What exactly do you mean? Elaborate.
It turns out it won't matter. I'm going to copy the customers into the business, change their state, and then send their state back to the original customers. Thank you anyways.
Topic archived. No new replies allowed.