[Win 64] confused with constructors

I have to make changes to a previous lab I've done according to the instructor's requirements, and I'm totally confused with how to create the new class (StringOfCar).
I have tried and do some of the coding for the new lab already, but the there are a lot of mistakes with the new copy instructor, can anybody please give me some hints and advice? Thank you.

--------- Code of previous lab ---------
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
#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sstream>
#include <fstream>
using namespace std;

class Car{
//set data of the class as private
private:
    string reportingMark;    // string variable to store car member reportingMark
    int carNumber;           // int variable to store car member carNumber
    string kind;             // string variable to store car member kind
    bool loaded;             // boolean variable to store car member loaded
    string destination;      // string variable to store car member destination
public:
    //Constructors
    // Default constructor
    Car()
    {
        reportingMark = ""; // an empty string
        carNumber = 0;
        kind = "other";
        loaded = false;
        destination = "NONE";
    }

    // Copy constructor
    Car(const Car &obj)
    {
        reportingMark = obj.reportingMark;
        carNumber = obj.carNumber;
        kind = obj.kind;
        loaded = obj.loaded;
        destination = obj.destination;
    }

    // A constructor that accepts five constant reference parameters and calls setUp member function
    Car(string mark, int num, string make, bool state, string dest);

    // destructor
    ~Car()
    {
    };

    // mutator member functions
    void setReportingMark(string mark);
    void setCarNumber(int num);
    void setKind(string make);
    void setLoaded(bool state);
    void setDestination(string dest);

    // accessor member functions
    string getReportingMark() const;
    int getCarNumber() const;
    string getKind() const;
    bool getLoaded() const;
    string getDestination() const;

    // member functions
    void setUp(string, int, string, bool, string, Car);
    void output(Car);

    // Car operator=
    Car &operator=(const Car &carB);
};

//function definition
void Car::setReportingMark(string mark)
    {
        reportingMark = mark;
    };
void Car::setCarNumber(int num)
    {
        carNumber = num;
    };
void Car::setKind(string make)
    {
        kind = make;
    };
void Car::setLoaded(bool state)
    {
        loaded = state;
    };
void Car::setDestination(string dest)
    {
        destination = dest;
    };

string Car::getReportingMark() const
    {
        return reportingMark;
    };
int Car::getCarNumber() const
    {
        return carNumber;
    };
string Car::getKind() const
    {
        return kind;
    };
bool Car::getLoaded() const
    {
        return loaded;
    };
string Car::getDestination() const
    {
        return destination;
    };

// Car  operator=  **************************************************
Car & Car::operator=(const Car & carB)
{
    reportingMark = carB.reportingMark;
    carNumber     = carB.carNumber;
    kind          = carB.kind;
    loaded        = carB.loaded;
    destination   = carB.destination;

    return * this;

}
// function prototypes
void input ();
void output (Car);
void setUp (string, int, string, bool, string, Car&);

class StringOfCars{
    static const int ARRAY_MAX_SIZE = 10;
    Car *object[ARRAY_MAX_SIZE];
    int currentCar;

    // default constructor
    StringOfCars();

    // copy constructor
    StringOfCars(const Car &);

    // destructor
    ~StringOfCars();

    // push function

    // pop function

    // output function
};

/* ********** StringOfCars member functions ********** */
// default constructor
StringOfCars()
{
    static const int ARRAY_MAX_SIZE = 10;
    Car *object[ARRAY_MAX_SIZE];
    int curNum = 0;
}
// copy constructor
StringOfCars(const StringOfCars &obj)
{
    static const int ARRAY_MAX_SIZE = 10;
    Car *object[ARRAY_MAX_SIZE] = new object[ARRAY_MAX_SIZE];

}


// destructor
~StringOfCars()

int main()
{
    // call input function to read the data from user
    input();

    return 0;
}

/* ******************** output ********************
print all data
*/
void output(Car car)
{
    cout << "ReportingMark:\t ";
    // convert string to character array
    char u;
    char str[(car.getReportingMark()).length()];
    strcpy(str, (car.getReportingMark()).c_str());

    // change the lowercase to uppercase letter by letter
    for(int n = 0; n < (car.getReportingMark()).length(); n++)
    {
        u = str[n];
        putchar (toupper(u));
    }
    cout << endl;

    cout << "CarNumber:\t " << car.getCarNumber() << endl;
    cout << "Kind:\t\t " << car.getKind() << endl;
    if(car.getLoaded() == true)
    {
        cout << "Loaded:\t\t true\n";
        cout << "Destination:\t " << car.getDestination() << endl;
    }

    else if(car.getLoaded() == false)
    {
        cout << "Loaded:\t\t flase\n";
        if(car.getDestination() == "none" || car.getDestination() == "NONE")
        cout << "Destination:\t NONE\n";
        else
        cout << "Destination:\t " << car.getDestination() << endl;
    }
    cout << endl;
}

