Car outputs?

This is my code
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
#include<iostream>
#include<string>
#include<vector>

using namespace std;
//Car class

//Defined enum here
enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger};
//Defined array of strings
string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"};
class Car
{
protected: //changed to protected
string reportingMark;
int carNumber;
Kind kind; //changed to Kind
bool loaded;
string destination;

public:

//Defined setKind function
Kind setKind(string knd)
{
   for(int i=0;i<8;i++) //repeat upto 8 kinds
   {
       if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
        kind=(Kind)i; //setup that kind
   }
   return kind;
}
  
  
  
//Default constructor
Car()
{
}
//Parameterized constructor
Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = setKind(_kind);
loaded = _loaded;
destination = _destination;
}

//Destructor
~Car()
{
}

//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind; //calls setup function
loaded = other.loaded;
destination = other.destination;

}

Car operator=(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this;
}
friend bool operator==(Car c1,Car c2);

void setUp() //Implemented setup() code here
{
string knd;
//cout<<"Implementing Progress\n";
cout<<"Enter Type/Kind of car : ";
cin>>knd;
setKind(knd);
}

void output()
{
cout<<"Reporting Mark: "<<reportingMark<<endl;
cout<<"Card Number: "<< carNumber<<endl;
cout<<"Kind: "<< KIND_ARRAY[kind]<<endl; //Modified printing of Kind
cout<<"Loaded: "<< loaded<<endl;
cout<<"Destination: "<< destination<<endl;
}
};
//Friend function
bool operator==(Car c1,Car c2)
{
if(c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
return true;
}
else
{

return false;
}

}

//Class FreightCar

class FreightCar:public Car
{
public:
//Default constructor
FreightCar()
{
   Car();
}
//Parameterized constructor
FreightCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
Car(_reportingMark,_carNumber,_kind,_loaded,_destination); //calling base class constructor
}

//Defined virtual setKind() function
virtual Kind setKind(string knd)
{
   for(int i=0;i<8;i++) //repeat upto 8 kinds
   {
       if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
        kind=(Kind)i; //setup that kind
   }
   return kind;
}

//Virtual Destructor
virtual ~FreightCar()
   {
   }
};


//Class PassengerCar here

class PassengerCar:public Car
{
public:
//Default constructor
PassengerCar()
{
   Car();
}
//Parameterized constructor
PassengerCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
Car(_reportingMark,_carNumber,_kind,_loaded,_destination); //calling base class constructor
}

//Defined virtual setKind() function
virtual Kind setKind(string knd)
{
   for(int i=0;i<8;i++) //repeat upto 8 kinds
   {
       if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
        kind=(Kind)i; //setup that kind
   }
   return kind;
}

//Virtual Destructor
virtual ~PassengerCar()
   {
   }
};

/* ********** StringOfCars member functions ********** */
/* dont use StringOfCar in this
class StringOfCar
{
private:
Car *cars; //pointer of cars
int carCount; //defined additional data member carCount
public:
//Default constructor
StringOfCar()
{
   cars=NULL;
   carCount=0;
}
//Parameterized constructor
StringOfCar(Car *_cars)
{
cars = _cars;
carCount=0;
}

//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
carCount=0;
}

//Destructor
~StringOfCar()
{
   //deleting each car allocated
   for(int i=0;i<carCount;i++)
   delete cars;
}

int getCount() //UFD here defined here
{return carCount;}

void output()
{
for(int i=0; i<carCount; i++)
{
cars[i].output();
}
}

void push(Car car)
{
  
cars[carCount++]=car;
}

Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of cars
return lastObj;
}

void input()
{
cout<<"In progress!\n";
}
};*/

int main()
{

//Removed code and added modified code here
Car car1("SLSF",46871,"wrecker",true,"Memphis");
car1.output();
FreightCar car2("MP",98765,"gondola",true,"Saint Louis");
car2.output();
PassengerCar car3("PAPX",145,"combine",true,"Tucson");
car3.Car::output();
return 0;

}

So, I'm having a bit of trouble. When I run the program, it only outputs car1 but not the rest. Any ideas?
In your parametrized constructors in your derived classes you're attempting to use the base-class constructor to initialize the members. You must do this in the constructor initialization list instead of the constructor's body.
So this:
1
2
3
4
5
//Parameterized constructor
FreightCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
    Car(_reportingMark,_carNumber,_kind,_loaded,_destination); //calling base class constructor
}

becomes this:
1
2
3
4
5
//Parameterized constructor
FreightCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
    : Car(_reportingMark,_carNumber,_kind,_loaded,_destination); //calling base class constructor
    {
    }
FYI, your "Kind" enum and your KIND_ARRAY don't match. 'other' and 'box' are separate in the enum, but they're are put in one string in the array.
"other,box" should be "other","box"
Topic archived. No new replies allowed.