CS253: Software Development with C++

Spring 2021

Mixins

Show Lecture.Mixins as a slide show.

CS253 Mixins

🍨

Origin

The name “mixin” comes from adding various treats to ice cream, such as the McFlurry, Blizzard, or the ground-breaking research at Cold Stone Creamery.

Is-A

We know the is-a relationship, which comes from class inheritance:

class Dog {
  public:
    virtual const char * drink() { return "Lap lap lap."; }
    virtual const char * bark() { return "Woof!"; }
    virtual ~Dog() = default;
};

class Chihuahua : public Dog {
  public:
    virtual const char * bark() { return "¡Yip!"; }
};

Dog *koko = new Chihuahua;
cout << koko->drink() << '\n' << koko->bark();
delete koko;
Lap lap lap.
¡Yip!

Mixins

class PowerRing {
  public:
    auto zap() { return "green beam"; }
    auto fly() { return "up, up, and … ?"; }
};

class GreenLantern : public PowerRing { };

GreenLantern john_stewart;
cout << john_stewart.zap();
green beam

Usefulness