3 / 3 / 1
Регистрация: 31.01.2013
Сообщений: 63
1

Заменить наследование классов на наследование интерфейсов

10.05.2013, 21:51. Показов 2579. Ответов 9
Метки нет (Все метки)

C++
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
#include <iostream>
#include <assert.h>
 
using namespace std;
 
int people_on_base = 100; 
int vehicles_on_base = 100; 
double petrol_on_base = 100; 
double goods_on_base = 100; 
 
class IVehicle{
public:
    virtual ~IVehicle(){};
    virtual double getTankVolume() const = 0;
    virtual double getPetrolAmount() const = 0;
    virtual void arrive() = 0;
    virtual int leave() = 0;
};
 
class Vehicle:virtual public IVehicle{ 
private:
    double petrol_amount;
    double tank_volume;
public:
    Vehicle(){
        this->petrol_amount = 200;
        this->tank_volume = 300;
    }
 
    Vehicle(double petrol_amount, double tank_volume){
        this->petrol_amount = petrol_amount;
        this->tank_volume = tank_volume;
    }
 
    double getTankVolume() const{
        return this->tank_volume;
    }
 
    ~Vehicle(){}
 
    double getPetrolAmount() const{
        return this->petrol_amount;
    }
 
    void arrive(){
        people_on_base++;
        vehicles_on_base++; 
    }
 
    int leave(){
        if(petrol_on_base - this->tank_volume + this->petrol_amount < 0) return -1;
        if(people_on_base < 1) return -1;
        if(vehicles_on_base < 1) return -1;
        petrol_on_base -= (this->tank_volume - this->petrol_amount);
        this->petrol_amount = this->tank_volume;
        vehicles_on_base -= 1;
        people_on_base -= 1;
        return 0;
    }
};
 
class IBus: virtual public IVehicle, public Vehicle{
public:
    virtual ~IBus(){};
    virtual int getPeopleCount() const = 0;
    virtual int getMaxPeople() const = 0;
};
 
class Bus: virtual public IBus{
private:
    int people;
    int max_people;
public:
    Bus(int people, int max_people, double petrol, double max_petrol){
        Vehicle(petrol, max_petrol);
        this->people = people;
        this->max_people = max_people;
    }
    ~Bus(){}
    int getPeopleCount() const{
        return people; 
    }
    int getMaxPeople() const{
        return max_people;
    }
    void arrive(){
        people_on_base += this->people; 
        Vehicle::arrive();
        people=0;
    }
 
    int leave(){
        if(Vehicle::leave()!=0) return -1;
        if(this->max_people < people_on_base){
            this->people = this->max_people;
            people_on_base -= this->max_people;
        }
        else{
            this->people = people_on_base;
            people_on_base=0;
        }
        return 0;
    }
};
 
class ITruck: virtual public IVehicle, public Vehicle{
public:
    virtual ~ITruck(){};
    virtual double getCurrentLoad() const = 0;
    virtual double getMaxLoad() const = 0;
};
 
class Truck:virtual public ITruck{
    double load;
    double max_load;
public:
    Truck(double load, double max_load, double petrol, double max_petrol){
        Vehicle(petrol, max_petrol);
        this->load = load;
        this->max_load = max_load;
    }
    double getCurrentLoad() const{
        return load;
    }
    double getMaxLoad() const{
        return max_load;
    }
    void arrive(){
        Vehicle::arrive();
        goods_on_base += load;
        load = 0;
    }
 
    int leave(){
        if(Vehicle::leave()==-1) return -1;
        else if(goods_on_base<this->max_load) return -1;
        else goods_on_base -= max_load;
        load = max_load;
        return 0;
    }
};
 
class Expeditor:virtual public IBus, virtual public ITruck, public Bus, public Truck{
public:
    Expeditor(int people, int max_people, double load, double max_load, double petrol, double max_petrol): Truck(load, max_load, petrol, max_petrol), Bus(people, max_people, petrol, max_petrol){}
    void arrive(){
        int people = people_on_base;
        int vehicle = vehicles_on_base;
        Truck::arrive();
        people_on_base = people;
        vehicles_on_base = vehicle;
        Bus::arrive();
    }
    int leave(){
        int people = people_on_base;
        double p = petrol_on_base;
        int vehicle = vehicles_on_base;
        Truck::leave();
        petrol_on_base = p;
        people_on_base = people;
        vehicles_on_base = vehicle;
        Bus::leave();
        return 0;
    }
};
 
