abstract class error

Hey guys,

so a pretty beginner exercise but I decided to give it a shot, it's from a book I am currently reading on data structures and algorithms,

the problem I have is I can't instantiate the oneHundred bill class, obviously we can't instantiate the Bill class itself as it's an abstract class but shouldn't I be able to instantiate the oneHundred bill class as I don't provide any pure virtual functions in the class itself rather I just implement the virtual function that is supposed to be implemented which is - virtual string getMakePureVirtualCoin(). but the compiler is telling me I can't instantiate the oneHundred bill class as it's an abstract class, but is isn't, right??


C++\algorithmBookProblems\main.cpp|179|error: invalid new-expression of abstract class type 'OneHundred'|



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
  class Money{

   public:

   string makePureVirtual;
   string name;
   double amount;

   Money(){};
   Money(string name,double amount):name(name),amount(amount){};
   virtual string getMakePureVirtual()=0;
};

// notes / bills

class Bill : public Money{

   public:
       string makePureVirtualBill;
       Bill(){}
       Bill(string name,double amount): Money(name,amount){};
       virtual string getMakePureVirtualBill()=0;

};

class Five : public Bill{

public:
    Five():Bill("five",5){};
};

class Ten : public Bill{

public:
    Ten():Bill("ten",10){};
};

class Twenty : public Bill{

public:
    Twenty():Bill("twenty",20){};
};

class Fifty : public Bill{

public:
    Fifty():Bill("Fifty",50){};
};

class OneHundred : public Bill{

public:
    OneHundred():Bill("one hundred",100){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
};


// coins

class Coin : public Money{

   public:
       string makePureVirtualCoin;
       Coin(){}
       Coin(string name,double amount): Money(name,amount){};
       virtual string getMakePureVirtualCoin()=0;
};

class OneCent : public Coin{

public:
    OneCent():Coin("one Cent",0.01){};
};

class TwoCent : public Coin{

public:
    TwoCent():Coin("two Cent",0.02){};
};

class FiveCent : public Coin{

public:
    FiveCent():Coin("five Cent",0.05){};
};

class TenCent : public Coin{

public:
    TenCent():Coin("ten Cent",0.10){};
};

class TwentyCent : public Coin{

public:
    TwentyCent():Coin("twenty Cent",0.20){};
};

class FiftyCent : public Coin{

public:
    FiftyCent():Coin("fifty Cent",0.50){};
};

class OneEuro : public Coin{

public:
    OneEuro():Coin("one ",1){};
};

class TwoEuro : public Coin{

public:
    TwoEuro():Coin("two",2){};
};


Money* buySomething(int amount,int price){

     vector<Money*> money;

     if(amount < price)
        return NULL;

     double change = amount - price;
     double amountChangePaid = change;

     while(amountChangePaid > 0){

        if(amountChangePaid >= 100){

            amountChangePaid -= 100;
            OneHundred* onehundred = new OneHundred;
            //money.push_back(onehundred);
            continue;
        }

     }

}


int main()
{
    // to do
}


*edit - I forgot to implement the virtual function in the Moneyclass

adding virtual string getMakePureVirtual(){ cout << "2" << endl;} fixed the issue... so far
Last edited on
so fixed the issue but seems like my change dispenser is leaving me short a couple of cent

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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

class Money{

   public:

   string makePureVirtual;
   string name;
   double amount;

   Money(){};
   Money(string name,double amount):name(name),amount(amount){};
   virtual string getMakePureVirtual()=0;
};

// notes / bills

class Bill : public Money{

