Question: C++ In this problem you will change the StringOfCars class so it has an array of pointers to objects, rather than an array of objects

C++

In this problem you will change the StringOfCars class so it has an array of pointers to objects, rather than an array of objects themselves. This will allow us to have a string of cars that contains Car, FreightCar, and PassengerCar objects all in the same string of cars. This works because a pointer of type Car * can be made to point to Car objects as well as point to the child FreightCar and PassengerCar objects.

Remove the call to the output member function from the three build funtions: buildCar, buildFreightCar, and buildPassengerCar.

Because you have pointers of type Car * that may point to any one of the three types of objects, there is a problem. The system does not know what type object will be encountered until execution time. That means a system is needed so the functions that are overridden need to have a mechanism to select the correct version of the function at execution time, rather than having it fixed at compile time. This is done with the virtual declaration. To do this make the declaration of the setKind and the declaration of the ~Car functions virtual in the Car class. This is only done in the declaration, not the definition of the function. This is only done in the parent class, not the children classes.

To change the class StringOfCars, comment out all the code in the member functions of the StringOfCars class and fix them one or two at a time in the following order. These are similar to the previous functions, but changed to allow for the fact that we are putting pointers to cars in the array.

Build the default constructor first. Create a default string of cars in main.

Build an output function, similar to the old one, but dereferrencing the pointers.

Write a push function which adds a car to the string of cars. It takes a Car by constant reference, allocates space in the heap, makes a copy of the Car, and puts the pointer to the Car in the array.

Write a copy constructor similar to the old one, but it gets space for each car and copies each one, as well as getting space for the array.

omit the pop member function.

Add to the build functions a call to push the objects onto the string of cars.

Remove the output from the build functions.

Test the copy constructor by making stringOfCars2 in the stack for main that is a copy of stringOfCars1.

Print stringOfCars2.

This is 5.1

#include #include #include #include #include #include using namespace std; enum Kind { business, maintenance, tender, other, //car kind box, tank, flat, hopper, otherFreight, //FreightCar kind chair, sleeper, otherPassenger, combine //PassengerCar kind };