int main(){
petrol_on_base = 1000;
people_on_base = 10;
goods_on_base  = 20.000;
vehicles_on_base = 10;
 
// polymorphism!
IVehicle* v = new Bus(3, 5, 200, 300);
v->arrive();
 
assert(people_on_base == 14);   // 10 + 3 + driver
assert(goods_on_base == 23.0);  // 20 + 3
assert(vehicles_on_base == 11); // 10 + 1 
 
v->leave();
 
assert(people_on_base == 14 - 6);       // 5+driver left
assert(goods_on_base == 23.0 - 5.0);    // 5 tons left
assert(petrol_on_base == 900);          // minus (300-200)
assert(vehicles_on_base == 10);
 
// additional: test cast to Bus and Truck
IBus* b = dynamic_cast<IBus*>(v);
ITruck* t = dynamic_cast<ITruck*>(v);
assert(b);
assert(t);
 
delete v;
cout << "OK\n";
    return 0;
}
помогите устранить ошибку. Заменить множественное наследование классов на множественное наследование интерфейсов. Уже битый час сижу над ней к уму ни чего не приходит.
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
10.05.2013, 21:51
Ответы с готовыми решениями:

Множественное наследование интерфейсов - нужен пример
Наведите плиз пример кода множественного наследования интерфейсов?

Автоматическая генерация классов С ++ с UML диаграмм классов. Наследование в с++. Абстрактные классы. WhiteStarUML
Создать классовую модель(желательно в WhiteStarUML), которая включает в себя абстрактный класс...

Наследование классов. Копирование производных классов
Здравствуйте всем, у меня такой вопрос: написал код #include &quot;stdafx.h&quot; class A //Создаем...

Наследование классов
Здравствуйте всем! Классы и наследования еще не изучил а задали решить задачу. Просьба может у...

9
What a waste!
1607 / 1299 / 180
Регистрация: 21.04.2012
Сообщений: 2,727
10.05.2013, 22:25 2
А в чём проблема, так нельзя сделать?
C++
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
struct IVehicle {
 
   virtual double getTankVolume() const = 0;
   virtual double getPetrolAmount() const = 0;
   virtual void arrive() = 0;
   virtual int leave() = 0;
 
   virtual ~IVehicle() {};
};
 
struct IBus : IVehicle {
    
   virtual int getPeopleCount() const = 0;
   virtual int getMaxPeople() const = 0;
};
 
struct ITruck : IVehicle {
 
   virtual double getCurrentLoad() const = 0;
   virtual double getMaxLoad() const = 0;
};
 
 
class Vehicle : public IVehicle { /* ... */ };
 
class Bus: public IBus { /* ... */ };
 
class Truck : public ITruck { /* ... */ };
 
class Expeditor : public IBus, public ITruck { /* ... */ };
0
3 / 3 / 1
Регистрация: 31.01.2013
Сообщений: 63
10.05.2013, 22:33  [ТС] 3
Цитата Сообщение от gray_fox Посмотреть сообщение
struct IVehicle {
virtual double getTankVolume() const = 0;
* *virtual double getPetrolAmount() const = 0;
* *virtual void arrive() = 0;
* *virtual int leave() = 0;
virtual ~IVehicle() {};
};
struct IBus : IVehicle {
virtual int getPeopleCount() const = 0;
* *virtual int getMaxPeople() const = 0;
};
struct ITruck : IVehicle {
virtual double getCurrentLoad() const = 0;
* *virtual double getMaxLoad() const = 0;
};
Структуры хранят только типы! Как может структура хранить виртуальные функции?
0
What a waste!
1607 / 1299 / 180
Регистрация: 21.04.2012
Сообщений: 2,727
10.05.2013, 22:36 4
Yandex, struct и class отличаются только модификаторами доступа и наследования по умолчанию (struct - public, class - private).
0
3 / 3 / 1
Регистрация: 31.01.2013
Сообщений: 63
10.05.2013, 22:55  [ТС] 5
нет так
Цитата Сообщение от gray_fox Посмотреть сообщение
А в чём проблема, так нельзя сделать?

