用C语言模拟面向对象

 这个代码是修改自lwoopc,截取其中一部分,然后添加一部分。

在VC6.0和gcc可编译,大家可以从程序代码中的"//文件开始部分"开始看


CTOR是构造,DTOR是析构


#include <stdio.h>
#include <malloc.h>


#define CLASS(type)          		\
typedef struct type type;           	\
type* type##_new( );			\
void type##_ctor(type* t);      	\
void type##_dtor(type* t);      	\
void type##_delete(type* t);        	\
struct type



#define CTOR(type,sencentes)						\
  type* type##_new() {							\
  struct type *cthis;							\
  cthis = (struct type*)malloc(sizeof(struct type));			\
  if(!cthis)								\
  {									\
   return 0;								\
  }									\
  type##_ctor(cthis);							\
  return cthis;								\
 }									\
									\
void type##_ctor(type* cthis) {sencentes;}        
   


  

#define DTOR(type,sencentes)					\
void type##_delete(type* cthis)					\
{								\
        type##_dtor(cthis);					\
	free(cthis);						\
}								\
void type##_dtor(type* cthis){sencentes;}   



#define SET(name,value) ( cthis -> name ) = (value)

#define FUN(retType,funName) retType(* funName)
#define CONNECT(funName) ( cthis -> funName ) = (& _##funName)
#define INCALL(funName)  (* cthis->funName)		//在成员函数内调用其他函数
#define CALL(this,funName) (* this->funName)




//文件开始部分
void _coutSome(char * str)			//在原来函数名前加上_,和CONNECT宏相关
{
	printf("%s\n",str);
}


//haha 类
CLASS(haha){				//声明type##_new()和type##_ctor() 和 type##_delete()和type##_dtor() 函数
	#define MEMhaha int a;	\
			int b;	
	FUN(void,coutSome)();		//没定义在MEMhaha里面,表示自己专用的
	MEMhaha
};					

CTOR(haha,SET(a,10);
	  SET(b,5);
	  CONNECT(coutSome))		//定义type##_new()和type##_ctor()函数    括号内第二个参数中的语句用;号隔开
DTOR(haha,SET(a,0);
	  INCALL(coutSome) ("dtoring..."))//定义type##_delete()和type##_dtor()函数    括号内第二个参数中的语句用;号隔开


//wuwu 类,只有部分成员继承于haha,构造和析构等不继承,因为担心内存地址错误【继承后的排布不同】
CLASS(wuwu){ 	
	#define MEMwuwu MEMhaha \
			char c;
	MEMwuwu
};

CTOR(wuwu, printf("I am ctoring...\n");
	   SET(a,10);
	   SET(c,'w') )		//语句用;号隔开
DTOR(wuwu, printf("I am dtoring...\n");
           SET(a,0) ;
 	   SET(c,'?') )


//erer 类,和wuwu一模一样,但是本身只是把wuwu中的成员链接过来
CLASS(erer) {
	#define MEMerer 
	MEMwuwu
};

CTOR(erer, printf("I am ctoring...\n");
	   SET(a,10);
	   SET(c,'w') )		//语句用;号隔开
DTOR(erer, printf("I am dtoring...\n");
	   SET(a,0) ; 
           SET(c,'?') )


void main()
{

	// 需要自己new和delete哦,C语言只能先定义后使用,所以下面几行必须放在最开始
	wuwu *w;
	haha *h;
	erer *e;

	printf("\n\n\n");

	h = haha_new();

	CALL(h,coutSome)("newed");

	printf("new:%d\n",h->a);
	printf("new:%d\n",h->b);

	h->a  = 100;
	printf("set value:%d\n",h->a);

	haha_delete(h);			//test
	printf("delete:%d\n",h->a);	//test

	

	printf("\n\n\n");

	w = wuwu_new();			//继承

	printf("new:%d\n",w->a);
	printf("new:%c\n",w->c);

	w->a  = 100;
	w->c  = 'c';
	printf("set value:%d\n",w->a);
	printf("set value:%c\n",w->c);

	wuwu_delete(w);			//test,这个正常情况下不被调用
	printf("dtor:%d\n",w->a);
	printf("dtor:%c\n",w->c);


	printf("\n\n\n");

	e = erer_new();			

	printf("new:%d\n",e->a);
	printf("new:%c\n",e->c);

	e->a  = 100;
	e->c  = 'c';
	printf("set value:%d\n",e->a);
	printf("set value:%c\n",e->c);

	erer_delete(e);
	printf("dtor:%d\n",e->a);	//test
	printf("dtor:%c\n",e->c);

	printf("\n\n\n");

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值