使C语言实现面向对象的三个要素,你掌握了吗?

使C语言实现面向对象的三个要素,你掌握了吗?

 

编排 | strongerHuang编程

微信公众号 | strongerHuang安全

 

不知道有多少人去了解过语言的发展史,早期C语言的语法功能其实比较简单。随着应用需求和场景的变化,C语言的语法功能在不断升级变化。微信

 

虽然咱们的教材有这么一个结论:C语言是面向过程的语言,C++是面向对象的编程语言,但面向对象的概念是在C语言阶段就有了,并且应用到了不少地方,好比某些操做系统内核、通讯协议等。app

 

面向对象编程,也就是你们说的OOP(Object Oriented Programming)并非一种特定的语言或者工具,它只是一种设计方法、设计思想,它表现出来的三个最基本的特性就是封装、继承与多态编程语言

 

为何要用C语言实现面向对象

 

阅读文本以前确定有读者会问这样的问题:咱们有C++面向对象的语言,为何还要用C语言实现面向对象呢?ide

 

C语言这种非面向对象的语言,一样也可使用面向对象的思路来编写程序的。只是用面向对象的C++语言来实现面向对象编程会更简单一些,可是C语言的高效性是其余面向对象编程语言没法比拟的。函数

 

固然使用C语言来实现面向对象的开发相对不容易理解,这就是为何大多数人学过C语言却看不懂Linux内核源码。工具

 

因此这个问题其实很好理解,只要有必定C语言编程经验的读者都应该能明白:面向过程的C语言和面向对象的C++语言相比,代码运行效率、代码量都有很大差别。在性能不是很好、资源不是不少的MCU中使用C语言面向对象编程就显得尤其重要。布局

 

具有条件

 

要想使用C语言实现面向对象,首先须要具有一些基础知识。好比:(C语言中的)结构体、函数、指针,以及函数指针等,(C++中的)基类、派生、多态、继承等。性能

 

首先,不只仅是了解这些基础知识,而是有必定的编程经验,由于上面说了“面向对象是一种设计方法、设计思想”,若是只是停留在字面意思的理解,没有这种设计思想确定不行。

 

所以,不建议初学者使用C语言实现面向对象,特别是在真正项目中。建议把基本功练好,再使用。

 

利用C语言实现面向对象的方法不少,下面就来描述最基本的封装、继承和多态。

 

C/C++的学习裙【七一二 二八四 七零五 】,不管你是小白仍是进阶者,是想转行仍是想入行均可以来了解一块儿进步一块儿学习!裙内有开发工具,不少干货和技术资料分享!

 

封装

 

封装就是把数据和函数打包到一个类里面,其实大部分C语言编程者都已经接触过了。

 

C 标准库中的 fopen(), fclose(), fread(), fwrite()等函数的操做对象就是 FILE。数据内容就是 FILE,数据的读写操做就是 fread()、fwrite(),fopen() 类比于构造函数,fclose() 就是析构函数。

 

这个看起来彷佛很好理解,那下面咱们实现一下基本的封装特性。

#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

// Shape 的属性
typedef struct {
    int16_t x; 
    int16_t y; 
} Shape;

// Shape 的操做函数,接口函数
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape const * const me);
int16_t Shape_getY(Shape const * const me);

#endif /* SHAPE_H */

 

这是 Shape 类的声明,很是简单,很好理解。通常会把声明放到头文件里面 “Shape.h”。来看下 Shape 类相关的定义,固然是在 “Shape.c” 里面。

#include "shape.h"

// 构造函数
void Shape_ctor(Shape * const me, int16_t x, int16_t y)
{
    me->x = x;
    me->y = y;
}

void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) 
{
    me->x += dx;
    me->y += dy;
}

// 获取属性值函数
int16_t Shape_getX(Shape const * const me) 
{
    return me->x;
}
int16_t Shape_getY(Shape const * const me) 
{
    return me->y;
}

 

再看下 main.c

#include "shape.h"  /* Shape class interface */
#include <stdio.h>  /* for printf() */

int main() 
{
    Shape s1, s2; /* multiple instances of Shape */

    Shape_ctor(&s1, 0, 1);
    Shape_ctor(&s2, -1, 2);

    printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
    printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));

    Shape_moveBy(&s1, 2, -4);
    Shape_moveBy(&s2, 1, -2);

    printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
    printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));

    return 0;
}

 

编译以后,看看执行结果:

Shape s1(x=0,y=1)
Shape s2(x=-1,y=2)
Shape s1(x=2,y=-3)
Shape s2(x=0,y=0)

 