   public:
       string makePureVirtualBill;
       Bill(){}
       Bill(string name,double amount): Money(name,amount){};
       virtual string getMakePureVirtualBill()=0;

};

class Five : public Bill{

public:
    Five():Bill("five",5){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class Ten : public Bill{

public:
    Ten():Bill("ten",10){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class Twenty : public Bill{

public:
    Twenty():Bill("twenty",20){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class Fifty : public Bill{

public:
    Fifty():Bill("Fifty",50){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class OneHundred : public Bill{

public:
    OneHundred():Bill("one hundred",100){};
    virtual string getMakePureVirtualBill(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};


// coins

class Coin : public Money{

   public:
       string makePureVirtualCoin;
       Coin(){}
       Coin(string name,double amount): Money(name,amount){};
       virtual string getMakePureVirtualCoin()=0;
};

class OneCent : public Coin{

public:
    OneCent():Coin("one Cent",0.01){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class TwoCent : public Coin{

public:
    TwoCent():Coin("two Cent",0.02){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class FiveCent : public Coin{

public:
    FiveCent():Coin("five Cent",0.05){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class TenCent : public Coin{

public:
    TenCent():Coin("ten Cent",0.10){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class TwentyCent : public Coin{

public:
    TwentyCent():Coin("twenty Cent",0.20){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class FiftyCent : public Coin{

public:
    FiftyCent():Coin("fifty Cent",0.50){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class OneEuro : public Coin{

public:
    OneEuro():Coin("one ",1){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};

class TwoEuro : public Coin{

public:
    TwoEuro():Coin("two",2){};
    virtual string getMakePureVirtualCoin(){ cout << "now a class" << endl;}
    virtual string getMakePureVirtual(){ cout << "2" << endl;}
};


vector<Money*> buySomething(double amount,double price){

     vector<Money*> money;

     if(amount < price)
        return money;

     double change = amount - price;
     double amountChangePaid = change;

     while(amountChangePaid > 0){

        if(amountChangePaid >= 100){

            amountChangePaid -= 100;
            money.push_back(new OneHundred);
            continue;
        }

        if(amountChangePaid >= 50){

            amountChangePaid -= 50;
            money.push_back(new Fifty);
            continue;
        }

        if(amountChangePaid >= 20){
            amountChangePaid -= 20;
            money.push_back(new Twenty);
            continue;
        }

        if(amountChangePaid >= 10){
            amountChangePaid -= 10;
            money.push_back(new Ten);
            continue;
        }

        if(amountChangePaid >= 2){
            amountChangePaid -= 2;
            money.push_back(new TwoEuro);
            continue;
        }

        if(amountChangePaid >= 1){
            amountChangePaid -= 1;
            money.push_back(new OneEuro);
            continue;
        }

        if(amountChangePaid >= 0.50){
            amountChangePaid -= 0.50;
            money.push_back(new FiftyCent);
            continue;
        }

        if(amountChangePaid >= 0.20){
            amountChangePaid -= 0.20;
            money.push_back(new TwentyCent);
            continue;
        }

        if(amountChangePaid >= 0.10){
            amountChangePaid -= 0.10;
            money.push_back(new TenCent);
            continue;
        }

        if(amountChangePaid >= 0.05){
            amountChangePaid -= 0.05;
            money.push_back(new FiveCent);
            continue;
        }

        if(amountChangePaid >= 0.02){
            amountChangePaid -= 0.02;
            money.push_back(new TwoCent);
            continue;
        }

        if(amountChangePaid >= 0.01){
            amountChangePaid -= 0.01;
            money.push_back(new OneCent);
            continue;
        }
     }
     return money;
}


int main()
{

    vector<Money*> money = buySomething(200.42,88.55);

    for(int i = 0; i < money.size(); i++){

        cout << money.at(i)->name << endl;
    }
    return 0;
}


200.42 - 88.55 seems to give me back almost the correct amount of change but leaves me 7 cents short.





one hundred
ten
one
fifty Cent
twenty Cent
ten Cent



Last edited on
*edit again, spotted the bug I subtracted 10 instead of 0.10 now fixed, but now the while loop seems to infinitely loop, seems like it gets stuck on 0.01 cent and won't subtract any further to 0 .

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

vector<Money*> buySomething(double amount,double price){

     vector<Money*> money;

     if(amount < price)
        return money;

     double change = amount - price;
     double amountChangePaid = change;

     while(amountChangePaid > 0){

        cout << amountChangePaid << endl;

        if(amountChangePaid >= 100){

            amountChangePaid -= 100;
            money.push_back(new OneHundred);
            continue;
        }

        if(amountChangePaid >= 50){

            amountChangePaid -= 50;
            money.push_back(new Fifty);
            continue;
        }

        if(amountChangePaid >= 20){
            amountChangePaid -= 20;
            money.push_back(new Twenty);
            continue;
        }

        if(amountChangePaid >= 10){
            amountChangePaid -= 10;
            money.push_back(new Ten);
            continue;
        }

        if(amountChangePaid >= 2){
            amountChangePaid -= 2;
            money.push_back(new TwoEuro);
            continue;
        }

        if(amountChangePaid >= 1){
            amountChangePaid -= 1;
            money.push_back(new OneEuro);
            continue;
        }

        if(amountChangePaid >= 0.50){
            amountChangePaid -= 0.50;
            money.push_back(new FiftyCent);
            continue;
        }

        if(amountChangePaid >= 0.20){
            amountChangePaid -= 0.20;
            money.push_back(new TwentyCent);
            continue;
        }

        if(amountChangePaid >= 0.10){
            amountChangePaid -= 0.10;
            money.push_back(new TenCent);
            continue;
        }

        if(amountChangePaid >= 0.05){
            amountChangePaid -= 0.05;
            money.push_back(new FiveCent);
            continue;
        }

        if(amountChangePaid >= 0.02){
            amountChangePaid -= 0.02;
            money.push_back(new TwoCent);
            continue;
        }

        if(amountChangePaid >= 0.01){
            amountChangePaid -= 0.01;
            money.push_back(new OneCent);
            break;
        }
        cout << amountChangePaid << endl;
     }
     return money;
}





111.87
11.87
1.87
0.87
0.37
0.17
0.07
0.02
one hundred
ten
one
fifty Cent
twenty Cent
ten Cent
five Cent
one Cent



seems like it never gets to two cent? and when it does hit the if statement in which it checks if the amountChangePaid is greater than or equal to 2 it seems like the if statement is skipped.

anybody know why the while loop is not ending??

thanks
Last edited on
Topic archived. No new replies allowed.