码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-05-11 16:25:23      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

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

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

bubuko.com,布布扣
//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
bubuko.com,布布扣
bubuko.com,布布扣
//Shape.c

#include "Shape.h"

void shapeDisplay(Shape *shape){
    printf("Shape\n");
}

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;
}
bubuko.com,布布扣
bubuko.com,布布扣
//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
bubuko.com,布布扣
bubuko.com,布布扣
//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\n");
}

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;
}
bubuko.com,布布扣
bubuko.com,布布扣
//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\n",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\n",shapes[i]->functions.getX(shapes[i]));
    }
    return 0;
}
bubuko.com,布布扣

 

关于c语言模拟c++的多态,布布扣,bubuko.com

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

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/xiongqiangcs/p/3721795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!