【发布时间】:2017-12-12 04:19:44
【问题描述】:
我正在尝试编写一个将不同类型的蛋糕插入发票的程序,它使用了几个派生类。我想使用派生类的构造函数来初始化抽象父类中的一些数据成员。有没有办法可以做到这一点,以便我可以保持数据成员私有,并在派生类中调用基类构造函数来初始化它们?例如:
class Cake:
public:
Cake(string flavor, string frosting) {
cakeType = flavor;
frostingType = frosting;
}
private:
string cakeType;
string frostingType;
};
class LayerCake: public Cake {
public:
LayerCake(string flavor, string frosting, int layers, int
quantity) {
numLayers = layers;
cakeQuantity = quantity;
Cake(flavor, frosting);
private:
int numLayers;
int cakeQuantity;
};
【问题讨论】:
标签: c++ inheritance constructor