const string KIND_ARRAY[] = { "business", "maintenance", "tender", "other", //car kind "box", "tank", "flat", "hopper", "otherFreight", //FreightCar kind "chair", "sleeper", "otherPassenger", "combine" //PassengerCar kind };

class Car { protected: static const int Num_of_Width = 22; string reportingMark; int carNumber; Kind kind; bool loaded; string destination; public: Car() { setUp("", 0, "other", false, "NONE"); } Car(const Car &car1) { reportingMark = car1.reportingMark; carNumber = car1.carNumber; kind = car1.kind; loaded = car1.loaded; destination = car1.destination; } Car(string reportingMark, int carNumber, string kind, bool loaded, string destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } virtual void setKind(const string &k); void setUp(string reportingMark, int carNumber, string kind, bool loaded, string destination); void output(); Car & operator=(const Car & carB); ~Car(){}; friend bool operator==(const Car &car1,const Car &car2); };

class StringOfCars { private: static const int ARRAY_MAX_SIZE = 10; Car *carArr; int size; public: StringOfCars(); StringOfCars(const StringOfCars &soc); ~StringOfCars(); void output(); void push(const Car &c); Car pop(); };

class FreightCar : public Car { public: FreightCar() { setUp("", 0, "otherFreight", false, "NONE"); } FreightCar(const FreightCar &car1) : Car(car1) { } FreightCar(string reportingMark, int carNumber, string kind, bool loaded, string destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string &k);

};

class PassengerCar : public Car { public: PassengerCar() { setUp("", 0, "otherPassenger", false, "NONE"); } PassengerCar(const PassengerCar &car1) : Car(car1) { } PassengerCar(string reportingMark, int carNumber, string kind, bool loaded, string destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string &k); };

Car buildCar(string repMark, int carNo, string kind, bool loaded, string dest); FreightCar buildFreightCar(string repMark, int carNo, string kind, bool loaded, string dest); PassengerCar buildPassengerCar(string repMark, int carNo, string kind, bool loaded, string dest); void input(StringOfCars &str);

int main() { cout << " TEST 1 " << endl; FreightCar car1("SP", 34567, "box", true, "Salt Lake City"); Car car2 = car1; car2.output(); StringOfCars string1; cout << "TEST 2 " << endl; input(string1); cout << "STRING 1" << endl; string1.output(); /*cout << "TEST 3 " << endl; Car car3 = string1.pop(); cout << "CAR 3" << endl; car3.output(); cout << "STRING 1" << endl; string1.output();*/ }

void Car::output() { cout << setw(Num_of_Width) << "Reporting Mark:" << setw(Num_of_Width) << reportingMark << endl; cout << setw(Num_of_Width) << "Car Number: " << setw(Num_of_Width) << carNumber << endl; cout << setw(Num_of_Width) << "Kind: " << setw(Num_of_Width) << KIND_ARRAY[kind] << endl; cout << setw(Num_of_Width) << "Loaded: " << setw(Num_of_Width); if(loaded) cout << "Loaded" << endl; else cout << "Not Loaded" << endl; cout << setw(Num_of_Width) << "Destination: " << setw(Num_of_Width) << destination << endl; } void Car::setKind(const string &k) { if(k == KIND_ARRAY[business]) kind = business; else if(k == KIND_ARRAY[maintenance]) kind = maintenance; else if(k == KIND_ARRAY[tender]) kind = tender; else kind = other; } void Car::setUp(string reportingMark, int carNumber, string kind, bool loaded, string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; setKind(kind); this->loaded = loaded; this->destination = 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; } StringOfCars::StringOfCars() { carArr = new Car[ARRAY_MAX_SIZE]; size = 0; } StringOfCars::StringOfCars(const StringOfCars &soc) { carArr = new Car[ARRAY_MAX_SIZE]; size = 0; for(int i = 0; i < soc.size; i++) push(soc.carArr[i]); } StringOfCars::~StringOfCars() { delete []carArr; } void StringOfCars::output() { if(size == 0) { cout << "No Cars " << endl; return; } cout << " ******* String of cars ********" << endl; for(int i = 0; i < size; i++) { cout << (i+1) << "." << endl; carArr[i].output(); cout << endl; } cout << " *******************************" << endl; } void StringOfCars::push(const Car &c) { if(size < ARRAY_MAX_SIZE) carArr[size++] = c; } Car StringOfCars::pop() { return carArr[--size]; } bool operator==(const Car &car1,const Car &car2) { if((car2.reportingMark.compare(car1.reportingMark) == 0) && car2.carNumber == car1.carNumber) return true; else return false; }

void FreightCar::setKind(const string &k) { if(k == KIND_ARRAY[box]) kind = box; else if(k == KIND_ARRAY[tank]) kind = tank; else if(k == KIND_ARRAY[flat]) kind = flat; else if(k == KIND_ARRAY[hopper]) kind = hopper; else kind = otherFreight; }

void PassengerCar::setKind(const string &k) { if(k == KIND_ARRAY[chair]) kind = chair; else if(k == KIND_ARRAY[sleeper]) kind = sleeper; else if(k == KIND_ARRAY[otherPassenger]) kind = otherPassenger; else if(k == KIND_ARRAY[combine]) kind = combine; }

Car buildCar(string repMark, int carNo, string kind, string isLoaded, string dest) { bool loaded; if(isLoaded == "true") loaded = true; else loaded = false; Car c(repMark, carNo, kind, loaded, dest); cout << "buildCar" << endl; c.output(); return c; } FreightCar buildFreightCar(string repMark, int carNo, string kind, string isLoaded, string dest) { bool loaded; if(isLoaded == "true") loaded = true; else loaded = false; FreightCar c(repMark, carNo, kind, loaded, dest); cout << "buildFreightCar" << endl; c.output(); return c; } PassengerCar buildPassengerCar(string repMark, int carNo, string kind, string isLoaded, string dest) { bool loaded; if(isLoaded == "true") loaded = true; else loaded = false; PassengerCar c(repMark, carNo, kind, loaded, dest); cout << "buildPassengerCar" << endl; c.output(); return c; } void input(StringOfCars &string1) { ifstream inputFile; inputFile.open("/Users/raji/Documents/Chegg/c++/Test/Test/cardata51.txt"); if(inputFile.is_open()== false) { cerr<<"Error!! Can't read file... "; exit(0); } string reportingMark, kind, destination, type; int carNumber; string seqNo; string isLoaded = " "; string line; while(inputFile.peek() != EOF) { inputFile >> type >> seqNo >> reportingMark >> carNumber >> kind >> isLoaded ; getline(inputFile, destination); cout << seqNo << endl; if(type == "Car") { Car c = buildCar(reportingMark, carNumber, kind, isLoaded, destination); } else if(type == "FreightCar") { FreightCar c = buildFreightCar(reportingMark, carNumber, kind, isLoaded, destination); } else if(type == "PassengerCar") { PassengerCar c = buildPassengerCar(reportingMark, carNumber, kind, isLoaded, destination); } } inputFile.close(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!