关于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. JQ AJAX

    用AJAX方法不刷新网页使用下拉列表连接数据库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  2. (1)Underscore.js入门

    1. Underscore对象封装 Underscore并没有在原生的JavaScript对象原型中进行扩展,而是像jQuery一样,将数据封装在一个自定义对象中(下文中称"Undersco ...

  3. 【转载】pyqt QTableWidget的使用

    转载地址: http://blog.sina.com.cn/s/blog_9b78c91101019sgi.html QTableWidget是QT程序中常用的显示数据表格的空间,很类似于VC.C#中 ...

  4. PostgreSQL简单介绍

    自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...

  5. 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏

    网站优化时,往往需要对js文件,css文件进行压缩,以达到减少网络传输数据,减少网页加载时间:利用YaHoo的YUI可以实现Javascript,CSS,压缩,包括在线的js压缩和程序压缩,发现C#也 ...

  6. jquery获取和设置元素高度宽度

    jquery获取和设置元素高度宽度 1.height()/ width() 取得第一个匹配元素当前计算的高度/宽度值(px) height(val)/ width(val) 为每个匹配的元素设置CSS ...

  7. 单链表带头结点&不带头结点

    转自:http://blog.csdn.net/xlf13872135090/article/details/8857632 Node *head;  //声明头结点   带头结点初始化 void I ...

  8. 内容分发网络CDN(互联网技术)

    内容分发网络(互联网技术)CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更 ...

  9. 在Salesforce中处理Xml的生成与解析

    在Salesforce中处理Xml的生成与解析 1): Generate Xml private String ConvertAccountToXmlInfo(Account acc){ Dom.Do ...

  10. CF735D Taxes 哥德巴赫猜想\判定素数 \进一步猜想

    http://codeforces.com/problemset/problem/735/D 题意是..一个数n的贡献是它的最大的因子,这个因子不能等于它本身 然后呢..现在我们可以将n拆成任意个数的 ...