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 <stdio.h>


/*====================================================================
        以下是相关宏
====================================================================*/
#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;
}

发布了139 篇原创文章 · 获赞 54 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/aa120515692/article/details/104020975