0
我想重载继承类Espresso中的方法getDescription(),但是当我在main()中调用它时,会列出Unknown Beverage并且Espresso类中的默认构造函数似乎没有被调用。为什么我的继承Espresso类的默认构造函数中的重写方法(getDescription)不被识别?
我希望它使用这个扩展类从main()打印Espresso Coffee。
class Beverage
{
string description;
public:
Beverage() : description("Unknown Beverage"){};
string getDescription() { return description; };
};
class Espresso : public Beverage
{
public:
Espresso() { getDescription() = "Espresso Coffee"; };
~Espresso();
};
int main()
{
Beverage *beverage = new Espresso();
cout << beverage->getDescription();
};