Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44686
  • 博文数量: 11
  • 博客积分: 228
  • 博客等级: 二等列兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-24 10:37
文章分类

全部博文(11)

文章存档

2012年(3)

2011年(8)

分类: C/C++

2011-10-24 18:02:49

一、类的封装实现:借用高焕堂的宏文件,类很易封装为如下的格式

1、类的定,其中 CLASS()   lw_oopc_kc.h 中定义的宏

#include "lw_oopc_kc.h"

CLASS(A)

{

int a; void(*init)(void*,int); void(*put)(void*);

};

2、成员函的实现

类的封装质是用借用 struct  结构体,用函数表示 C++的方法成员函。接来给类 A 的方法写实函数。

void init_A(void *t,int x)

{

A *cthis = (A*)t;

cthis->a = x;

}

void put_A(void*t)

{

A *cthis = (A*)t;

printf(" %d ",cthis->a);

}

3、类(结体)中函数指与实现数的关联 通过下面宏把类方法(数指针和实现联:

CTOR(A)

FUNCTION_SETTING (init, init_A); FUNCTION_SETTING (put, put_A);

END_CTOR

4、对象的义、构和初始化

如果没有这个连接处理,类实际struct数指针就没有函数的功能。函 init_A()XXX_A()  名模式是指明 XXX_A()属于 A 的函数,便程序理解和护。下就是要构造 。在 C++中这系统用构实现C  这个能显调用lw_oopc_kc.h (或"lw_oopc.h")可以利用宏 CLASS_CTOR(classobj)来将定义的对象行构造使之 有数据的时有方的功能实例化个对象 3 如下:

A   aa1;                           // 1、定义对象

CLASS_CTOR(Aaa1);       // 2、构造对象使得函指针和数关联

aa1.init(&aa1, 10);               // 3、初始化对的成员注意要: &aa1(取址)

二、C 继承的实现:

1、子类的定义:在类的开头用已经定义的进行定义一个变量,为了更加明白,明是继承,增加一个宏定义:

#define INHERIT(BASE)    IMPLEMENTS(BASE)

于是以类 B A 为例子下:

CLASS(B)

{

INHERIT(A);              //  继承 A int b;                                   //  的成员 void (*init) (void*, int x);

void (*put) (void*);

};

2、子类的成员函数实现,为了方便辨别,类 B 员函数带后缀 _B

void init_B (void*t, int x, int y)

{

B *cthis =   (B*) t;

CLASS_CTOR(A, cthis->A);               //----继承的类在这里构造,对象是 cthis->A cthis->A.init(&cthis->A, x);                //----继承的基类的初始, 注意&cthis->A cthis->b = y;

}

void put_B (void *t)

{

B *cthis = (B*) t;

cthis->A.put (&cthis->A);                    //---子类调用父类的方式

printf(" %d ",cthis->b);                          //---出类成

}

3、子类的构造函数,和无继承类样,将函数指和函数关联

CTOR(B)

FUNCTION_SETTING (init, init_B);                    //---数指针和数关联的宏

FUNCTION_SETTING (put, put_B); END_CTOR

说明对基类构造在子类构造宏 CTOR(B) END_CTOR 之间进行那时候B 没有实例化故没有实对象CLASS_CTOR(A, cthis->A);不能放在里面,只好init_B() 函数里面因为那 B 类已有实例对象样的做法与 C++法不一C++在构造 B 的对象时先调用 A 类的造函数例化对中的类部分。面为 main()函数的用处理:

int main()

{

A aa1; B b

CLASS_CTOR(A,aa1);                              //--构造 aa1 对象 aa1.init(&aa1,5);                                                               //--初始化 aa1 对象 aa1.put(&aa1);                                                               //--调用 aa1 对象成员函数

CLASS_CTOR(B, b);                                //--- b 对象

b.init(&b,100,78);                                     //--初始化 b 对象包括基类 A 的构造和初始化

b.put(&b);                                                //--调用 b 对象成函数

b.A.put(&b.A);                                         //--调用 b 对象的基类成员函数

return 0;

输出结果5    100   78      100

多态的实现简而之即一接口多种实现是用相的抽象的代码现不同 的功能 C 中多的实现通过接来实现的 lw_oopc.h 宏文件一个计的多态例子如下:

1接口的定义本例是实现加法减法运算减都是调用类的同一个成员函数却分别实现 了加、减的功能。本例的接口表示获计算结果,不知道采样什么样的计算方法。

/*    operater.h   */

#ifndef OPER_H

#define OPER_H INTERFACE(IOPERATOR)

{

double (*GetResult)(double,double);

};

#endif

/*-------------end of operater.h ------------------*/

2 在加法类 Add 中实现接口 IOPERATOR

/*****   Add.C ***/

#include "lw_oopc_kc.h"

#include"operater.h"      // 头文件序很重lw_oopc_kc.h 在前,因很简不解释

#include "classes.h"

/************************** Add 定义在 classes.h

CLASS(Add)

{

IMPLEMENTS(IOPERATOR);

};************/

static double GetResult(double a,double b)

{

return (a+b);

}

CTOR(Add)

FUNCTION_SETTING(IOPERATOR.GetResult,GetResult); END_CTOR

/***----- END OF ADD.C-----****/

3 在减法类 Sub 中实现接口 IOPERATOR

/***--- Sub.c ---******/

#include "lw_oopc_kc.h"

#include"operater.h"

#include "classes.h"

/*********** Sub 定义在 classes.h

CLASS(Sub)

{

IMPLEMENTS(IOPERATOR);

};*/

static double GetResult(double a,double b)

{

return (a-b);

}

CTOR(Sub) FUNCTION_SETTING(IOPERATOR.GetResult,GetResult);

END_CTOR

/***----- END OF Sub.C-----****/

4


 

4 组合,把 operater.hAdd.CSub.C main.C 组成一个工程,main.c 文件如下

/***--- main.c ---******/

#includetdio.h>

#include "lw_oopc_kc.h"

#include"operater.h"      // 头文件序很讲lw_oopc_kc.h 必须在

#include "classes.h" int  main()

{

int a = 10, b=5;

int c1,c2;

IOPERATOR *poper;               //--指针,用针实现多态

Add A                          //---对象 A 成员数实现

Sub S                          //--- B 成员函数现减法

CLASS_CTOR(Add, A); CLASS_CTOR(Sub, S);

//---态内存理方法

poper = &A;        //也可动态内方法:oper = New(Add); 记得 free()

c1 = (poper->GetResult(a,b));      // c1 结果 = 15 a+b

poper = &S;

c2 = poper->GetResult(a,b);      // c2 = 5 a-b

return 0;

}

/***----- END OF main.C-----****/

总结:

1、在 lw_oopc_kc.h 的基础上,为了增加可理解性不改变原作含义为前提下,增加了以下宏

#define CLASS_CTOR(Class,obj)       Class##Setting(&obj)        //--的构造宏

#define INHERIT(BASE)     IMPLEMENTS(BASE)                        //---类继承宏

#ifndef LW_OOPC_PURE_STATIC

#ifndef LW_OOPC_STATIC

#define New(Class)     Class##New()                  //--象动态造宏

#endif

#endif

2类的实例化必须有 3 定义构造初始化尤其初始化时候通常是通过指针的应来实现对类内部成员的访问。

3继承实现方法用父类名在子类中定义一作为子类的一个成员变量通过拥有该对象实 现子类对父类功能的拥有,即继承。

注意子类成员中用父定义的对象,其构造数要放在子类的初始化函数(本人的解决方 法因为子类的构造函数通过宏实现,不能直接用父类的构造函(如果您有更好办法,请和给大家分享)。

5 函数和函数指针的写法:将函数名变指针,函数数只要参数说明。

e.g.        double GetResult(double a,double b) 转为函数针为:

double * GetResult(double, double

------------------------------------------------------------------------------------

附:lw_oopc_kc.h 宏文件内容

/*  lw_oopc_kc.h */

#ifndef _LW_OOPC_H

#define _LW_OOPC_H

#include <stdlib.h>

#define CLASS(type) \ typedef struct type type; \ struct type

#ifndef LW_OOPC_PURE_STATIC

#ifndef LW_OOPC_STATIC

#ifndef LW_OOPC_DYNAMIC

#define CTOR(type) \

void* type##Setting(type*);      \

void* type##New()\

{  \

struct type *t; \

t = (struct type *)malloc(sizeof(struct type)); \

return type##Setting(t); \

}  \

void* type##Setting(type *t) \

{

#else

#define CTOR(type) \

void* type##New()\

{  \

struct type *t; \

t = (struct type *)malloc(sizeof(struct type));

#endif

#else

#define CTOR(type) \

void* type##Setting(type *t) \

{

#endif

#endif

#define END_CTOR return (void*)t;      }

#define FUNCTION_SETTING(f1, f2)      t->f1 = f2;

#define IMPLEMENTS(type) type type

#define INTERFACE(type) \ typedef struct type type; \ struct type

#endif

/*     end     */

阅读(533) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:strcpy函数与memcpy函数

给主人留下些什么吧!~~