如何使用C語言的物件導向

來源:互聯網
上載者:User

如何使用C語言的物件導向

我們都知道,C++才是物件導向的語言,但是C語言是否能使用物件導向的功能?

(1)繼承性

typedef struct _parent
{
int data_parent;
}Parent;
typedef struct _Child
{
struct _parent parent;
int data_child;
}Child;

在設計C語言繼承性的時候,我們需要做的就是把基礎資料放在繼承的結構的首位置即可。這樣,不管是資料的訪問、資料的強轉、資料的訪問都不會有什麼問題。

(2)封裝性

class的類成員預設情況下都是private,而struct的成員都是public(不能改變),所以如何讓C語言實現封裝的功能呢?答案就是函數指標;這在核心中得到了廣泛的應用;

struct _Data;
typedef  void (*process)(struct _Data* pData);
typedef struct _Data
{
    int value;
    process pProcess;
}Data;

封裝性的意義在於,函數和資料是綁在一起的,資料和資料是綁在一起的。這樣,我們就可以通過簡單的一個結構指標訪問到所有的資料,遍曆所有的函數。封裝性,這是類擁有的屬性,當然也是資料結構體擁有的屬性。

(3)多態性

在C++中,多態通常都是使用虛函數來實現的,但是C語言中並沒有虛函數,如何?重載呢?

答案也顯而易見,也是函數指標的擴充,以下面例子為例:

#include <stdio.h>
#include <stdlib.h>

//虛函數表結構
struct base_vtbl
{
    void(*dance)(void *);
    void(*jump)(void *);
};

//基類
struct base
{
    /*virtual table*/
    struct base_vtbl *vptr;
};

void base_dance(void *this)
{
    printf("base dance\n");
}

void base_jump(void *this)
{
    printf("base jump\n");
}

/* global vtable for base */
struct base_vtbl base_table =
{
        base_dance,
        base_jump
};

//基類的建構函式
struct base * new_base()
{
    struct base *temp = (struct base *)malloc(sizeof(struct base));
    temp->vptr = &base_table;
    return temp;
}


//衍生類別
struct derived1
{
    struct base super;
    /*derived members */
    int high;
};

void derived1_dance(void * this)
{
    /*implementation of derived1's dance function */
    printf("derived1 dance\n");
}

void derived1_jump(void * this)
{
    /*implementation of derived1's jump function */
    struct derived1* temp = (struct derived1 *)this;
    printf("derived1 jump:%d\n", temp->high);
}

/*global vtable for derived1 */
struct base_vtbl derived1_table =
{
    (void(*)(void *))&derived1_dance,
    (void(*)(void *))&derived1_jump
};

//衍生類別的建構函式
struct derived1 * new_derived1(int h)
{
    struct derived1 * temp= (struct derived1 *)malloc(sizeof(struct derived1));
    temp->super.vptr = &derived1_table;
    temp->high = h;
    return temp;
}

 

int main(void)
{

    struct base * bas = new_base();
    //這裡調用的是基類的成員函數
    bas->vptr->dance((void *)bas);
    bas->vptr->jump((void *)bas);


    struct derived1 * child = new_derived1(100);
    //基類指標指向衍生類別
    bas  = (struct base *)child;

    //這裡調用的其實是衍生類別的成員函數
    bas->vptr->dance((void *)bas);
    bas->vptr->jump((void *)bas);
    return 0;
}

綜上所述,可以實現C語言的物件導向功能;

本文永久更新連結地址:https://www.bkjia.com/Linux/2018-02/151062.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.