关于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. DBCC常用命令小汇

    DBCC是SQL Server提供的一组控制台命令,功能很强大,掌握一些必要的语句,对操作数据库有不少帮助,所以决定整理一下,发现已有不少类似的整理,减少了不少工作,归类如下: 一.DBCC 帮助类命 ...

  2. Mishka and Interesting sum Codeforces Round #365 (树状数组)

    树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ...

  3. Unity3D使用过程中常见的20个问题

    1:天空盒有接缝怎么解决?答:在贴图导入设置里设置Wrap Mode为"Clamp". 2:DDS格式怎么不显示?答:Unity不支持DDS格式,Unity会将除DDS外的其他格式 ...

  4. unable to access android sdk add-on list

    在bin\properties里添加disable.android.first.run=true

  5. 消息提示插件toastr.js与Messenger组件

    Toastr是一款基于jQuery的通知插件,可以灵活的自定义样式和拓展其功能! toastr是一个基于Jquery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. cdn最 ...

  6. go语言

    Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.和今天的C++或C一样,Go是一种系统语言. 1.windows开发工具:Golang for Windows ...

  7. 【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现

    http://blog.csdn.net/shootyou/article/details/44852639 Emoji表情字符现在在APP已经广泛支持了.但是MySQL的UTF8编码对Emoji字符 ...

  8. android常用对话框封装

    在android开发中,经常会用到对话框跟用户进行交互,方便用户可操作性:接下来就对常用对话框进行简单封装,避免在项目中出现冗余代码,加重后期项目的维护量:代码如有问题欢迎大家拍砖指正一起进步. 先贴 ...

  9. .net自动生成数据库表的类

    // 获取到所有的用户表.DataTable userTableName = GetTable( "select name as tablename from sysobjects wher ...

  10. java出错

    某天cmd运行java突然莫名其妙的出错:(之前还好好的) Error occurred during initialization of VMjava/lang/NoClassDefFoundErr ...