Confused on creating new random objects

Hello, I'm a little confused on this topic. Lets say I have these 2 classes.

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
class Me
{
     protected:
         vector<money*> myPocket;
     public:
         //constructor etc
}

class money
{
     protected:
         int value;
         int uniqueID;
         string name;
     public:
        //constructor etc to set the name,ID and value

}

main()
{
   money * bill = new money;

   myPocket.push_back(bill)
}


From my understanding this would create a new instance of money. I'm confused how I could use this "new" operator and say.. randomly generate a 1, 5,20, and 50 dollar bill (with their correct name, uniqueid, etc.)and then put it in the myPocket vector for later use. Do you need to include constructors when using the new operator? Do people normally store information in a text file and read it when they want to create a "random" object out of a list of certain objects? Sorry, I'm just a little confused on how to use the new operator to make new items and properly store/keep track of them.
Last edited on
first of all you are defining your classes wrong. define like such
1
2
3
4
5
6
class <#class name#> {
    <#instance variables#>//private by default
    
public:
    <#member functions#>
};//you forgot this  


second your main function has no return type so that is also wrong.

third, learn which can access which with protected, private, and public.
lastly, you are accessing a vector on like 24 that has not been alloced yet, so you are literally manipulating random memory if your compiler EVEN allows that to be a legal statement.
you ALSO forgot the ; after the push_back.
Neither pointers nor new are of any use here. The money should be in your pocket. What you wrote is a pocket that knows where somebody else's money may (or may not) be.

Make it yours:

1
2
3
4
5
6
7
class Me
{
     private:
         vector<money> myPocket;
     public:
         //constructor etc
};


As for randomly generating 1, 5, 20, or 50, the easiest approach is to randomly generate 0, 1, 2, 3 and then store the appropriate bill in your pocket:

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
#include <iostream>
#include <vector>
#include <random>
using namespace std;

class money
{
 private:
    int value;
    int uniqueID;
    string name;
 public:
    money(int v, int id, const string& name)
    : value(v), uniqueID(id), name(name) {}
    int val() const { return value; }
};

class Me
{
 private:
    vector<money> myPocket;
 public:
    void put(const money& m)
    {
        myPocket.push_back(m);
        std::cout << "Added " << m.val() << " to my pocket\n";
    }
};

const money bills[4] = { money(1, 0, "one"), money(5, 0, "five"), 
                         // what happened to 10?
                         money(20, 0, "twenty"), money(50, 0, "fifty") };

int main()
{
   Me me;

   random_device rd;
   mt19937 eng(rd());
   uniform_int_distribution<> d(0, 3);

   for(int n = 0; n < 100; ++n)
       me.put(bills[d(eng)]);
}

online demo: http://ideone.com/UyNMr
Last edited on
Topic archived. No new replies allowed.