关于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 ;
}
关于c语言模拟c++的多态的更多相关文章
- c语言模拟c++的继承和多态
//C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...
- 语言模拟ATM自动取款机系统
C语言实验报告 题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...
- c语言中继承和多态的简单实现
C语言本身是不支持继承和多态的,但其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 GObject,它是整个图形界面开发库 GTK 的基石,在IBM developerWor ...
- C语言模拟实现多态
一.多态的主要特点 1.继承体系下.继承:是面向对象最显著的一个特性.继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性 和行为,并能扩展新的能力,已有类被称为父类/基类,新增加的类被称作子 ...
- C语言::模拟实现strlen函数
题目要求 编写一个C语言程序模拟实现strlen函数. 算法 strlen函数功能是计算字符串中字符的个数.(除\0外) 而字符串本身就是一个字符数组,只不过末尾以\0结束. 因此,我们只需遍历除\0 ...
- C语言设计模式-封装-继承-多态
快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...
- c++语言虚函数实现多态的原理(更新版)
自上一个帖子之间跳过了一篇总结性的帖子,之后再发,今天主要研究了c++语言当中虚函数对多态的实现,感叹于c++设计者的精妙绝伦 c++中虚函数表的作用主要是实现了多态的机制.首先先解释一下多态的概念, ...
- C语言:模拟密码输入显示星号
一个安全的程序在用户输入密码时不应该显示密码本身,而应该回显星号或者点号,例如······或******,这在网页.PC软件.ATM机.POS机上经常看到.但是C语言没有提供类似的功能,控制台上只能原 ...
- c语言模拟实现oc引用计数
#include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题: 1,指向某块动态内存的指针有几个? // ...
随机推荐
- 大话数据结构–1.基础知识+2.算法
2.算法: 算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每个指令表现为一个或多个操作. 特性:输入.输出.有穷性.确定性.可行性. 2.9.1.算法时间复杂度: 语句 ...
- C# 读取CSV文件
CSV文件是用逗号作为分隔符的,所以如果是简单的CSV文件,用split(',')就可以了. 但是Excel 编辑CSV文件,且内容中有逗号,得到的csv文件如下:"aaa,aaa" ...
- MongoDB的介绍和使用场景(1)
MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSQL 数据库产品中最热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式,MongoDB 使用 C++开发.M ...
- Shell编程基础教程3--Shell输入与输出
3.Shell输入与输出 3.1.echo echo命令可以显示文本行或变量,或者把字符串输出到文件 echo [option] string ...
- 使用 Log4Net 记录日志
第一步:下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的 log4net-1.2.11-bin-n ...
- git branch用法总结
git branch git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如: #git branch* master newbranch ...
- 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏
网站优化时,往往需要对js文件,css文件进行压缩,以达到减少网络传输数据,减少网页加载时间:利用YaHoo的YUI可以实现Javascript,CSS,压缩,包括在线的js压缩和程序压缩,发现C#也 ...
- [SQL]oracle 的to_char、to_number、to_date用法
关键字: oracle 的to_char.to_number.to_date用法 TO_CHAR 是把日期或数字转换为字符串TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER ...
- WIN7系统下U盘安装Ubuntu双系统
1. 准备工作 1. 官网下载Ubantu镜像,我下载的是Ubantu14.04.3 LTS版本: 2. 下载 Universal USB Installer (UUI)用于制作U盘启动盘: 3. 下 ...
- MicroService/web Service/webAPI/RPC
[TOC] 微服务 服务拆分,利用轻量化机制(通常为HTTP源API)实现通信,复杂度可控,独立部署,技术选型灵活,容错,扩展. 康威定律的实际体现 微服务架构模式深刻影响了应用和数据库之间的关系,不 ...