整个例子,很是简单,很是好理解。之后写代码时候,要多去想一想标准库的文件IO操做,这样也有意识地去培养面向对象编程的思惟。

使C语言实现面向对象的三个要素,你掌握了吗?

 

继承

 

继承就是基于现有的一个类去定义一个新类,这样有助于重用代码,更好的组织代码。在 C 语言里面,去实现单继承也很是简单,只要把基类放到继承类的第一个数据成员的位置就好了。

 

例如,咱们如今要建立一个 Rectangle 类,咱们只要继承 Shape 类已经存在的属性和操做,再添加不一样于 Shape 的属性和操做到 Rectangle 中。

 

下面是 Rectangle 的声明与定义:

#ifndef RECT_H
#define RECT_H

#include "shape.h" // 基类接口

// 矩形的属性
typedef struct {
    Shape super; // 继承 Shape

    // 本身的属性
    uint16_t width;
    uint16_t height;
} Rectangle;

// 构造函数
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height);

#endif /* RECT_H */
#include "rect.h"

// 构造函数
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height)
{
    /* first call superclass’ ctor */
    Shape_ctor(&me->super, x, y);

    /* next, you initialize the attributes added by this subclass... */
    me->width = width;
    me->height = height;
}

 

咱们来看一下 Rectangle 的继承关系和内存布局:

使C语言实现面向对象的三个要素,你掌握了吗?

 

由于有这样的内存布局,因此你能够很安全的传一个指向 Rectangle 对象的指针到一个指望传入 Shape 对象的指针的函数中,就是一个函数的参数是 “Shape *”,你能够传入 “Rectangle *”,而且这是很是安全的。这样的话,基类的全部属性和方法均可以被继承类继承!

#include "rect.h"  
#include <stdio.h> 

int main() 
{
    Rectangle r1, r2;

    // 实例化对象
    Rectangle_ctor(&r1, 0, 2, 10, 15);
    Rectangle_ctor(&r2, -1, 3, 5, 8);

    printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r1.super), Shape_getY(&r1.super),
           r1.width, r1.height);
    printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r2.super), Shape_getY(&r2.super),
           r2.width, r2.height);

    // 注意,这里有两种方式,一是强转类型,二是直接使用成员地址
    Shape_moveBy((Shape *)&r1, -2, 3);
    Shape_moveBy(&r2.super, 2, -1);

    printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r1.super), Shape_getY(&r1.super),
           r1.width, r1.height);
    printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r2.super), Shape_getY(&r2.super),
           r2.width, r2.height);

    return 0;
}

 

输出结果:

Rect r1(x=0,y=2,width=10,height=15)
Rect r2(x=-1,y=3,width=5,height=8)
Rect r1(x=-2,y=5,width=10,height=15)
Rect r2(x=1,y=2,width=5,height=8)

 

多态

 

C++ 语言实现多态就是使用虚函数。在 C 语言里面,也能够实现多态。

 

如今,咱们又要增长一个圆形,而且在 Shape 要扩展功能,咱们要增长 area() 和 draw() 函数。可是 Shape 至关于抽象类,不知道怎么去计算本身的面积,更不知道怎么去画出来本身。并且,矩形和圆形的面积计算方式和几何图像也是不同的。

 

下面让咱们从新声明一下 Shape 类:

#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

struct ShapeVtbl;
// Shape 的属性
typedef struct {
    struct ShapeVtbl const *vptr;
    int16_t x; 
    int16_t y; 
} Shape;

// Shape 的虚表
struct ShapeVtbl {
    uint32_t (*area)(Shape const * const me);
    void (*draw)(Shape const * const me);
};

// Shape 的操做函数,接口函数
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape const * const me);
int16_t Shape_getY(Shape const * const me);

static inline uint32_t Shape_area(Shape const * const me) 
{
    return (*me->vptr->area)(me);
}

static inline void Shape_draw(Shape const * const me)
{
    (*me->vptr->draw)(me);
}


Shape const *largestShape(Shape const *shapes[], uint32_t nShapes);
void drawAllShapes(Shape const *shapes[], uint32_t nShapes);

#endif /* SHAPE_H */

 

看下加上虚函数以后的类关系图:

使C语言实现面向对象的三个要素,你掌握了吗?

 

5.1 虚表和虚指针

虚表(Virtual Table)是这个类全部虚函数的函数指针的集合。

 

虚指针(Virtual Pointer)是一个指向虚表的指针。这个虚指针必须存在于每一个对象实例中,会被全部子类继承。

 

在《Inside The C++ Object Model》的第一章内容中,有这些介绍。

 

