Help

I am trying to divide this code into classes. I am learning how to program and this is for my personal use. If anyone can help it will be greatly appreciated.

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
299
300
301
302
const int NUM_REPAIR_BAYS = 4;

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <queue>

using namespace std;

// Vehicle info
struct Vehicle
{
   int id; // -Id number   
   string Type; // V1, V2, etc) 
   bool Armour[4]; // bool: F,S,R,P) 
   string Weapons; // (list of strings w1, w2, w3...etc) 
   // Drive train 
   bool Transmission; 
   bool Differential; 
   char Engine;  // (O,N,D) O-operational N-No engine D-Damaged 
};

// functions

// create a Vehicle
Vehicle* create_vehicle(int id);

// print out vehicle details
void print(Vehicle* v);

// return available vehicle
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq);

// return repair bay availabe
int repair_bay_available(Vehicle* repairBay[]);

// return true if vehicle pased test
bool passed_test(Vehicle* v);

// return false if vehicle pass test
bool failed_test(Vehicle* v);

int main()
{
  int i;
  queue<Vehicle*> q; // vehicle que
  queue<Vehicle*> pq; // priority queue
  Vehicle* repair_bay[NUM_REPAIR_BAYS]={0}; // repair bay
  vector<Vehicle*> salvage_yard; // salvage yard
  vector<Vehicle*> active_duty; // active duty
  Vehicle* v;
  int id = 0;
  
  srand((unsigned int)time(0));

  for(int t=0;t<5;t++)
  {
// Create a number (0-4) new random vehicles per day
int n = rand() % 4;


// As the new vehicles are created they are placed into a queue
for(i=0;i<n;i++) 
{
     v = create_vehicle(id++);
     cout << "creating vehicle: " << endl;
     print(v);
     q.push(v);
}

// The members of the queue (#max/time) are then processed by a "yard manager
// The front of the queue is popped and added to: Salvage Yard
// or  Repair Bay if available Vector[4] 

        v = vehicle_available(q,pq):

// get available vehicle
if(v != NULL)
{
     cout << "processing vehicle: ";
     print(v);
     i = repair_bay_available(repair_bay);
          if(i >= 0)
               {
	cout << "Vehicle sent to repair bay #" << (i+1) << endl;
	repair_bay[i] = v;
               }
          else
	{
                cout << "All repair bays filled, vehicle sent to salvage yard" << endl;
	salvage_yard.push_back(v);
	}
		   
// When a vehicle in a bay is finished it is then run to testing, each component is tested, 
// if all pass the vehicle is returned to duty else returned to a priority queue which will pre-empt 
// the repair queue (fastest first) 

// for each repair bay
for(i=0;i<NUM_REPAIR_BAYS;i++)
{
     v = repair_bay[i];
     if(v != NULL)
	{
                if(passed_test(v)) // passed test? 
	     {
	      cout << "vehicle passed repair test, vehicle sent to active duty" << endl;
	      active_duty.push_back(v);
	      repair_bay[i]=0;
	      }
	 else  if(failed_test(v)) // failed test
	     {
	      cout << "vehicle failed repair test, vehicle sent to priority queue" << endl;
	      pq.push(v);
	      repair_bay[i]=0;
	     }
	}
}
}
}

     cout << endl;

// print out whats in queue
cout << "Vehicles in left in Queue: " << endl;
while(q.size() > 0)
      {
       print(q.front());
       q.pop();
       }
      cout << endl;

// print out whats in priority queue
cout << "Vehicles in left in priority Queue: " << endl;
     while(pq.size() > 0)
         {
          print(pq.front());
          pq.pop();
          }
      cout << endl;

// print out whats in storage bay
cout << "Vehicles in Repair Bay: " << endl;
     for(i=0;i<NUM_REPAIR_BAYS;i++)
           if(repair_bay[i] != 0) 
           print(repair_bay[i]);
           cout << endl;

// print out whats in salvage yard
cout << "Vehicles in Salvage Yard: " << endl;
     for(i=0;i<salvage_yard.size();i++)
           print(salvage_yard[i]);
           cout << endl;

// print out whats in active duty
cout << "Vehicles in Active Duty: " << endl;
     for(i=0;i<active_duty.size();i++)
           print(active_duty[i]);
           cout << endl;


return 0;
}

//************************************************************************************************************

