[NetBehaviour] yesterdays object lesson

James Morris jwm.art.net at gmail.com
Wed Oct 9 22:45:19 CEST 2013


// c++

#include <iostream>

class base {
public:
    enum TYPE { I = 26, F = 369 };

    base(TYPE t) : type(t) {};
    virtual void show() = 0;

    static base* create(int);
    static base* create(float);

    void is_what() {
        std::cout << "is ";
        switch(type) {
        case I: std::cout << "I"; break;
        case F: std::cout << "F"; break;
        default: std::cout << "?"; break;
        }
        std::cout << std::endl;
    }
  private:
    TYPE type;
    base() {};
};

class i : public base {
  public:
    void show() { std::cout << "int value: "
                        << n << std::endl; }
  private:
    friend class base;
    i(int val) : base(I), n(val) {};
    int n;
};

class f : public base {
  public:
    void show() { std::cout << "float val: "
                       << n << std::endl; }
  private:
    friend class base;
    f(float val) : base(F), n(val) {};
    float n;
};

base* base::create(int n) {
    return new i(n);
}

base* base::create(float n) {
    return new f(n);
}

int main() {
    base* b1 = base::create(67);
    base* b2 = base::create(0.675f);

    b1->show();
    b1->is_what();
    b2->show();
    b2->is_what();

    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.netbehaviour.org/pipermail/netbehaviour/attachments/20131009/1c2745d1/attachment.htm>


More information about the NetBehaviour mailing list