5.2 在构造函数中设置vptr

在每个对象实例中,vptr 必须被初始化指向其 vtbl。最好的初始化位置就是在类的构造函数中。事实上,在构造函数中,C++ 编译器隐式地建立了一个初始化的vptr。在 C 语言里面, 咱们必须显示的初始化vptr。

 

下面就展现一下,在 Shape 的构造函数里面,如何去初始化这个 vptr。

static inline uint32_t Shape_area(Shape const * const me) {    return (*me->vptr->area)(me);}

 

5.3 继承 vtbl 和 重载 vptr

上面已经提到过,基类包含 vptr,子类会自动继承。可是,vptr 须要被子类的虚表从新赋值。而且,这也必须发生在子类的构造函数中。下面是 Rectangle 的构造函数。

#include "rect.h"  
#include <stdio.h> 

// Rectangle 虚函数
static uint32_t Rectangle_area_(Shape const * const me);
static void Rectangle_draw_(Shape const * const me);

// 构造函数
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height)
{
    static struct ShapeVtbl const vtbl = 
    {
        &Rectangle_area_,
        &Rectangle_draw_
    };
    Shape_ctor(&me->super, x, y); // 调用基类的构造函数
    me->super.vptr = &vtbl;           // 重载 vptr
    me->width = width;
    me->height = height;
}

// Rectangle's 虚函数实现
static uint32_t Rectangle_area_(Shape const * const me) 
{
    Rectangle const * const me_ = (Rectangle const *)me; //显示的转换
    return (uint32_t)me_->width * (uint32_t)me_->height;
}

static void Rectangle_draw_(Shape const * const me) 
{
    Rectangle const * const me_ = (Rectangle const *)me; //显示的转换
    printf("Rectangle_draw_(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(me), Shape_getY(me), me_->width, me_->height);
}

 

5.4 虚函数调用

有了前面虚表(Virtual Tables)和虚指针(Virtual Pointers)的基础实现,虚拟调用(后期绑定)就能够用下面代码实现了。

uint32_t Shape_area(Shape const * const me)
{
    return (*me->vptr->area)(me);
}

这个函数能够放到.c文件里面,可是会带来一个缺点就是每一个虚拟调用都有额外的调用开销。为了不这个缺点,若是编译器支持内联函数(C99)。咱们能够把定义放到头文件里面,相似下面:

static inline uint32_t Shape_area(Shape const * const me) 
{
    return (*me->vptr->area)(me);
}

 

若是是老一点的编译器(C89),咱们能够用宏函数来实现,相似下面这样:

#define Shape_area(me_) ((*(me_)->vptr->area)((me_)))

 

看一下例子中的调用机制:

使C语言实现面向对象的三个要素,你掌握了吗?

 

5.5 main.c

#include "rect.h"  
#include "circle.h" 
#include <stdio.h> 

int main() 
{
    Rectangle r1, r2; 
    Circle    c1, c2; 
    Shape const *shapes[] = 
    { 
        &c1.super,
        &r2.super,
        &c2.super,
        &r1.super
    };
    Shape const *s;

    // 实例化矩形对象
    Rectangle_ctor(&r1, 0, 2, 10, 15);
    Rectangle_ctor(&r2, -1, 3, 5, 8);

    // 实例化圆形对象
    Circle_ctor(&c1, 1, -2, 12);
    Circle_ctor(&c2, 1, -3, 6);

    s = largestShape(shapes, sizeof(shapes)/sizeof(shapes[0]));
    printf("largetsShape s(x=%d,y=%d)\n", Shape_getX(s), Shape_getY(s));

    drawAllShapes(shapes, sizeof(shapes)/sizeof(shapes[0]));

    return 0;
}

输出结果:

largetsShape s(x=1,y=-2)
Circle_draw_(x=1,y=-2,rad=12)
Rectangle_draw_(x=-1,y=3,width=5,height=8)
Circle_draw_(x=1,y=-3,rad=6)
Rectangle_draw_(x=0,y=2,width=10,height=15)

 

使C语言实现面向对象的三个要素,你掌握了吗?

 

总结

 

仍是那句话,面向对象编程是一种方法,并不局限于某一种编程语言。用 C 语言实现封装、单继承,理解和实现起来比较简单,多态反而会稍微复杂一点,若是打算普遍的使用多态,仍是推荐转到 C++ 语言上,毕竟这层复杂性被这个语言给封装了,你只须要简单的使用就好了。但并不表明,C 语言实现不了多态这个特性。

相关文章
相关标签/搜索