• 函数指针与多态


    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int add(int a, int b)
    {
        return a + b;
    }
    
    int sub(int a, int b)
    {
        return a - b;
    }
    
    int cheng(int a, int b)
    {
        return a*b;
    }
    
    
    int cal(int a, int b, int(*pFunc)(int,int))
    {
        return pFunc(a, b);
    }
    
    // 
    
    class AbstractCal
    {
    public:
        void setX(int x)
        {
            this->x = x;
        }
        void setY(int y)
        {
            this->y = y;
        }
        virtual int GetResult() = 0;
    protected:
        int x;
        int y;
    };
    
    class AddCal :public AbstractCal
    {
    public:
        AddCal(int a, int b)
        {
            x = a;
            y = b;
        }
        int GetResult() { return x + y; }
    };
    
    
    
    class SubCal :public AbstractCal
    {
    public:
        SubCal(int a, int b)
        {
            x = a;
            y = b;
        }
        int GetResult() { return x - y; }
    };
    
    
    class CheCal :public AbstractCal
    {
    public:
        CheCal(int a, int b)
        {
            x = a;
            y = b;
        }
        int GetResult() { return x * y; }
    };
    
    
    int Cal(AbstractCal* obj)
    {
        return obj->GetResult();
    }
    
    int main()
    {
        cout << cal(10, 20, add) << endl;
        cout << cal(10, 20, sub) << endl;
        cout << cal(10, 20, cheng) << endl;
        cout << "-------------------------------" << endl;
    
        AddCal a(10,20);
        cout << Cal(&a) << endl;
    
        SubCal b(10, 20);
        cout << Cal(&b) << endl;
        CheCal c(10, 20);
        cout << Cal(&c) << endl;
    
        return 0;
    }
  • 相关阅读:
    NOP源码分析六--实体、数据的分层与处理。
    NOP源码分析七---继续
    NOP源码分析 八---set的存储
    Nop 源码分析四 任务系统
    NOP源码分析五,文件位置等详细内容,感冒真难受,嗓子痒又疼。。
    1
    mobx
    ts随笔
    13.vue-vuex
    13.vue-axios
  • 原文地址:https://www.cnblogs.com/mmc9527/p/10437800.html
Copyright © 2020-2023  润新知