struct IVehicle {
virtual double getTankVolume() const = 0;
* *virtual double getPetrolAmount() const = 0;
* *virtual void arrive() = 0;
* *virtual int leave() = 0;
virtual ~IVehicle() {};
};
struct IBus : IVehicle {
virtual int getPeopleCount() const = 0;
* *virtual int getMaxPeople() const = 0;
};
struct ITruck : IVehicle {
virtual double getCurrentLoad() const = 0;
* *virtual double getMaxLoad() const = 0;
};
class Vehicle : public IVehicle { /* ... */ };
class Bus: public IBus { /* ... */ };
class Truck : public ITruck { /* ... */ };
class Expeditor : public IBus, public ITruck { /* ... */ };
нет так нельзя не выполняется строка IVehicle* v = new Bus(3, 5, 200, 300);
0
What a waste!
1607 / 1299 / 180
Регистрация: 21.04.2012
Сообщений: 2,727
10.05.2013, 22:58 6
Цитата Сообщение от Yandex Посмотреть сообщение
нет так нельзя не выполняется строка IVehicle* v = new Bus(3, 5, 200, 300);
http://ideone.com/dF7juq
0
3 / 3 / 1
Регистрация: 31.01.2013
Сообщений: 63
10.05.2013, 23:53  [ТС] 7
C++
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
#include <iostream>
#include <assert.h>
using namespace std;
 
int people_on_base = 100; 
int vehicles_on_base = 100; 
double petrol_on_base = 100; 
double goods_on_base = 100; 
 
struct IVehicle {
 
   virtual double getTankVolume() const = 0;
   virtual double getPetrolAmount() const = 0;
   virtual void setTankVolume(double p) = 0;
   virtual void setPetrolAmount(double p) = 0;
   virtual void arrive() = 0;
   virtual int leave() = 0;
 
   virtual ~IVehicle() {};
};
 
struct IBus : IVehicle {
    
   virtual int getPeopleCount() const = 0;
   virtual int getMaxPeople() const = 0;
   virtual void setPeopleCount(int p) = 0;
   virtual void setMaxPeople(int p) = 0;
};
 
struct ITruck : IVehicle {
 
   virtual double getCurrentLoad() const = 0;
   virtual double getMaxLoad() const = 0;
   virtual void setCurrentLoad(double p) = 0;
   virtual void setMaxLoad(double p) = 0;
};
 
 
class Vehicle : public IVehicle {
private:
    double petrol_amount;
    double tank_volume;
public:
    Vehicle(){
        this->petrol_amount = 200;
        this->tank_volume = 300;
    }
 
    Vehicle(double petrol_amount, double tank_volume){
        this->petrol_amount = petrol_amount;
        this->tank_volume = tank_volume;
    }
    
    void getTankVolume(double p){
        this->tank_volume = p;
    }
 
    void setPetrolAmount(double p){
        this->petrol_amount = p;
    }
 
    double getTankVolume() const{
        return this->tank_volume;
    }
 
    ~Vehicle(){}
 
    double getPetrolAmount() const{
        return this->petrol_amount;
    }
 
    void arrive(){
        people_on_base++;
        vehicles_on_base++; 
    }
 
    int leave(){
        if(petrol_on_base - this->tank_volume + this->petrol_amount < 0) return -1;
        if(people_on_base < 1) return -1;
        if(vehicles_on_base < 1) return -1;
        petrol_on_base -= (this->tank_volume - this->petrol_amount);
        this->petrol_amount = this->tank_volume;
        vehicles_on_base -= 1;
        people_on_base -= 1;
        return 0;
    }
};
 