/* ******************** setUp ********************
pass data of variables got from input function to variables declared in class car
*/
void setUp(string mark, int num, string make, bool state, string dest, Car &car1)
    {
        // call the mutator member functions to pass the data to the class
        car1.setReportingMark(mark);
        car1.setCarNumber(num);
        car1.setKind(make);
        car1.setLoaded(state);
        car1.setDestination(dest);
    };

/* ******************** input ********************
read data from a file
*/
void input()
{
    // define variables
    string mark;
    int num;
    string make;
    bool state;
    string dest;

    // declare the string "Type" to get the first column of the data out of the way
    string Type;

    // declare a string for reading boolean type data
    string st;

    // declare and open the file
    ifstream inputFile;
    inputFile.open("inputFile.txt");

    // declare a Car object
    Car temp;

    // print error message and exit the program if the file does not exist
    if(!inputFile) // if the file does not exist
    {
        fputs("Fail to open the file", stderr);
        exit(0); // exit the program
    }

    else
    {
        // use while loop to read the file line by line;
        // the peek function returns EOF is no next character exists
        while(inputFile.peek() != EOF)
        {
            // read data from file
            inputFile >> Type;
            inputFile >> mark;
            inputFile >> num;
            inputFile >> make;

            // declare what false and true readings from the file means
            inputFile >> st;
            if(st == "false")
                state = false;
            else
                state = true;

            // get rid of the leading whitespace before using getline function to read the destination
            while(inputFile.peek() == ' ')
            {
                inputFile.get();
            }
            getline(inputFile, dest);

            // call the setUp function to pass the data to the class
            setUp(mark,num,make,state,dest,temp);

            // call the output function to print the data
            output(temp);
        }
    }

    // close the file
    inputFile.close();

    cout << endl;
}
Last edited on
sorry, the system only allows 9000 characters maximum for one post, so here is the lab requirements:


--------- requirements for new lab ---------
Copy the following operator= overload member function that returns the
left hand operator by reference:

// Car operator= **************************************************
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber = carB.carNumber;
kind = carB.kind;
loaded = carB.loaded;
destination = carB.destination;

return * this;
}

Several cars coupled together are referred to as a string of cars.

Create another class called StringOfCars, containing:

* a pointer to an array of Car objects in the heap.
* a static const int ARRAY_MAX_SIZE set to 10.
* an int size containing the current number of Cars in the array.
* a default constructor which gets space for the array in the heap,
and sets the the size to zero.
* a copy constructor which gets new space in the heap for the array
and copies all the Car objects.
* a destructor which returns the space to the heap.
* a push function which adds a car to the string of cars.
* a pop function which removes a car from the string of cars, Last In
First Out (LIFO).
* an output function which prints a heading for each car:
car number n where n is the position in the array starting from 1
for the first car and then uses the Car output function to print the
data for each car Or, if the array is empty prints: NO cars

Order of functions in the code:

1. main
2. Car member functions
1. Car constructors in the order
1. default constructor
2. copy constructor
3. other constructors
2. output
3. setUp
4. operator=
3. StringOfCars member functions
1. Car constructors in the order
1. default constructor
2. copy constructor
2. destructor
3. output
4. push
5. pop
4. operator== friend of Car
5. input

Put an eye catcher before the beginning of each function, class, and the
global area:
// class name function name comment(if any)
*************************************************

Modify the main function to do the following tests:

1. Test the Car operator= function.
Before the call to the input function:
Print: TEST 1
Creat a Car object named car1 with initial values:
reportingMark: SP
carNumber: 34567
kind: box
loaded: true
destination: Salt Lake City
Create a Car object named car2 which is a copy of car1
Print car2

2. Test the StringOfCar push function.
Change the input function to have a parameter that is a reference to
a StringOfCars.
Add to main:
Create a default StringOfCars object named string1.
Print: TEST 2
Pass string1 to the input function.
Use the same file as in problem D1.
In the input function, just after creating the car, push it on to
string1.
Remove the print of the car in the input function.
Print: STRING 1
In the main function, after using the input function, print string1.

3. Test the StringOfCars pop function.
Add to main:
Print: TEST 3
Create a car named car3.
Pop one car from string1 into car3.
Print: CAR 3
Print car3.
Print: STRING 1
Then print the contents of string1 again
closed account (jvqpDjzh)
please, format your code: select your code and press the button <> on the right side. Just an advice!
sorry about it, I have formatted my code :P
Topic archived. No new replies allowed.