关于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 ;
}

关于c语言模拟c++的多态的更多相关文章

  1. c语言模拟c++的继承和多态

    //C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...

  2. 语言模拟ATM自动取款机系统

    C语言实验报告       题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...

  3. c语言中继承和多态的简单实现

    C语言本身是不支持继承和多态的,但其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 GObject,它是整个图形界面开发库 GTK 的基石,在IBM developerWor ...

  4. C语言模拟实现多态

    一.多态的主要特点 1.继承体系下.继承:是面向对象最显著的一个特性.继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性 和行为,并能扩展新的能力,已有类被称为父类/基类,新增加的类被称作子 ...

  5. C语言::模拟实现strlen函数

    题目要求 编写一个C语言程序模拟实现strlen函数. 算法 strlen函数功能是计算字符串中字符的个数.(除\0外) 而字符串本身就是一个字符数组,只不过末尾以\0结束. 因此,我们只需遍历除\0 ...

  6. C语言设计模式-封装-继承-多态

    快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...

  7. c++语言虚函数实现多态的原理(更新版)

    自上一个帖子之间跳过了一篇总结性的帖子,之后再发,今天主要研究了c++语言当中虚函数对多态的实现,感叹于c++设计者的精妙绝伦 c++中虚函数表的作用主要是实现了多态的机制.首先先解释一下多态的概念, ...

  8. C语言:模拟密码输入显示星号

    一个安全的程序在用户输入密码时不应该显示密码本身,而应该回显星号或者点号,例如······或******,这在网页.PC软件.ATM机.POS机上经常看到.但是C语言没有提供类似的功能,控制台上只能原 ...

  9. c语言模拟实现oc引用计数

    #include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题:  1,指向某块动态内存的指针有几个? //    ...

随机推荐

  1. yum -y list java* 查看当前java的版本

    [root@NB ok]# yum -y list java* Loaded plugins: fastestmirror, refresh-packagekit, security Loading ...

  2. jQuery – 6.选择器

    1. 属性过滤选择器: 1. $("div[id]")选取有id属性的<div> 2. $("div[title=test]")选取title属性为 ...

  3. NPOI读写Excel

    1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 ...

  4. Win10 兼容性 Visual studio web应用程序 ASP.NET 4.0 尚未在 Web 服务器上注册

    系统升级到windows10 ,Visual studio 创建web应用程序时出现如下提示ASP.NET 4.0尚未在 Web 服务器上注册.为了使网站正确运行,可能需要手动将 Web 服务器配置为 ...

  5. 攻城狮在路上(肆)How tomcat works(二) 一个简单的servlet容器

    该节在上一节的基础上增加了所谓对静态资源和动态资源访问的不同控制流程.示例里面采用的是对路径“/servlet/”进行了特殊处理. 一. 主要还是从HttpServer1中的main方法开始,先解析出 ...

  6. ThinkPHP3.2判断手机端访问并设置默认访问模块的方法

    ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法 目录结构 公共模块Common,Home模块,Mobile模块 配置Application/Common/Conf/config.p ...

  7. [Spring] 事务级别定义

    记录下来,以后备用 //事务传播属性 @Transactional(propagation=Propagation.REQUIRED)//如果有事务,那么加入事务,没有的话新创建一个 @Transac ...

  8. [Tools] Eclipse XML 注释和撤销注释

    eclipse中编辑java或C/C++文件时,注释的快捷键均为 "CTRL + / ",编辑xml文件时,该快捷键无效. eclipse XML 注释:CTRL + SHIFT ...

  9. view和activity的区别

    activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示.  viewGroup继承自view,实现了ViewManager,ViewParent接 ...

  10. Redis学习笔记(2) Redis基础类型及命令之一

    1. 基础命令 (1) 获取符合规则的键名列表 格式为:KEYS pattern 其中pattern表示支持通配符 # 建立一个名为bar的键 > SET bar OK # 获取Redis所有键 ...