Greetings. This question is in regards to an assignment I'm working on; that being said, I am not looking for an answer in code, but rather someone to just set my head straight and give me a hint in the right direction.
We are given an input .txt file with roughly a dozen lines that look like the following:
truck t12643 2006 Ford F150 White 3 4500 card
car c111422 2014 Toyota Corolla White 2 cash
Each line will start with either "car" or "truck", which will be useful in main() to determine how the information is parsed. Notice, depending on whether the line is in regards to cars or trucks, determines what info the line holds. Both vehicles (Car and Truck are child classes of the parent class Vehicle) share information such as ID (t12643 for example), year, make, model, and color. The two differ in that trucks are further characterized by their number of axles (in the above case, 3) and their weight. Cars are further characterized by their number of doors (above, 2). Then both have a method of payment since the assignment relates to a tollbooth.
Still with me? Alright, I'm about to get to the question. For the main() file, MY INSTRUCTIONS (among other things like to open the input file and an output file, etc etc) ARE:
"in a loop, read from the input file the type (car or truck); based on the type read, dynamically allocate the memory
using the constructor that takes in the file pointer, and return the address to the vehicle pointer that corresponds to the
type (truck or car), and then pass that pointer to the arrive() function for the tollbooth object."
What I'm having trouble with is this: I must read information into the Car or Truck object which each have constructors like Truck::Truck(ifstream &infile), and the parent class, Vehicle, has a constructor also shown below. How do I use the vehicle constructor FIRST (since it reads information in the same order as is displayed in the text file), THEN use the Truck/Car constructor depending on which is needed?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Vehicle::Vehicle(ifstream &infile)
{
infile >> id;
infile >> year;
infile >> make;
infile >> model;
infile >> color;
}
Truck::Truck(ifstream &infile)
{
infile >> axles;
infile >> weight;
infile >> paymentType;
}
|
Those constructors were given to me with blank implementations (meaning, I know I have to use both that take ifstream &infile, but I completed it myself). So this could be totally wrong. But can someone at least point me in the direction of what concept I'm missing here? Something to do with polymorphism, perhaps (something I've only done one assignment on in the past)?