I’m trying to overload the method getDescription() in the inherited class Espresso, but when I call it in main(), Unknown Beverage is listed and the default constructor in the Espresso class doesn’t seem to be getting called.
I want it to print out Espresso Coffee from main() using this extended class.
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();
};
doesn’t change the
descriptionmember. It returns a new string which you change.You need a setter method in the base class: