用C语言实现面向对象
来源:互联网 发布:黑马程序员java教程 编辑:程序博客网 时间:2024/05/25 21:35
注:这个程序是在软件设计师教程上看到,并做了一点点修改得到。。
在一个简化的绘图程序中,支持点(point)和圆(circle)两种图形,在设计过程中采用面向对象思想,认为所有的点和圆都是一种图形(shape),
并定义类型shape_t、point_t、circle_t分别表示基本图形、点和圆,点和圆自然具有基本图形的所有特征。
下面是利用C语言,通过函数指针和可变参数机制实现上面的类层次关系:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
// 程序中的两个图形:点和圆
typedef enum {point,circle}shape_type;
// 基本图形类型
typedef struct {
shape_type type;
void (*destroy)();
void (*draw)();
}shape_t;
// 点类
typedef struct {
shape_t common;
int x,y;
}point_t;
// 圆类
typedef struct {
shape_t common;
point_t *center;
int radius;
}circle_t;
/*
下面是相关方法*/
void destroyPoint(point_t *this)
{
int x=this->x,y=this->y;
free(this);
printf("P(%d,%d) destroyed!\n",x,y);
}
void drawPoint(point_t *this)
{
printf("P(%d,%d)",this->x,this->y);
}
point_t * createPoint(va_list *ap)
{
point_t *p_point;
if ((p_point=(point_t*)malloc(sizeof (point_t)))==NULL)
return NULL;
p_point->common.type=point;
p_point->common.destroy=destroyPoint;
p_point->common.draw=drawPoint;
p_point->x=va_arg(*ap,int);
p_point->y=va_arg(*ap,int);
return p_point;
}
void destroyCircle(circle_t *this)
{
int x=this->center->x;
int y=this->center->y;
int r=this->radius;
this->center->common.destroy(this->center);
free(this);
printf("C(P(%d,%d),%d) destroyed!\n",x,y,r);
}
void drawCircle(circle_t *this)
{
printf("C(");
this->center->common.draw(this->center);
printf(",%d)",this->radius);
}
circle_t* createCircle(va_list* ap)
{
circle_t *p_circle;
if ((p_circle=(circle_t*)malloc(sizeof(circle_t)))==NULL)
return NULL;
p_circle->common.type=circle;
p_circle->common.destroy=destroyCircle;
p_circle->common.draw=drawCircle;
p_circle->center=createPoint(ap);
p_circle->radius=va_arg(*ap,int);
return p_circle;
}
shape_t* createShape(shape_type st,...)
{
va_list ap;
shape_t* p_shape=NULL;
va_start(ap,st);
if (st==point) p_shape=(shape_t*)createPoint(&ap);
if (st==circle) p_shape=(shape_t*)createCircle(&ap);
va_end(ap);
return p_shape;
}
int main()
{
int i;
shape_t *shapes[2];
shapes[0]=createShape(point,2,3);
shapes[1]=createShape(circle,20,40,10);
for (i=0;i<2;i++) {
shapes[i]->draw(shapes[i]);
printf("\n");
}
for (i=1;i>=0;i--)
shapes[i]->destroy(shapes[i]);
return 0;
}
- 用C语言实现面向对象编程
- 用C语言实现面向对象
- 用 C 语言实现面向对象编程
- 用C语言实现面向对象
- 用 C 语言实现面向对象编程
- c语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- 用c语言实现面向对象的编程方式
- 用c语言实现面向对象的编程方式
- 用C语言实现面向对象的开发
- 用C语言实现面向对象程序设计(一)
- 用C语言实现面向对象程序设计(二)
- Android getWidth和getMeasuredWidth的正解
- mysql 优化技巧心得一(key_buffer_size设置).
- RMAN备份类型
- .net framework 4.5 独立安装包下载
- jquery中复选框全部选定或者全部不选定
- 用C语言实现面向对象
- 打开设备管理器
- 集群下tomcat间session复制
- uva 10104 (扩展欧几里德算法)
- 经验分享:CSS浮动(float,clear)通俗讲解
- Activity在切换屏幕时的方法调用【转】
- Windows驱动开发:用C还是C++
- for循环时jquery闭包问题
- ibatis与spring整合实例(附源码)