// create a Vehicle
Vehicle* create_vehicle(int id)
{

   Vehicle* v = new Vehicle;
	
   v->id  =id;   

   int x = rand() % 4;

	   // V1, V2, etc) 
      v->Type =  "V1"; 
   if(x == 1)
      v->Type =  "V2"; 
   if(x == 2)
      v->Type =  "V3"; 
	if(x == 3)
      v->Type =  "V4"; 


   v->Armour[0] = true; // bool: F,S,R,P) 
   v->Armour[1] = rand() % 2==0;
   v->Armour[2] = rand() % 2==0;
   v->Armour[3] = rand() % 2==0;

   x = rand() % 3 + 1;

   if(x == 1)
      v->Weapons  = "w1";  // (list of strings w1, w2, w3...etc) 
   if(x == 2)
      v->Weapons = "w1, w2";
   if(x == 3)
      v->Weapons = "w1, w2, w3";
   if(x == 4)
      v->Weapons = "w1, w2, w3, w4";
   
    // Drive train 
   v->Transmission = rand() * 2 == 0;
   v->Differential = rand() * 2 == 0;

   x = rand() % 3;

   if(x == 0)
      v->Engine = 'O';  // (O,N,D) O-operational N-No engine D-Damaged 
   if(x == 1)
	  v->Engine = 'N';
   if(x == 2)
	  v->Engine = 'D';

   return v;

}

//************************************************************************************************************

// print out vehicle details
void print(Vehicle* v)
{

	cout << v->id << " " << v->Type << " ";
	if(v->Armour[0]) cout << "F";
	if(v->Armour[1]) cout << "S";
	if(v->Armour[2]) cout << "R";
	if(v->Armour[3]) cout << "P";
    cout << " ";
    cout << v->Weapons << " ";
	if(v->Transmission) cout << "Transmission";
	if(v->Differential) cout << "Differential";
	cout << v->Engine << endl;
}

//************************************************************************************************************

// return vehicle avaiable
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq)
{

	Vehicle* v=NULL;

	// check priority queue first for vehicles

		if(pq.size() > 0)
		{
			v = pq.front();
			pq.pop();
		}

		// check queue for vehicles
		else if(q.size() > 0)
		{
			v = q.front();
			q.pop();
		}

    return v;
}

//************************************************************************************************************

// return repair bay availabLe
int repair_bay_available(Vehicle* repair_bay[])
{
	for(int i=0;i<NUM_REPAIR_BAYS;i++)
	{
		if(repair_bay[i] == NULL)
			return i;
	}

	return -1;
}

//************************************************************************************************************

// return true if vehicle pased test
bool passed_test(Vehicle* v)
{
     // 50 % vehicle pass
	if(rand()%100>50)
	{
		// set repaired
        v->Transmission=false; 
        v->Differential=false; 
        v->Engine='O';
		return true;
	}

	else return false;
}

//************************************************************************************************************

// return false if vehicle pass test
bool failed_test(Vehicle* v)
{
     return rand()%100<20; 
}
Last edited on
As you should know, classes are an extension of structs, so you're off to a good start by organizing your vehicle information in a struct.

create_vehicle becomes your constructor.
Only the syntax of print changes.
This should give you a start:

vehicle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Vehicle info
class Vehicle
{  int id; // -Id number
   string Type; // V1, V2, etc)
   bool Armour[4]; // bool: F,S,R,P)
   string Weapons; // (list of strings w1, w2, w3...etc)
   // Drive train
   bool Transmission;
   bool Differential;
   char Engine;  // (O,N,D) O-operational N-No engine D-Damaged

public:
   Vehicle (int id);
   void print ();
};


Constructor:
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
// create_vehicle becomes the constructor 
Vehicle::Vehicle (int _id)
{	id = _id;	// Note the underscore to distinguish the two names.
  int x = rand() % 4;

       // V1, V2, etc)
    Type =  "V1";
    if(x == 1)
      Type =  "V2";
   if(x == 2)
      Type =  "V3";
    if(x == 3)
      Type =  "V4";


   Armour[0] = true; // bool: F,S,R,P)
   Armour[1] = rand() % 2==0;
   Armour[2] = rand() % 2==0;
   Armour[3] = rand() % 2==0;

   x = rand() % 3 + 1;

   if(x == 1)
      Weapons  = "w1";  // (list of strings w1, w2, w3...etc)
   if(x == 2)
      Weapons = "w1, w2";
   if(x == 3)
      Weapons = "w1, w2, w3";
   if(x == 4)
      Weapons = "w1, w2, w3, w4";

    // Drive train
   Transmission = rand() * 2 == 0;
   Differential = rand() * 2 == 0;

   x = rand() % 3;

   if(x == 0)
      Engine = 'O';  // (O,N,D) O-operational N-No engine D-Damaged
   if(x == 1)
      Engine = 'N';
   if(x == 2)
      Engine = 'D';
}


