Simple list class

I wanted to add a simple class Say - "Customer" and another class "Purchase". I want to make collection of Customer class which holds information about product purchased on specific day.
Can anyone please suggest how to make classes which interacts with referential manner.

For example an object of class "Customer " should contain some kind of array(or some other collection) of Purchase class object.
Thanks in advance.
I appreciate the help.
Brian
You want to dynamically create your Purchase classes. Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Customer
{
private:
  Purchase* purch;

public:
  Customer(){}
 ~Customer(){}

  void makePurchaseClass();
}; 

void Customer::makePurchaseClass()
{
  int i;
  cout << "How many: ";
  cin >> i;
  purch = new Purchase[i];
}


At least, that is how my beginner skills would do it. Of course, makePurchaseClass() would probably be better accepting "i" as an argument to keep the cout out of your class. That example works though.

At that point you do have an array of purchase classes, so you will want to say something like purch[2] to get the third Purchase instance.
Last edited on
Here's something very simple that should get you started.


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
#include <vector>
#include <iostream>

using namespace std;

class purchase
{
public:
  string nameOfObjectBought;
  int numberPurchased;
};


class customer
{
public:
  string name;
  vector<purchase> thisCustomersPurchases;
  void  showPurchases();
};


void customer::showPurchases()
{
  cout << "Customer " << name << " has bought:" <<endl;
  for (int eger=0; eger < thisCustomersPurchases.size(); ++eger)
  {
    cout << thisCustomersPurchases[eger].numberPurchased << " of item "<<
            thisCustomersPurchases[eger].nameOfObjectBought << endl;
  }
}

int main()
{
  customer firstCustomer;
  firstCustomer.name = "bob";

  purchase firstThingBought;
  firstThingBought.nameOfObjectBought = "egg";
  firstThingBought.numberPurchased = 7;
  firstCustomer.thisCustomersPurchases.push_back(firstThingBought);

  purchase secondThingBought;
  secondThingBought.nameOfObjectBought = "ham";
  secondThingBought.numberPurchased = 2;
  firstCustomer.thisCustomersPurchases.push_back(secondThingBought);
  
  firstCustomer.showPurchases();
  
}





Last edited on
Thanks for your help . I am still trying to think on your way.


I am sorry i am new to C++ as i am a C# programmer. so i am trying to elaborate my problem, I have two separate classes
Customer and Purchased.

1. Customer" - This class should have a
a) field int CustomerID(int) ,
b) CustomerName(char[30]) ,
c)some array of "Purchased" and
d)one function "addProducts(Purchased x) which inserts the object instance of "Purchased" class inside the array of Purchased.

2. Purchased" - this class should have two field CustomerID(int) and array of ProductID(int)

Now lets imagine that
a) customerID 1 has purchased ProductID 1,2,3
b) customerID 2 has purchased PoductID 2,4,5


Then i want to first create an instance of "Purchased" class say-"Purchased1" with assigning value of customerID= 1 and product ids 1,2,3 as an array.

in a same way i created another instance of "Purchased" class say Purchased2 with assigning values of customerID=2 and product ids 2,3,4 as an array.

now i looking to create an instance of "Customer" class say Customer1 and add up these instances Purchased1 and Purchased2 as using the function
addProducts(Purchased x) .



Thanks a ton for trying to help.

now i looking to create an instance of "Customer" class say Customer1 and add up these instances Purchased1 and Purchased2 as using the function
addProducts(Purchased x) .

I'm not too sure what this means, you want to keep a list of the purchases?

I think Moschops' method is the way to go. Your Purchace class is so simple that it only needs to be a vector (basically an array with a dynamic size) within the Customer class.

My way isn't going to work if you want to keep making new purchases.
Last edited on
Hey Moschops and LowestOne,
I really appreciate your help Since the vector thing has really worked. I am really happy for it.
You guys are awesome as you have such a nice attitude to help others.

Thanks again. :) ENJOY!
Topic archived. No new replies allowed.