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

时间:2023-03-09 14:41:01
关于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\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 = ;
shape->y = ;
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\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 = ;
rectangle->base.y = ;
rectangle->width = ;
rectangle->height = ;
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,);
sptr->functions.display();
sptr->functions.setY(sptr,);
printf("%d\n",sptr->functions.getX(sptr)); Shape *shapes[];
shapes[] = getShapesInstances();
shapes[]->functions.setX(shapes[],);
shapes[] = getRectangleInstance();
shapes[]->functions.setX(shapes[],);
shapes[] = getShapesInstances();
shapes[] ->functions.setX(shapes[],);
int i = ;
for( i = ; i < ; ++ i){
shapes[i]->functions.display();
printf("%d\n",shapes[i]->functions.getX(shapes[i]));
}
return ;
}