c与面象对象之继承
来源:互联网 发布:php包含html文件 编辑:程序博客网 时间:2024/05/16 08:24
c语言是面过程的语言,不过可以通过结构体编写出具有类似对象的结构化程序。本以为c语言只能进行类似对象的封装,今天在<<Linux C编程实战>>中还看到可以用结构体模拟继承的方法。代码如下:
#include <stdio.h>----stdio.h
#include <stdlib.h>----stdlib.h
#define FATHER(child) (struct Father *)(child)
void print1(int i)
{
printf("this is father and i = %d/n",i);
}
void print2(int i)
{
printf("this is child and i = %d/n",i);
}
struct Father
{
int a;
void (*pointer1_to_function)(int);
};
struct Child
{
struct Father f;
int b;
void (*pointer2_to_function)(int);
};
void father_member_funtion(struct Father *f, char *string)
{
printf("/n");
f->pointer1_to_function(f->a);
printf("%s/n/n",string);
}
int main()
{
struct Child *p_child;
struct Father *p_father;
p_child = (struct Child *)malloc(sizeof(struct Child));
p_child->f.a = 10;
p_child->f.pointer1_to_function = print1;
p_child->b = 20;
p_child->pointer2_to_function = print2;
p_child->pointer2_to_function(p_child->b);
p_father = FATHER(p_child);
p_father->pointer1_to_function(p_father->a);
father_member_funtion(p_father,"hello");
return 0;
}
- c与面象对象之继承
- C++primer学习:面象对象程序设计(5):容器与继承
- C++primer学习:面象对象程序设计(4):继承类中的作用域和拷贝控制
- 面象对象与实际的结合
- 面象对象
- java 面象对象
- C#面象对象学习:接口与抽象类
- C#面象对象学习:类(class)与结构(struct)
- C#面象对象1
- C#面象对象2
- PHP中的面象对象
- JavaScript面象对象设计
- 面象对象设计总结
- 面象对象设计原则
- 黑马程序员--面象对象
- 面象对象 自学一
- 面象对象 自学二
- 黑马程序员关于面象对象以及它的三大特性:封装 继承 多态
- 公司的加班感言 ZT
- springside 学习日志
- 精简test1
- 高手帮忙CToolApp是什么类? 先谢谢了
- XP下自己动手重装IE6.0的3种方法(建议第三种,原创)
- c与面象对象之继承
- about some code of html editor
- Visual Studio 2005中编译调试新功能
- Postgres 8.4 数据类型增进
- import语句
- Sql2000中对标识列的操作方法
- 在Qt中实现一个对话框
- 今天进驻Csdn
- [ZT] ASP.NET 关于大文件上传问题