[The code below is runnable so feel free to run it to see what I am talking about]
I am following along with a tutorial from a book that is Java-based. The code below is essentially a bunch of classes that can be used to accurately get the description and price of a beverage ordered (no matter how many additional condiments are added on to the base Beverage). The issue is the getDescription() and cost() functions in the Beverage class are not being overridden by the CondimentDecorator class, or any of the various condiment classes (Mocha, Milk, Whip, etc.).
When I run it, "Unknown Beverage" prints out as the description.
How can I figure out where the inheritance bug is at?
#include <iostream>
#include <string>
using namespace std;
class Beverage {
string description = "Unknown Description";
public:
string getDescription(){
return description;
}
int cost(); // just declare it - don't do anything
};
class HouseBlend : public Beverage {
string description = "House Blend";
string getDescription(){
return description;
}
int count(){
return .89;
}
};
class DarkRoast : public Beverage {
string description = "Dark Roast";
string getDescription(){
return description;
}
int count(){
return .99;
}
};
class Decaf : public Beverage {
string description = "Decaf";
string getDescription(){
return description;
}
int count(){
return 1.05;
}
};
class Espresso : public Beverage {
string description = "Espresso";
string getDescription(){
return description;
}
int count(){
return 1.99;
}
};
class CondimentDecorator : public Beverage {
string getDescription();
};
class Soy : public CondimentDecorator {
Beverage *beverage;
public:
Soy(Beverage *beverage){
this->beverage = beverage;
}
string getDescription(){
return beverage->getDescription() + " , Soy ";
}
int cost(){
return beverage->cost() + .15;
}
};
class Milk : public CondimentDecorator{
Beverage *beverage;
public:
Milk(Beverage *beverage){
this->beverage = beverage;
}
string getDescription(){
return beverage->getDescription() + " , Milk ";
}
int cost(){
return beverage->cost() + .10;
}
};
class Mocha : public CondimentDecorator{
Beverage *beverage;
public:
Mocha(Beverage *beverage){
this->beverage = beverage;
}
string getDescription(){
return beverage->getDescription() + " , Mocha ";
}
int cost(){
return beverage->cost() + .20;
}
};
class Whip : public CondimentDecorator{
Beverage *beverage;
public:
Whip(Beverage *beverage){
this->beverage = beverage;
}
string getDescription(){
return beverage->getDescription() + " , Whip ";
}
int count(){
return beverage->cost() + .10;
}
};
//class StarbuzzCoffeeRun {
int main(){
Beverage *beverage = new Espresso;
cout << beverage->getDescription(); // << "$" << beverage->cost() << endl ;
Beverage *beverage2 = new DarkRoast;
beverage2 = new Mocha(beverage2);
cout << beverage2->getDescription();
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
cout << beverage2->getDescription(); // << "$" << beverage2->cost() << endl ;
Beverage *beverage3 = new HouseBlend;
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
cout << beverage3->getDescription(); // << "$" << beverage3->cost() << endl ;
return 0;
}