C语言实现OOP 版本3 :简化代码
我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试
shape.h
#ifndef SHAPE_H
#define SHAPE_H typedef struct shape_t
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Shape; void release(void *shape); #endif
shape.c
#include <stdlib.h>
#include "shape.h" void release(void *shape)
{
free(((Shape*)shape)->shapeData);
free(shape);
}
circle.h
#ifndef CIRCLE_H
#define CIRCLE_H #include "shape.h" typedef struct
{
double r;
}Circle; Shape* makeCircle(double r); #endif
circle.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "shape.h"
#include "circle.h" const double PI = 3.14159; static void area(void *shape)
{
Circle *_circle = (Circle*)((Shape *)shape)->shapeData;
printf("the circle area is %f \n", _circle->r * _circle->r * PI);
} Shape* makeCircle(double r)
{
Shape *shape = (Shape *)malloc(sizeof(Shape));
Circle *circle = (Circle *)malloc(sizeof(Circle));
assert(shape != NULL && circle != NULL);
assert(r > ); circle->r = r;
shape->shapeData = circle;
shape->area = &area;
shape->release = &release; return shape;
}
rectange.h
#ifndef RECTANGLE_H
#define RECTANGLE_H typedef struct{
float x;
float y;
}Rectangle; Shape *makeRectangle(float x, float y);
#endif
rectange.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "shape.h"
#include "rectangle.h" static void area(void *shape)
{
Rectangle *rectangle = (Rectangle*)((Shape *)shape)->shapeData;
printf("the rectangle area is %f \n", rectangle->x * rectangle->y);
} Shape* makeRectangle(float x, float y)
{
Shape *shape = (Shape *)malloc(sizeof(Shape));
Rectangle *rectangle = (Rectangle *)malloc(sizeof(Rectangle));
assert(shape != NULL && rectangle != NULL);
assert(x > && y > ); rectangle->x = x;
rectangle->y = y;
shape->shapeData = rectangle;
shape->area = &area;
shape->release = &release; return shape;
}
main.c
#include <stdio.h>
#include "shape.h"
#include "circle.h"
#include "rectangle.h" void printShapeArea(Shape **shapes,int length)
{
int i=;
for(i=;i<length;i++)
{
Shape *shape = shapes[i];
shape->area(shape);
shape->release(shape);
}
} int main()
{
Shape *p[] = {makeCircle(3.2),makeCircle(3.2),makeRectangle(,)};
printShapeArea(p,);
return ;
}
C语言实现OOP 版本3 :简化代码的更多相关文章
- C语言实现OOP 版本2
写版本2的原因,还是发现在不同的具体图形模块里发现了重复的release代码,这是坏味道,所以还是决定消除这些重复代码,DRY! shape.h #ifndef SHAPE_H #define SHA ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
- C# 语言规范_版本5.0 (第2章 词法结构)
1. 词法结构 1.1 程序 C# 程序 (program) 由一个或多个源文件 (source file) 组成,源文件的正式名称是编译单元 (compilation unit)(第 9.1 节). ...
- 几种不同程序语言的HMM版本
几种不同程序语言的HMM版本 “纸上得来终觉浅,绝知此事要躬行”,在继续翻译<HMM学习最佳范例>之前,这里先补充几个不同程序语言实现的HMM版本,主要参考了维基百科.读者有兴趣的话可以研 ...
- 链表中用标兵结点简化代码 分类: c/c++ 2014-09-29 23:10 475人阅读 评论(0) 收藏
标兵结点(头结点)是在链表中的第一个结点,不存放数据,仅仅是个标记 利用标兵结点可以简化代码.下面实现双向链表中的按值删除元素的函数,分别实现 带标兵结点和不带标兵结点两版本,对比可见标兵结点的好处. ...
- 用block做事件回调来简化代码,提高开发效率
我们在自定义view的时候,通常要考虑view的封装复用,所以如何把view的事件回调给Controller就是个需要好好考虑的问题, 一般来说,可选的方式主要有target-action和de ...
- LevelDB源码分析--使用Iterator简化代码设计
我们先来参考来至使用Iterator简化代码2-TwoLevelIterator的例子,略微修改希望能帮助更加容易立即,如果有不理解请各位看客阅读原文. 下面我们再来看一个例子,我们为一个书店写程序, ...
- C#泛型简化代码量示例
泛型简化代码量 下是我在项目中通过泛型来简化工作的一个Demo,记录一下: using System; using System.Collections.Generic; namespace My ...
- 玩转UITableView系列(一)--- 解耦封装、简化代码、适者生存!
UITableView这个iOS开发中永远绕不开的UIView,那么就不可避免的要在多个页面多种场景下反复摩擦UITableView,就算是刚跳进火坑不久的iOS Developer也知道实现UITa ...
随机推荐
- 【转】预装(push)lib64中so文件查找错误
原文网址:http://blog.csdn.net/caroline_wendy/article/details/43615361 Android系统已经升级为64位系统,在进行预装(adb push ...
- cf443A Anton and Letters
A. Anton and Letters time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CSS flex让所有灵活的项目都带有相同的长度,忽略它们的内容:
.flexbox { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; displa ...
- Enum Types
参考Java的官方tutorial和Doc整理如下. What is Enum An enum type is a special data type. It enables for a variab ...
- HDU 4444 Walk (离散化建图+BFS+记忆化搜索) 绝对经典
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给你一些n个矩形,给你一个起点,一个终点,要你求从起点到终点最少需要转多少个弯 题解:因为 ...
- table表格边框样式
; border-left:1px solid #aaa; border-top:1px solid #aaa; } td{border-right:1px solid #aaa; border-bo ...
- (转)iOS7界面设计规范(11) - UI基础 - 图标与图形
不知别人如何,我自己来讲,平时很习惯很有动力去做的一些事,譬如博客吧,一旦生活中出现一些让自己很难受的状况,就很容易受到影响:像是,你平时所习惯的生活状态都是基于某种东西的,一旦这种东西崩塌,会影响到 ...
- Windows下命令行直接编译程序
D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...
- Win32多线程编程(2) — 线程控制
Win32线程控制只有是围绕线程这一内核对象的创建.挂起.恢复.终结以及通信等操作,这些操作都依赖于Win32操作系统提供的一组API和具体编译器的C运行时库函数.本篇围绕这些操作接口介绍在Windo ...
- CentOS7--DNS处理模块DnsPython的简单使用
初步了解: DnsPython是Python实现的一个DNS工具包,支持几乎所有的记录类型. 安装: # wget http://www.dnspython.org/kits/1.9.4/dnspyt ...