Sandwich shop interface using the Decorator pattern I have implemented a very basic sandwich (sub) shop which a user can add extra toppings at an additional cost. The cost is calculated and the contents of the sandwich is outputted.

I am learning design patterns and the book I am learning from (Head First Design Patterns) is in Java, which I am not too familiar with, so I may be making some errors.

Sub.h

#ifndef SUB_H
#define SUB_H

//Class Sub: Abstract Base class. 
//Following the decorator pattern

#include &lt;string&gt;
#include &lt;memory&gt;

class Sub
{
  public:
    virtual double getCost() = 0;
    virtual std::string getToppings() = 0;
    virtual ~Sub() = 0;
};

class Plain: public Sub
{
  public:
    double getCost() override;
    std::string getToppings() override;
};

class ExtraCheese: public Sub
{
  public:
    ExtraCheese(std::shared_ptr&lt;Sub&gt; s) : sub(s) {}
    double getCost() override;
    std::string getToppings() override;
  private:
    std::shared_ptr&lt;Sub&gt; sub;
};

class ExtraMeat: public Sub
{
  public:
    ExtraMeat(std::shared_ptr&lt;Sub&gt; s) : sub(s) {}
    double getCost() override;
    std::string getToppings() override;
  private:
    std::shared_ptr&lt;Sub&gt; sub;
};

class ExtraSauce: public Sub
{
  public:
    ExtraSauce(std::shared_ptr&lt;Sub&gt; s) : sub(s) {}
    double getCost() override;
    std::string getToppings() override;
  private:
    std::shared_ptr&lt;Sub&gt; sub;
};

#endif


Sub.cpp

#include "Sub.h"

inline Sub::~Sub(){}

//class Plain : public Sub
double Plain::getCost() 
{
  return 3.75;
}

std::string Plain::getToppings()
{
  return "Basic Sub";
}

//class ExtraCheese : public Sub

double ExtraCheese::getCost()
{
  return sub-&gt;getCost() + 0.75;
}

std::string ExtraCheese::getToppings()
{
  return sub-&gt;getToppings() + ", Extra Cheese";
}

//class ExtraMeat : public Sub
double ExtraMeat::getCost()
{
  return sub-&gt;getCost() + 1.50;
}

std::string ExtraMeat::getToppings()
{
  return sub-&gt;getToppings() + ", Extra Meat";
}

//class ExtraSauce : public Sub
double ExtraSauce::getCost()
{
  return sub-&gt;getCost() + 0.15;
}

std::string ExtraSauce::getToppings()
{
  return sub-&gt;getToppings() + ", Extra Sauce";
}


main.cpp

#include &lt;iostream&gt;
#include &lt;memory&gt;
#include "Sub.h"

int main()
{
  std::shared_ptr&lt;Sub&gt; s(new Plain());

  s = std::make_shared&lt;ExtraCheese&gt;(s); 
  s = std::make_shared&lt;ExtraMeat&gt;(s);
  s = std::make_shared&lt;ExtraSauce&gt;(s);

  std::cout&lt;&lt;"COST: "&lt;&lt; s-&gt;getCost() &lt;&lt;std::endl;
  std::cout&lt;&lt;"Toppings: " &lt;&lt; s-&gt;getToppings() &lt;&lt;std::endl;
}
No:

An object "Sub" and a decorator "Topping" implement the same interface. 
In addition a decorator takes an item (of the same interface) that it is decorating.
This is not quite what you have done.

What if I want extra cheese and meat?

You need something more like this:

class Price
{
    public:
       virtual     ~Price()   = 0;
       virtual int getPrice() = 0;
};

class Sub: public Price
{
    public:
        int getPrice() override {return 375;}
};

class ExtraCheese: public Price
{
    std::unique_ptr&lt;Price&gt;   item
    public:
        ExtraCheese(std::unique_ptr&lt;Price&gt;&amp;&amp; item)
             : item(std::move(item))
        {}
         int getPrice() override {return item-&gt;getPrice() + 25;}
};

int main()
{
    std::unique_ptr&lt;Price&gt;   sub(new Sub);
    std::unique_ptr&lt;Price&gt;   subCheese(new ExtraCheese(sub));
    std::unique_ptr&lt;Price&gt;   subCheeseMeat(new ExtraMeat(subCheese));

    std::cout &lt;&lt; subCheeseMeat-&gt;getPrice();
}