/* 父类 */
struct parent_class
{
int a, b;
char *str;
};
/* 继承于父类的子类 */
struct child_class
{
struct parent class p;
int a, b;
};
/* 操作示例函数*/
void func()
{
struct child_class obj, *obj_ptr; /* 子类对象及指针 */
struct parent_class *parent_ptr; /* 父类指针 */
obj_ptr = &obj;
/* 取父指针 */
parent_ptr = (struct parent*) &obj;
/* 可通过转换过类型的父类指针访问相应的属性 */
parent ptr->a = 1;
parent ptr->b = 5;
/* 子类属性的操作 */
obj ptr->a = 10;
obj ptr->b = 100;
}
在上面代码中,注意child_class结构中第一个成员p,这种声明方式代表child_class类型的数据中开始的位置包含一个parent_class类型的变量。在函数func中obj是一个child_class对象,正像这个结构类型指示的,它前面的数据应该包含一个parent_class类型的数据。在第21行的强制类型赋值中parent_ptr指向了obj变量的首地址,也就是obj变量中的p对象。好了,现在parent_ptr指向的是
一个真真实实的parent类型的结构,那么可以按照parent的方式访问其中的成员,当然也包括可以使用和parent结构相关的函数来处理内部数据,因为一个正常的,正确的代码,它是不会越界访问parent结构体以外的数据。经过这基本的结构体层层相套包含,对象简单的继存关系就体现出来了:父对象放于数据块的最前方,代码中可以通过强制类型转换获得父对象指针。
/* 抽象父类 */
struct parent_class
{
int a;
/* 反映不同类别属性的方法 */
void (*vfunc)(int a);
}
/* 抽象类的方法调用 */
void parent_class_vfunc(struct parent_class *self, int a)
{
assert(self != NULL);
assert(slef->vfunc != NULL);
/* 调用对象本身的虚拟函数 */
self->vfunc(a);
}
/* 继承自parent class的子类 */
struct child_class
{
struct parent_class parent;
int b;
};
/* 子类的构造函数 */
void child_class_init(struct child_class* self)
{
struct parent_class* parent;
/* 强制类型转换获得父类指针 */
parent = (struct base_class*) self;
assert(parent != NULL);
/* 设置子类的虚拟函数 */
parent->vfunc = child_class_vfunc;
}
/* 子类的虚拟函数实现 */
static void child class vfunc(struct child class*self, int a)
{
self->b = a + 10;
}
#include <stdio.h>
#include <stdlib.h>
//接口
#ifndef Interface
#define Interface struct
#endif
//类
#ifndef Class
#define Class struct
#endif
//抽象形状类
Class Shape;
typedef Class Shape shape;
//抽象形状类的方法声明
shape* Shape(int edges);
int shape_getEdges(shape *);
int shape_getArea(void);
void _Shape(shape *);
//三角形类
Class Triangle;
typedef Class Triangle triangle;
//三角形类的方法声明
triangle * Triangle(int bottom, int height);
int triangle_getEdges(triangle *);
int triangle_getArea(triangle *);
void _Triangle(triangle *);
//矩形类
Class Rectangle;
typedef Class Rectangle rectangle;
//矩形类的方法声明
rectangle * Rectangle(int bottom, int height);
int rectangle_getEdges(rectangle *);
int rectangle_getArea(rectangle *);
void _Rectangle(rectangle *);
//抽象形状类实现
Class Shape
{
int edges;
int (*getEdges)(shape*);
int (*getArea)(void);
};
//形状类构造函数
shape* Shape(int edges)
{
shape * obj = (shape *) malloc(sizeof(shape));
obj->edges = edges;
obj->getEdges = shape_getEdges;
obj->getArea = shape_getArea;
return obj;
}
int shape_getEdges(shape* obj)
{
return obj->edges;
}
int shape_getArea(void)
{
return -1;
}
//形状类析构函数
void _Shape(shape * obj)
{
if(obj == NULL)
return;
free(obj);
}
//三角形类实现
Class Triangle
{
shape * super;
int bottom;
int height;
int (*getEdges)(triangle *);
int (*getArea)(triangle *);
};
//三角形类构造函数
triangle * Triangle(int bottom, int height)
{
triangle* obj = (triangle*) malloc(sizeof(triangle));
//调用Shape构造函数用于实现继承
obj->super = Shape(3);
obj->bottom = bottom;
obj->height = height;
obj->getEdges = triangle_getEdges;
obj->getArea = triangle_getArea;
return obj;
}
int triangle_getEdges(triangle * obj)
{
return obj->super->edges;
}
int triangle_getArea(triangle * obj)
{
return (obj->bottom * obj->height) / 2;
}
//三角形类析构函数
void _Triangle(triangle * triangle)
{
_Shape(triangle->super);
if(triangle == NULL)
{
return;
}
free(triangle);
}
//矩形类实现
Class Rectangle
{
shape * super;
int bottom;
int height;
int (*getEdges)(rectangle *);
int (*getArea)(rectangle *);
};
//矩形类构造函数
rectangle * Rectangle(int bottom, int height)
{
rectangle * obj = (rectangle *)malloc(sizeof(rectangle));
//调用Shape构造函数用于实现继承
obj->super = Shape(4);
obj->bottom = bottom;
obj->height = height;
obj->getEdges = rectangle_getEdges;
obj->getArea = rectangle_getArea;
return obj;
}
int rectangle_getEdges(rectangle * obj)
{
return obj->super->edges;
}
int rectangle_getArea(rectangle * obj)
{
return (obj->bottom * obj->height);
}
//矩形类析构函数
void _Rectangle(rectangle * obj)
{
_Shape(obj->super);
if(obj == NULL)
{
return;
}
free(obj);
}
//测试
void main(){
shape* shapeObj = Shape(0);
printf("%d\n", shapeObj->getEdges(shapeObj));
printf("%d\n", shapeObj->getArea());
_Shape(shapeObj);
triangle* triangleObj = Triangle(4, 5);
printf("%d\n", triangleObj->getEdges(triangleObj));
printf("%d\n", triangleObj->getArea(triangleObj));
_Triangle(triangleObj);
rectangle* rectangleObj = Rectangle(4, 5);
printf("%d\n", rectangleObj->getEdges(rectangleObj));
printf("%d\n", rectangleObj->getArea(rectangleObj));
_Rectangle(rectangleObj);
}