#include // for cout #include // for clamp using namespace std; /* * The typedef keyword declares an alias for a type. * * Use it like this: * typedef double cash; // Declare cash as an alias for double. * * It can also be used inside a class, to provide a type (if public) * or to provide a convenient alias (if private). * * Classes can also create a private typedef for std::string or std::ostream, * to avoid littering the class declaration with std::. */ // Store an even number class Even { public: Even() : value(0) { } void set_value(int n) { value = n & ~01; } // make it even int get_value() const { return value; } private: int value; }; // Store an even percentage class EvenPercent : public Even { // This example is trivial, but when we get to templates, the name // of the base class “Even” could be a LOT more complicated. typedef Even super; // Make convenience alias for base class public: void set_value(int v) { super::set_value(clamp(v, 0, 100)); // Call base class mutator } }; int main() { EvenPercent ep; ep.set_value(7); // Should become 6 cout << "Expect 6: " << ep.get_value() << '\n'; ep.set_value(150); // Should become 100 cout << "Expect 100: " << ep.get_value() << '\n'; return 0; }