Question: C++ Copy the solution from problem 4.2 Problem 5.1 In this problem we will use inheritance to create two new classes, both of which will
C++
Copy the solution from problem 4.2
Problem 5.1
In this problem we will use inheritance to create two new classes, both of which will inherit from the class Car
Do not change the StringOfCar class . You are not using a StringOfCars in this problem, but will need it later. You can remove the StringOfCars from the parameter list for the input function and put it back later, or keep the StringOfCars parameter, pass a StringOfCars to the input funcion, and ignore it.
As a preliminary step, create a new global function (that is a function that is not a member function) named buildCar. The buildCar function has the five parameters needed for a Car. When called, it builds an object of type Car by using the Car constructor that has five parameters. For testing in this assignment, after building the car object, the buildCar function calls the output member function for the Car. Now, modify the input function so it calls the buildCar function to build the car, rather than building the car in the input function.
The kind of cars for the three classes will be: Car: business, maintenance, tender, other FreightCar: box, tank, flat, hopper, otherFreight PassengerCar: chair, sleeper, otherPassenger, combine
Use an enum to keep the kind, rather than using a string as we did in previous problems. In the global area define an enum named Kind, with the following values in this order: business, maintenance, tender, other box, tank, flat, hopper, otherFreight, chair, sleeper, otherPassenger, combine. Also in the global area define an array of const string objects named KIND_ARRAY. It will contain strings with the same text as the names of the values in the enum, in the same order.
Change the Kind field in the Car class so it is: Kind kind; Adjust the functions to use the enum and KIND_ARRAY. The output function can use the KIND_ARRAY values to print the string corresponding to the enum value.
Build a new member function setKind. in the Car::setUpCar function, pass it a constant reference to the string provided by the user. If the string is not business, maintenance or tender in the Car class, set the kind to other. The setKind member function will set the correct Kind.
Change private to protected in the Car class only.
Make two classes that inherit from the Car class: FreightCar and PassengerCar. Each class will need a default constructor, a copy constructor, and a constructor that has five parameters. Only one more function will be built in each class; all the rest will be inherited. No additional member data will be added.
Create setKind functions for the FreightCar and PassengerCar classes that are similar to the setKind function for the Car class, but with different values. The setKind function for the FreightCar class uses only the values: box, tank, flat ,hopper, otherFreight The setKind function for the PassengerCar class uses only the values: chair, sleeper, otherPassenger, combine
Create two new global function named buildFreightCar and buildPassengerCar, similar to the buildCar function. These are used to build a FreightCar or a Passenger car, respectively.
Revise the main function to call the input function and do nothing else.
In the input function loop read one line from the file each time through the loop, look at the Type field in the record and call the corresponding build function to build that type of car. Before calling the appropriate build function, print a header giving the sequence number of the car read, with the number 1 for the first car and incremented for each successive car.
The file for this problem should contain (without headers, of course):
Type order ARR number kind loaded destination
Car car1 SP 819480 maintenance false NONE
Car car2 SLSF 46870 business true Memphis
Car car3 AOK 150 tender true McAlester
FreightCar car4 MKT 123450 tank false Fort Worth
FreightCar car5 MP 98760 box true Saint Louis
FreightCar car6 SP 567890 flat true Chicago
FreightCar car7 GMO 7870 hopper true Mobile
PassengerCar car8 KCS 7890 chair true Kansas City
PassengerCar car9 PAPX 140 sleeper true Tucson
PassengerCar car10 B&O 740 combine false NONE
This file is provided as cardata51.txt
THIS IS 4.2
#include
class Car { private: static const int Num_of_Width = 22; string reportingMark; int carNumber; string kind; bool loaded; string destination;
public: Car() { setUp(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination ="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); }
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();
};
void input(StringOfCars &str);
int main() { cout << " TEST 1 " << endl; Car 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 << 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::setUp(string reportingMark, int carNumber, string kind, bool loaded, string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; this->kind = 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 input(StringOfCars &string1) { ifstream inputFile;
inputFile.open("cardata.txt");
if(inputFile.is_open()== false) { cerr<<"Error!! Can't read file... "; exit(0); }
string reportingMark, kind, destination, type; bool loaded; int carNumber; string isLoaded = " "; string line;
while(inputFile.peek() != EOF) { getline(inputFile, line); inputFile >> type >> reportingMark >> carNumber >> kind >> isLoaded >> destination; if(isLoaded == "true") loaded = true; else loaded = false; cout < Car temp(reportingMark, carNumber, kind, loaded, destination); string1.push(temp); } inputFile.close(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
