C ++基类加载函数 [英] C++ Base Class load function
本文介绍了C ++基类加载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个基类和一个加载函数,如果我有一个派生类,我可以使用基类的加载函数,并添加额外的数据成员,如运动车的引擎大小从文件加载。下面是我的函数定义。
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;
}
推荐答案
p>
yes you can :
class Car{
public:
virtual void Load(ifstream& carFile);
}
重要的是Load方法是虚拟的。
it is important that the Load method is virtual.
class SportsCar : public Car{
public:
virtual void Load(ifstream& carFile);
private:
int engineSize;
}
这里的方法是覆盖,所以你使用继承
Here the method is overriden, so you are using inheritance
SportsCar::Load(ifstream& carFile){
Car::Load(carFile);
carFile >> engineSize;
}
及以上是实现。
这篇关于C ++基类加载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文