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:
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, constfloat& 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(constfloat& 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, constfloat& 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();
};
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(constfloat& 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.
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.
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.