class Bus: public IBus {
    int people;
    int max_people;
public:
    Bus(int people, int max_people, double petrol, double max_petrol){
        IBus::setPetrolAmount(petrol);
        IBus::setTankVolume(max_petrol);
        this->people = people;
        this->max_people = max_people;
    }
    ~Bus(){}
    int getPeopleCount() const{
        return people; 
    }
    int getMaxPeople() const{
        return max_people;
    }
    void setPeopleCount(int p){
        this->people = p; 
    }
    void getMaxPeople(int p){
        this->max_people = p;
    }
    void arrive(){
        people_on_base += this->people; 
        IVehicle::arrive();
        people=0;
    }
 
    int leave(){
        if(IVehicle::leave()!=0) return -1;
        if(this->max_people < people_on_base){
            this->people = this->max_people;
            people_on_base -= this->max_people;
        }
        else{
            this->people = people_on_base;
            people_on_base=0;
        }
        return 0;
    }
};
 
class Truck : public ITruck {
double load;
double max_load;
public:
    Truck(double load, double max_load, double petrol, double max_petrol){
        ITruck::setPetrolAmount(petrol);
        ITruck::setTankVolume(max_petrol);
        this->load = load;
        this->max_load = max_load;
    }
    double getCurrentLoad() const{
        return load;
    }
    double getMaxLoad() const{
        return max_load;
    }
    void setCurrentLoad(double p){
        this->load = p;
    }
    void setMaxLoad(double p){
        this->max_load = p;
    }
    void arrive(){
        IVehicle::arrive();
        goods_on_base += load;
        load = 0;
    }
 
    int leave(){
        if(IVehicle::leave()==-1) return -1;
        else if(goods_on_base<this->max_load) return -1;
        else goods_on_base -= max_load;
        load = max_load;
        return 0;
    }
};
 
class Expeditor : public IBus, public ITruck {
public:
    Expeditor(int people, int max_people, double load, double max_load, double petrol, double max_petrol){
        IBus::setMaxPeople(max_people);
        IBus::setPeopleCount(people);
        ITruck::setMaxLoad(max_load);
        ITruck::setCurrentLoad(load);
        IBus::setPetrolAmount(petrol);
        IBus::setTankVolume(max_petrol);
    }
    void arrive(){
        int people = people_on_base;
        int vehicle = vehicles_on_base;
        ITruck::arrive();
        people_on_base = people;
        vehicles_on_base = vehicle;
        IBus::arrive();
    }
    int leave(){
        int people = people_on_base;
        double p = petrol_on_base;
        int vehicle = vehicles_on_base;
        ITruck::leave();
        petrol_on_base = p;
        people_on_base = people;
        vehicles_on_base = vehicle;
        IBus::leave();
        return 0;
    }
};
 
 
 
int main() {
  IVehicle* v = new Bus(45, 56,45, 34);
}
1>d:\программирование\7plus b\4\4.cpp(203) : error C2259: 'Bus' : cannot instantiate abstract class

