利用c语言面向对象编程,C语言如何面向对象编程

闲来无事,写一下C语言如何面向对象编程吧:

面向对象编程

/*===========================================================

文件名: class.c

版本:1.0

时间:2017-11-12

作者:连志安

功能:C语言是一个面向过程编程的语言,本程序简单的实现面向对象

的思想,功能较为简陋。

1、如何定义一个类。

其实可以使用C语言的结构体加函数指针来实现。

例如:

struct fat {

int age;

void (*create)(struct fat * this); //创建函数

void (*destroy)(struct fat * this); //销毁函数

void (*write)(struct fat * this); //成员函数

};

typedef struct fat * class_fat; //这一句是必须的,把结构体指针

2、继承:

struct son {

CLASS_FATHER(fat); //继承此父类========= 这里就是继承了

void (*create)(struct son * this); //创建函数

void (*destroy)(struct son * this); //销毁函数

void (*write)(struct son * this); //成员函数

};

typedef struct son * class_son;

3、使用

struct son * son1 = new_class(son1, son); //创建一个实例

son1->write(son1); //调用写函数

del_class(son1); //销毁实例

4、父类函数复写:

class_overwrite(write, son_write);//重写父函数 write. 并用新的son_write函数

//class_def(write);//不重写父函数

剩下的看下面代码,不懂联系 连志安

===========================================================*/

#include

/*====================================================================

以下是相关宏

====================================================================*/

#define class_size(cls)sizeof(struct cls)

#define CLASS_FATHER(x) struct x * my_father

#define new_class(new, cls) (struct cls *)malloc(class_size(cls)); new->create = cls##_create; new->destroy = cls##_destroy; new->create(new)

#define del_class(new) new->destroy(new); free(new); new = NULL

#define father this->my_father

#define CLASS_FATHER_INIT(cls) father = new_class(father, cls);

#define CLASS_FATHER_DESTROY()del_class(father)

#define class_def(x)this->x = father->x

#define class_overwrite(x, fun) this->x = fun

/*===================================================

父类

===================================================*/

struct fat {

int age;

void (*create)(struct fat * this); //创建函数

void (*destroy)(struct fat * this); //销毁函数

void (*write)(struct fat * this); //成员函数

};

typedef struct fat * class_fat;

//父类写函数

void fat_write(class_fat this)

{

printf("fat_write \r\n");

}

//

void fat_create(class_fat this)

{

this->write = fat_write;

printf("fat_create \r\n");

}

void fat_destroy(class_fat this)

{

printf("fat_destroy \r\n");

}

/*===================================================

子类

===================================================*/

struct son {

CLASS_FATHER(fat); //继承此父类

void (*create)(struct son * this); //创建函数

void (*destroy)(struct son * this); //销毁函数

void (*write)(struct son * this); //成员函数

};

typedef struct son * clas_son;

void son_write(clas_son this)

{

father->write(father); //调用父类的写函数

printf("son_write \r\n");

}

void son_create(clas_son this)

{

CLASS_FATHER_INIT(fat);//声明父类

class_overwrite(write, son_write);//重写父函数

//class_def(write);//不重写父函数

printf("son_create \r\n");

}

void son_destroy(clas_son this)

{

CLASS_FATHER_DESTROY(); //注销父类

printf("son_destroy \r\n");

}

void plubic_write(clas_son son)

{

son->write(son);

}

int main(void)

{

struct son * son1 = new_class(son1, son); //创建一个实例

son1->write(son1); //调用写函数

del_class(son1); //销毁实例

while(1);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值