print
1
2
3
4
5
6
7
8
9
10
11
12
void Vehicle::print()
{	cout << id << " " << Type << " ";
    if(Armour[0]) cout << "F";
    if(Armour[1]) cout << "S";
    if(Armour[2]) cout << "R";
    if(Armour[3]) cout << "P";
    cout << " ";
    cout << Weapons << " ";
    if(Transmission) cout << "Transmission";
    if(Differential) cout << "Differentisl";
    cout << Engine << endl;
}


Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Compiling: main.cpp
main.cpp: In function 'int main()':
main.cpp:48: error: 'create_vehicle' was not declared in this scope
main.cpp:50: error: 'print' was not declared in this scope
main.cpp:64: error: 'print' was not declared in this scope
main.cpp:124: error: 'print' was not declared in this scope
main.cpp:134: error: 'print' was not declared in this scope
main.cpp:145: error: 'print' was not declared in this scope
main.cpp:151: warning: comparison between signed and unsigned integer expressions
main.cpp:152: error: 'print' was not declared in this scope
main.cpp:158: warning: comparison between signed and unsigned integer expressions
main.cpp:159: error: 'print' was not declared in this scope
vehicle.h: In function 'bool passed_test(Vehicle*)':
vehicle.h:10: error: 'bool Vehicle::Transmission' is private
main.cpp:222: error: within this context
vehicle.h:11: error: 'bool Vehicle::Differential' is private
main.cpp:223: error: within this context
vehicle.h:12: error: 'char Vehicle::Engine' is private
main.cpp:224: error: within this context
Process terminated with status 1 (0 minutes, 0 seconds)
14 errors, 2 warnings
 
Last edited on
Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Compiling: main.cpp
main.cpp: In function 'int main()':
main.cpp:48: error: 'create_vehicle' was not declared in this scope
main.cpp:50: error: 'print' was not declared in this scope
main.cpp:64: error: 'print' was not declared in this scope
main.cpp:124: error: 'print' was not declared in this scope
main.cpp:134: error: 'print' was not declared in this scope
main.cpp:145: error: 'print' was not declared in this scope
main.cpp:151: warning: comparison between signed and unsigned integer expressions
main.cpp:152: error: 'print' was not declared in this scope
main.cpp:158: warning: comparison between signed and unsigned integer expressions
main.cpp:159: error: 'print' was not declared in this scope
vehicle.h: In function 'bool passed_test(Vehicle*)':
vehicle.h:10: error: 'bool Vehicle::Transmission' is private
main.cpp:222: error: within this context
vehicle.h:11: error: 'bool Vehicle::Differential' is private
main.cpp:223: error: within this context
vehicle.h:12: error: 'char Vehicle::Engine' is private
main.cpp:224: error: within this context
Process terminated with status 1 (0 minutes, 0 seconds)
14 errors, 2 warnings
 


I am a little confused because it keeps saying that print and create_vehicle is not declared when it seems like it is and how do I gain access to the private data?

Thanks for the help by the way.
Last edited on
In vehicle.cpp:
1
2
3
4
#include <iostream>
#include <cstdlib>
#include <string>
#include "vehicle.h" 

You need to include <string>, since vehicle.h uses it.

Line 48 should be:
 
  v = new Vehicle (id++);

create_vehicle is replaced by the invoking the constructor of the Vehicle class.

print is now a member function, so it is called as follows:
 
v->print();

This change needs to be made each place print is called.

I gave you the constructor and print as examples expecting you to take those examples and apply them to the rest of the program. I'm not here to write your program for you. You need to learn how to use classes.

Since passed_test also applies to a vehicle, it should also be a member function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// return true if vehicle pased test
bool Vehicle::passed_test()
{
     // 50 % vehicle pass
    if(rand()%100>50)
    {
        // set repaired
        Transmission=false;
        Differential=false;
        Engine='O';
        return true;
    }

    else return false;
}

Note that since passed_test is now a member function, it has access to Vehicle's private data.

Okay. I get it now.

I know you are not here to write my code. I was just using your examples to get an idea of what to do and to see if everything works. Thanks for all your help.
Do I also have to make classes or another file for the print functions that takes parameters?
Example:

1
2
3
4
5
 v->print(pq.front())

 v->print(pq.front())

v->print(repair_bay[i])


I notice that the print function in vehicle header file does not take parameters.
Last edited on

Do I also have to make classes for the print functions that takes parameters?


print would not be a class. print is a function within a class.
Since front returns a Vehicle pointer and repair_bay is an array of Vehicle pointers, you can do the following:
1
2
pq.front()->print();
repair_bay[i]->print();

Thank you again
Topic archived. No new replies allowed.