не могу никак избавится от этой ошибки. Что делать?
0
What a waste!
1607 / 1299 / 180
Регистрация: 21.04.2012
Сообщений: 2,727
11.05.2013, 00:01 8
Yandex, реализовать все виртуальные ф-ии.
Цитата Сообщение от Yandex Посмотреть сообщение
void getMaxPeople(int p){
setMaxPeople наверное?
Нереализованы:
C++
1
2
3
4
virtual double getTankVolume() const = 0;
virtual double getPetrolAmount() const = 0;
virtual void setTankVolume(double p) = 0;
virtual void setPetrolAmount(double p) = 0;
Цитата Сообщение от Yandex Посмотреть сообщение
IVehicle::arrive();
Что здесь будет вызвано?
0
3 / 3 / 1
Регистрация: 31.01.2013
Сообщений: 63
11.05.2013, 00:09  [ТС] 9
Цитата Сообщение от gray_fox Посмотреть сообщение
Что здесь будет вызвано?
ну я думал что должно принять тело класса Vehicle
0
What a waste!
1607 / 1299 / 180
Регистрация: 21.04.2012
Сообщений: 2,727
11.05.2013, 01:36 10
Цитата Сообщение от Yandex Посмотреть сообщение
ну я думал что должно принять тело класса Vehicle
Ну он же не наследует Vehicle. Надо либо включать экземпляр Vehicle как член класса, либо наследовать Vehicle.

Добавлено через 1 час 9 минут

Не по теме:

хэх какая жесть...

Кликните здесь для просмотра всего текста
C++
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <memory>
 
 
struct IVehicle {
 
   virtual double getTankVolume() const   = 0;
   virtual double getPetrolAmount() const = 0;
   virtual void setTankVolume(double p)   = 0;
   virtual void setPetrolAmount(double p) = 0;
   virtual void arrive() = 0;
   virtual int leave() = 0;
 
   virtual ~IVehicle() {};
};
 
struct IBus : virtual IVehicle {
    
   virtual int getPeopleCount() const = 0;
   virtual int getMaxPeople() const   = 0;
};
 
struct ITruck : virtual IVehicle {
 
   virtual double getCurrentLoad() const = 0;
   virtual double getMaxLoad() const     = 0;
};
 
 
int    people_on_base    = 100; 
int    vehicles_on_base  = 100; 
double petrol_on_base    = 100; 
double goods_on_base     = 100; 
 
 
class Vehicle : public IVehicle { 
 
public:
   Vehicle(double petrol_amount, double tank_volume)
         : petrol_amount(petrol_amount)
         , tank_volume(tank_volume)
         {}
 
   virtual double getTankVolume() const {
      return tank_volume;
   }
   virtual double getPetrolAmount() const {
      return petrol_amount;  
   }
   virtual void setTankVolume(double p) {
      tank_volume = p;
   }
   virtual void setPetrolAmount(double p) {
      petrol_amount = p;
   }
   virtual void arrive() {
      people_on_base++;
      vehicles_on_base++; 
   }
   virtual int leave() {
      if(petrol_on_base - this->tank_volume + this->petrol_amount < 0) return -1;
      if(people_on_base < 1) return -1;
      if(vehicles_on_base < 1) return -1;
      petrol_on_base -= (this->tank_volume - this->petrol_amount);
      this->petrol_amount = this->tank_volume;
      vehicles_on_base -= 1;
      people_on_base -= 1;
      return 0;
   }
 
private:
   double petrol_amount;
   double tank_volume;
};
 
namespace detail {
 
struct BusImpl {
 
   BusImpl(int people, int max_people)
         : people(people)
         , max_people(max_people)
         {}
 
   void arrive() {
      people_on_base += people;
      people = 0;
   }
   int leave() {
      if(max_people < people_on_base){
         people = max_people;
         people_on_base -= max_people;
      }
      else{
         people = people_on_base;
         people_on_base=0;
      }
      return 0;
   }
 
   int     people;
   int     max_people;
};
 
}
 
class Bus: public IBus {
 
public:
   Bus(int people, int max_people, double petrol, double max_petrol)
        : vehicle(petrol, max_petrol)
        , busImpl(people, max_people)
        {}   
 
   virtual double getTankVolume() const {
      return vehicle.getTankVolume();
   }
   virtual double getPetrolAmount() const {
      return vehicle.getPetrolAmount();
   }
   virtual void setTankVolume(double p) {
      vehicle.setTankVolume(p);
   }
   virtual void setPetrolAmount(double p) {
      vehicle.setPetrolAmount(p);
   }
   virtual void arrive() {
       vehicle.arrive();
       busImpl.arrive();
   }
   virtual int leave() {
      if (vehicle.leave() != 0) {
         return -1;
      } else {
         return busImpl.leave();
      }
   }
   virtual int getPeopleCount() const {
      return busImpl.people;
   }
   virtual int getMaxPeople() const {
      return busImpl.max_people;
   }
 
private:
   Vehicle         vehicle;
   detail::BusImpl busImpl;
};
 
namespace detail {
 
struct TruckImpl {
 
   TruckImpl(double load, double max_load)
         : load(load)
         , max_load(max_load)
         {}
 
   void arrive() {
      goods_on_base += load;
      load = 0;
   }
   int leave() {
      if(goods_on_base < this->max_load) return -1;
      else goods_on_base -= max_load;
      load = max_load;
      return 0;
   }
 
   double load;
   double max_load;
};
 
}
 
class Truck : public ITruck {
 
public:
   Truck(double load, double max_load, double petrol, double max_petrol) 
         : vehicle(petrol, max_petrol)
         , truckImpl(load, max_load)
         {}
 
   virtual double getTankVolume() const {
      return vehicle.getTankVolume();
   }
   virtual double getPetrolAmount() const {
      return vehicle.getPetrolAmount();
   }
   virtual void setTankVolume(double p) {
      vehicle.setTankVolume(p);
   }
   virtual void setPetrolAmount(double p) {
      vehicle.setPetrolAmount(p);
   }
   virtual void arrive() {
      vehicle.arrive();
      truckImpl.arrive();
   }
   virtual int leave() {
      if (vehicle.leave() == -1) {
         return -1;
      } else {
         return truckImpl.leave();
      }
   }
   virtual double getCurrentLoad() const {
      return truckImpl.load;
   }
   virtual double getMaxLoad() const {
      return truckImpl.max_load;
   }
 
private:
   Vehicle            vehicle;
   detail::TruckImpl  truckImpl;
};
 
class Expeditor : public IBus, public ITruck {
 
public:
   Expeditor(int people, int max_people, double load, double max_load, double petrol, double max_petrol)
         : vehicle(petrol, max_petrol)
         , busImpl(people, max_people)
         , truckImpl(load, max_load)
         {}
 
   virtual double getTankVolume() const {
      return vehicle.getTankVolume();
   }
   virtual double getPetrolAmount() const {
      return vehicle.getPetrolAmount();
   }
   virtual void setTankVolume(double p) {
      vehicle.setTankVolume(p);
   }
   virtual void setPetrolAmount(double p) {
      vehicle.setPetrolAmount(p);
   }
   virtual void arrive() {
      int people = people_on_base;
      int vehicles = vehicles_on_base;
 
      vehicle.arrive();
      truckImpl.arrive();
 
      people_on_base = people;
      vehicles_on_base = vehicles;
 
      busImpl.arrive();
   }
   virtual int leave() {
      int people = people_on_base;
      double p = petrol_on_base;
 
      int vehicles = vehicles_on_base;
 
      int vehicleLeaveCode = vehicle.leave();
      if (vehicleLeaveCode != -1) {
         truckImpl.leave();
      }
 
      petrol_on_base = p;
      people_on_base = people;
      vehicles_on_base = vehicles;
      
      if (vehicleLeaveCode != 0) {
         busImpl.leave();
      }
 
      return 0;
   }
   virtual int getPeopleCount() const {
      return busImpl.people;
   }
   virtual int getMaxPeople() const {
      return busImpl.max_people;
   }
   virtual double getCurrentLoad() const {
      return truckImpl.load;
   }
   virtual double getMaxLoad() const {
      return truckImpl.max_load;
   }
 
private:
   Vehicle             vehicle;
   detail::BusImpl     busImpl;
   detail::TruckImpl   truckImpl;
};
 
 
 
int main() {
  std::auto_ptr<IVehicle> vehicle(new Vehicle(45, 67));
  std::auto_ptr<IVehicle> bus(new Bus(45, 56, 45, 34));
  std::auto_ptr<IVehicle> truck(new Truck(50, 5, 654, 234));
  std::auto_ptr<IVehicle> expeditor(new Expeditor(45, 56, 56, 5, 65, 6));
}

0
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
11.05.2013, 01:36
Помогаю со студенческими работами здесь

Наследование классов
В общем, стоит задача: Ввести текст из 12 строк, далее его отредактировать по условию задачи. ...

Наследование классов
Всем привет! У меня проблема. Вот задание: 1. Разработать следующие классы: • базовый класс...

Наследование классов
#include &quot;stdafx.h&quot; #include &lt;stdio.h&gt; #include &lt;conio.h&gt; class PARENT { public: void...

наследование классов
нужно создать программу состоящую из классов по принципу наследования то есть отец-сын-внук...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
10
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2023, CyberForum.ru