I have a base class of cars and a load function, if I have a derived class, can I use the load function from the base class and add extra data members such as engine size for sports cars to be loaded from a file. Below is my function definition.
void Car::Load(ifstream& carFile)
{
carFile >> CarName >> Age >> Colour >> Price;
}
yes you can :
it is important that the Load method is virtual.
Here the method is overriden, so you are using inheritance
and above is the implementation.
If the function is declared
virtual
in classCar
you can override and extend it in the derived classNote I have used
std::istream
in my code sample, since it's not relevant if the stream is coming from a file for these parts of code.You probably need a discriminator for the car type in the file (
std::istream
respectively), to decide which class actually should be instantiated: