vector population with pointers

So, i have defined a father class of grocery items that have the attributes of the items such as price name and quantity. I was also asked to make a subclass for each category of groceries, such as dairy, cereal and such. I need to make a program that lets a the user populate a list (i think a vector would be the best choice) after the list is populated the user has to be able to change the quantity of each item.

I researched and since they are all different classes and dont all have the same attributes im going to need to use the pointer to point to that class then push it back, right? but im having a hard time coming up with the code.

i figured for the user end it will first ask u if u want to add an item. then what kind and then ask u to type in all the attributes. but im suppose to have one add function for all the classes.

so i fiured it would be something for the dairy i set the pointer to the dairty and then push back (new.dairy)?

but its the part where i actually fill in the attributes that im stuck in. I havent programmed in years so, im sure im missing the basics of it..

would i set a point of item* class (the base class) to p
then proceed to get user input to populate the vector

such as

p->name // name of the grocery
any ideas?
since they are all different classes and don't all have the same attributes
Use polymorphism, with a method to set the values of the class.
1
2
3
grocery *p;
p = new derived_class;
p->input();
Last edited on
you can have a look at this one. It is not totally clean, but here is the idea.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class GroceryItem
{  
    protected:
        char name; //for simplicity only
        float price;
    
    public:
        virtual void setPrice(float p) = 0;
        virtual void setName(char c) = 0;
        virtual void printIt() = 0;
};

class Dairy: public GroceryItem
{
    private:
        string thisObject;
        
    public:
    Dairy() {
        thisObject = "Dairy";
        price = 0.0;
        name = ' ';
    }
    
    virtual void setPrice(float p) { price = p;}
    virtual void setName(char n) { name = n;}
    virtual void printIt() { cout << thisObject << " name = " << name << " price = " << price  << endl; }
};


class CannedGoods: public GroceryItem
{
    private:
        string thisObject;
        
    public:
    CannedGoods() {
        thisObject = "CannedGoods";
        price = 0.0;
        name = ' ';
    }
    
    virtual void setPrice(float p) { price = p;}
    virtual void setName(char n) { name = n;}
    virtual void printIt() { cout << thisObject << " name = " << name << " price = " << price  << endl; }
};


int main(int argc, char *argv[])
{
    vector<GroceryItem*> storage;
    storage.push_back(new Dairy());
    storage.push_back(new CannedGoods());
    
    for (int i = storage.size()-1; i >= 0; i--) {
        storage[i]->setPrice((i+50)*1000);
        storage[i]->setName(i+80);
        storage[i]->printIt();
        delete storage[i];
    }
    
    storage.clear();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



.....


Points.
the vector is a storage of GroceryItems. It could be Dairy, CannedGoods, etc.
Prices and name is set in the loop. (For simplicity)


Last edited on
If setPrice, setName and printIt will do the same for all the derived class, put the code in the base class. There will be no need for that methods to be declared virtual.
You could also use the constructor of the base class
1
2
3
4
GroceryItems::GroceryItems(){
  price = 0.0;
  name = ' ';
}
Last edited on
Topic archived. No new replies allowed.