Question on basic inheritance, polymorphism, file input

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)?
You can call the parent’s constructor in the child class’s constructor if that’s any help..
IT IS!!!
Ok, so I've played around with this and I'm still stuck. How do you go about calling the base class' constructor in the subclass' constructor?


1
2
Child(some_arguments) : Parent(arguments_that_parent_takes) {
}
1
2
3
4
5
6
7
8
9

Truck::Truck(ifstream &infile) : Vehicle(infile)
{
    Vehicle(infile);
    infile >> axles;
    infile >> weight;
    infile >> paymentType;
}


Lot of errors with this. Apologies to keep bugging you with this. If there is a reference on this you can recommend I'd be happy to read it
Hum. Maybe Vehicle::Vehicle(infile); ? What are the errors? I’m sorry I don’t have any references for you to look at :/
I am not looking for an answer in code

The problem is a textual description of a code requires tons of words.

1) Delegating constructors are explained here:
https://en.cppreference.com/w/cpp/language/initializer_list
1
2
3
4
5
6
7
8
Truck::Truck(ifstream &infile)
    :   Vehicle(infile)     <-- correct
{
    Vehicle(infile);        <-- wrong
    infile >> axles;
    infile >> weight;
    infile >> paymentType;
}


You might need to add using Vehicle::Vehicle;, according to your code.
More info here:
https://en.cppreference.com/w/cpp/language/using_declaration
(chapter Inheriting constructors)

using the constructor that takes in the file pointer

Well, ok, you’re using a reference… Anyway, you’d better ask your teacher: some of them are very demanding about verbatim following assignment instructions.
The code threw in was correct. I at least have a test case working now, thank you.
Topic archived. No new replies allowed.