美文网首页
C++ 菜鸟笔记-虚函数多态

C++ 菜鸟笔记-虚函数多态

作者: chbeychevelle | 来源:发表于2019-01-02 17:56 被阅读0次
    #include <iostream>
    using namespace std;
    class baseNum {
    public:
        virtual void one() { cout << "one" << endl; two(); }
        void two() { cout << "two" << endl; three(); }
        void three() { cout << "three" << endl; four(); }
        void four() { cout << "four" << endl; five(); }
        void five() { cout << "five" << endl; }
    };
    class chinaNum : public baseNum
    {
    public:
        void one() { cout << "一" << endl; two(); }
        void two() { cout << "二" << endl; three(); }
        void three() { cout << "三" << endl; four(); }
        void four() { cout << "四" << endl; five(); }
        void five() { cout << "五" << endl; }
    };
    class aliNum :public baseNum
    {
    public:
        void one() { cout << "1" << endl; two(); }
        void two() { cout << "2" << endl; three(); }
        void three() { cout << "3" << endl; four(); }
        void four() { cout << "4" << endl; five(); }
        void five() { cout << "5" << endl; }
    };
    int main()
    {
        baseNum * num;
        int chonice;
        while (1)
        {
            bool falg = false;
            cout << "退出(0)英文(1)中文(2)阿拉伯数字(3):";
            cin >> chonice;
            switch (chonice)
            {
            case 0 :
                falg = true;
                break;
            case 1:
                num = new baseNum;
                num->one();
                break;
            case 2:
                num = new chinaNum;
                num->one();
                break;
            case 3:
                num = new aliNum;
                num->one();
                break;
            default:
                cout << "选择不正确,请重新输入!" << endl;
                break;
            }
    
            if (falg) {
                break;
            }
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ 菜鸟笔记-虚函数多态

          本文链接:https://www.haomeiwen.com/subject/rdqtrqtx.html