• 关于c语言模拟c++的多态


    关于c++多态,个人认为就是父类调用子类的方法,c++多态的实现主要通过虚函数实现,如果类中含有虚函数,就会出现虚函数表,具体c++多态可以参考《深度探索c++对象模型》

    c语言模拟多态主要通过函数指针实现,可以参考《Object Orientated Programming in ANSI-C》

    //shape.h
    
    #ifndef __SHAPE_H
    #define __SHAPE_H
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _functions vFunctions;
    
    typedef struct _shape Shape;
    
    typedef void (*fptrSet)(void* , int);
    typedef int (*fptrGet)(void*);
    typedef void (*fptrDisplay)();
    
    struct _functions{
        fptrSet setX;
        fptrGet getX;
        fptrSet setY;
        fptrGet getY;
        fptrDisplay display;
    };
    
    struct _shape{
         vFunctions functions;
         int x;
         int y;
    };
    
    Shape* getShapesInstances();
    
    void shapeDisplay(Shape *shape);
    
    void shapeSetX(Shape *shape, int x);
    
    void shapeSetY(Shape *shape, int y);
    
    int shapeGetX(Shape *shape);
    
    int shapeGetY(Shape *shape);
    #endif
    //Shape.c
    
    #include "Shape.h"
    
    void shapeDisplay(Shape *shape){
        printf("Shape
    ");
    }
    
    void shapeSetX(Shape *shape, int x){
        shape->x = x;
    }
    
    void shapeSetY(Shape *shape, int y){
        shape->y = y;
    }
    
    int shapeGetX(Shape *shape){
        return shape->x;
    }
    
    int shapeGetY(Shape *shape){
        return shape->y;
    }
    
    
    Shape* getShapesInstances(){
        Shape *shape = (Shape*)malloc(sizeof(Shape));
        shape->functions.display = shapeDisplay;
        shape->functions.setX = shapeSetX;
        shape->functions.getX = shapeGetX;
        shape->functions.setY = shapeSetY;
        shape->functions.getY = shapeGetY;
        shape->x = 100;
        shape->y = 100;
        return shape;
    }
    //Rectangle.h
    
    #ifndef __RECTANGLE__H
    #define __RECTANGLE__H
    
    #include "Shape.h"
    
    typedef struct _rectangle{
        Shape base;
        int width;
        int height;
    }Rectangle;
    
    void rectangleSetX(Rectangle *rectangle, int x);
    
    void rectangleSetY(Rectangle *rectangle, int y);
    
    int rectangleGetX(Rectangle *rectangle);
    
    int rectangleGetY(Rectangle *rectangle);
    
    void rectangleDisplay();
    
    Rectangle* getRectangleInstance();
    
    #endif
    //Rectange.c
    
    #include "Rectange.h"
    
    void rectangleSetX(Rectangle *rectangle, int x){
        rectangle->base.x = x;
    }
    
    void rectangleSetY(Rectangle *rectangle, int y){
        rectangle->base.y = y;
    }
    
    int rectangleGetX(Rectangle *rectangle){
        return rectangle->base.x;
    }
    
    int  rectangleGetY(Rectangle *rectangle){
        return rectangle->base.y;
    }
    
    void rectangleDisplay(){
        printf("Rectange
    ");
    }
    
    Rectangle* getRectangleInstance(){
        Rectangle *rectangle = (Rectangle *)malloc(sizeof(Rectangle));
        rectangle->base.functions.display = rectangleDisplay;
        rectangle->base.functions.setX = rectangleSetX;
        rectangle->base.functions.getX = rectangleGetX;
        rectangle->base.functions.setY = rectangleSetY;
        rectangle->base.functions.getY = rectangleGetY;
        rectangle->base.x = 200;
        rectangle->base.y = 200;
        rectangle->width  = 300;
        rectangle->height = 500;
        return rectangle;
    }
    //main.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "Shape.h"
    #include "Rectange.h"
    int main()
    {
        Shape *sptr = (Shape *)getShapesInstances();
        sptr->functions.setX(sptr,35);
        sptr->functions.display();
        sptr->functions.setY(sptr,60);
        printf("%d
    ",sptr->functions.getX(sptr));
    
        Shape *shapes[3];
        shapes[0] = getShapesInstances();
        shapes[0]->functions.setX(shapes[0],35);
        shapes[1] = getRectangleInstance();
        shapes[1]->functions.setX(shapes[1],45);
        shapes[2] = getShapesInstances();
        shapes[2] ->functions.setX(shapes[2],55);
        int  i =0 ;
        for( i = 0 ; i < 3; ++ i){
            shapes[i]->functions.display();
            printf("%d
    ",shapes[i]->functions.getX(shapes[i]));
        }
        return 0;
    }
  • 相关阅读:
    poj 3254 Corn Fields 状压dp
    poj 1330 Nearest Common Ancestors LCA/DFS
    poj1182 食物链 带权并查集 偏移量
    ural 1019. Line Painting 线段树 离散化
    zoj 2301 || hdu 1199 Color the Ball 线段树 离散化
    poj 1195 Mobile phones 二维树状数组
    poj 2155 Matrix 二维树状数组
    poj3067 Japan 树状数组 逆序数
    OpenWRT Init (User space boot) reference for Chaos Calmer: procd
    怎样写一个基于procd的init脚本
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3721795.html
Copyright © 2020-2023  润新知