About Vectors....

So i have this code for an exercise about a Delivery company that delivers packages. Each package has its different characteristics and its separated in 3 categories, the basic, the advanced, and the overnight.

The question is how can i access the member functions, like calculateDate, of any class when the object is created in the vector. I have beed trying using an iterator but it comes up with an error...

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <cstdlib>
#include <vector>
#include <iostream>
#include <ctime>
#include <string>
#define a 5
#define f 2
#define b 10
using namespace std;

class Date
{
private:
    int day, month, year;
public:
    Date();
    void display_date ();
    Date operator+ (int x);
};

class Package
{
protected:
    float weight;
    string namesend;
    string namerec;
    string addrec;
    Date date;
public:
    Package();
    virtual float calculateCost();
    virtual void calculateDate ();
    virtual void displayPackage ();
    void set_weight (float wei);
    float get_weight();
    void set_namesend (string name);
    string get_namesend ();
    void set_namerec (string rec);
    string get_namerec ();
    void set_addrec (string addrec);
    string get_addrec ();
};

class Advanced_Package:public Package
{
public:
    Advanced_Package();
    float calculateCost();
    void calculateDate();
    void displayPackage ();
};

class Overnight_Package:public Package
{
public:
    Overnight_Package();
    float calculateCost();
    void calculateDate();
    void displayPackage ();
};
Date::Date()
{
    time_t rawtime;
    struct tm* timeinfo;
    time(&rawtime);
    timeinfo=localtime(&rawtime);
    day=timeinfo->tm_mday;
    month=timeinfo->tm_mon + 1;
    year=timeinfo->tm_year + 1900;
}

void Date::display_date()
{
    cout << day << "/" << month << "/" << year << endl;
}

Date Date::operator +(int x)
{    
    Date temp;
    temp.day=temp.day+x;
    if(temp.month%2==0)
    {
        if(temp.day>31)
        {
            temp.day=temp.day-31;
            temp.month++;
            if(temp.month>12)
            {
                temp.year++;
                temp.month=temp.month-12;
            }
        }
    }
    else
    {
        if(temp.day>30)
        {
            temp.day=temp.day-30;
            temp.month++;
            if(temp.month>12)
            {
                temp.year++;
                temp.month=temp.month-12;
            }
        }
    }
    return temp;
}

Package::Package()
{
    weight=0;
    namesend="unknown";
    namerec="unknown";
    addrec="unknown";
}
void Package::set_weight (float wei)
{
    weight=wei;
}
float Package::get_weight()
{
    return weight;
}
void Package::set_namesend (string name)
{
    namesend=name;
}
string Package::get_namesend ()
{
    return namesend;
}
void Package::set_namerec (string rec)
{
    namerec=rec;
}
string Package::get_namerec ()
{
    return namerec;
}
void Package::set_addrec (string adrec)
{
    addrec=adrec;
}
string Package::get_addrec ()
{
    return addrec;
}
void Package::calculateDate()
{
    date=date+5;
}
void Package::displayPackage()
{
    cout << weight << "  " << namesend << "  " << namerec << "  " << addrec << "\nyou have picked Basic package delivery"<< endl;
    cout << "ETOD: ";
    date.display_date();
    cout << endl;
}

float Package::calculateCost()
{
    return a*(weight);
}
Advanced_Package::Advanced_Package()
{
    weight=0;
    namesend="unknown";
    namerec="unknown";
    addrec="unknown";
}
float Advanced_Package::calculateCost()
{
    return f+a*(weight);
}
void Advanced_Package::calculateDate()
{
    date=date+2;
}
void Advanced_Package::displayPackage()
{
    cout << weight << "  " << namesend << "  " << namerec << "  " << addrec << "\nyou have picked Advanced package delivery"<< endl;
    cout << "ETOD: ";
    date.display_date();
    cout << endl;
}
Overnight_Package::Overnight_Package()
{
    weight=0;
    namesend="unknown";
    namerec="unknown";
    addrec="unknown";
}
float Overnight_Package::calculateCost()
{
    return (b+a)*(weight);
}
void Overnight_Package::calculateDate()
{
    date=date+1;
}
void Overnight_Package::displayPackage()
{
    cout << weight << "  " << namesend << "  " << namerec << "  " << addrec << "\nyou have picked Overnight package delivery"<< endl;
    cout << "ETOD: ";
    date.display_date();
    cout << endl;
}


i have been trying using this, but the program ends when it gets to the line after the creation of the object

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
75
76
77
78
79
80
81
82
83
vector<Package*> packages;
    string str;
    float num;
    int option;
    vector<Package*>::iterator it=packages.begin();
    vector<Package*>::iterator at;
    do{
    cout << "-----------Menu-----------------------" << endl;
    cout << "|Type 1 to send a Basic package      |" << endl;
    cout << "|Type 2 to send an Advanced package  |" << endl;
    cout << "|Type 3 to send an Overnight package |" << endl;
    cout << "|Type 4 to view all pending packages |" << endl;
    cout << "|Type 0 to exit                      |" << endl;
    cout << "--------------------------------------" << endl;
    cin >> option;
    cin.ignore();
    
    if(option==1)
    {
        packages.push_back(new Package());
        (*it)->calculateDate();
        cout << "insert your name" << endl;
        getline(cin, str);
        (*it)->set_namesend(str);
        cout << "insert receivers name" << endl;
        getline(cin, str);
        (*it)->set_namerec(str);
        cout << "insert receiver's address" << endl;
        getline(cin, str);
        (*it)->set_addrec(str);
        cout << "insert the weight of the package" << endl;
        cin >> num;
        cin.ignore();
        (*it)->set_weight(num);
        ++it;
    }
    else if(option==2)
    {
        packages.push_back(new Advanced_Package());        
        (*it)->calculateDate();
        cout << "insert your name" << endl;
        getline(cin, str);
        (*it)->set_namesend(str);
        cout << "insert receivers name" << endl;
        getline(cin, str);
        (*it)->set_namerec(str);
        cout << "insert receiver's address" << endl;
        getline(cin, str);
        (*it)->set_addrec(str);
        cout << "insert the weight of the package" << endl;
        cin >> num;
        cin.ignore();
        (*it)->set_weight(num);
        ++it;
    }
    else if(option==3)
    {
        packages.push_back(new Overnight_Package());
        (*it)->calculateDate();
        cout << "insert your name" << endl;
        getline(cin, str);
        (*it)->set_namesend(str);
        cout << "insert receivers name" << endl;
        getline(cin, str);
        (*it)->set_namerec(str);
        cout << "insert receiver's address" << endl;
        getline(cin, str);
        (*it)->set_addrec(str);
        cout << "insert the weight of the package" << endl;
        cin >> num;
        cin.ignore();
        (*it)->set_weight(num);
        ++it;
    }
    else if(option==4)
    {
        for(at = packages.begin(); at!=packages.end(); ++at)
    {
        (*at)->displayPackage();
    }
    }
    }
    while(option!=0);
Last edited on
ohh nevermind i figured it out...
i had been initializing the iterator, before the vector had any objects, instead of initializing it inside every <if>

any other suggestions are welcome :D
Last edited on
Topic archived. No new replies allowed.