用C語言實現C 中類的封裝繼承多型

2021-10-03 08:59:05 字數 4552 閱讀 1345

作用:① 標頭檔案中使用,防止標頭檔案被多重呼叫;② 作為測試使用,省去注釋**的麻煩;③ 作為不同角色或者場景的判斷使用。。

使用:把頭檔案的內容都放在#ifndef和#endif中

#ifndef 《標識》

#define 《標識》

......

......

#endif

解釋:《標識》在理論上來說可以是自由命名的,但每個標頭檔案的這個「標識」都應該是唯一的。標識的命名規則一般是頭檔名全大寫,前後加下劃線,並把檔名中的「.」也變成下劃線,如:stdio.h

#ifndef _stdio_h_

#define _stdio_h_

......

#endif

封裝的含義是隱藏內部的行為和資訊,使用者能看到對外提供的介面和公開的資訊。

實現方法:

這樣可以隱藏內部資訊,因為外部不知道物件所佔記憶體的大小,所以不能靜態的建立該類的物件,只能呼叫類提供的建立函式才能建立。這種方法的缺陷是不支援繼承,因為子類中得不到任何關於父類的資訊。

檔案

#ifndef point_h

#define point_h

struct point;

point* new_point(double x,double y); //建立乙個新物件

void free_point(point* point_);//釋放乙個物件

#endif

檔案

#include"point.h"

#include #include struct point

;point* new_point(double x, double y)

void free_point(point* point_)

}

//主程式中

#include #include"point.h"

#include"stdio.h"

int main()

這樣使用point.h 的程式就不知道 point 的內部結構,實現了資料的封裝,外部只能使用宣告的兩個函式

如此操作,只有類的實現**才知道priv或者結構體的真正定義

檔案中

#ifndef point _h

#define point_h

typedef

struct point point;

typedef

struct pointprivate pointprivate;

struct point

;double

get_x

(point* point_)

;double

get_y

(point* point_)

;point*

new_point

(double x_,

double y_)

;//建立新物件

void

free_point

(point* point_)

;// 釋放物件

#endif

檔案中

#include"point.h"

#include"stdlib.h"//malloc的標頭檔案

#include"stdio.h"

struct pointprivate

;double get_x(point* point_)

double get_y(point* point_)

point* new_point(double x_, double y_)

void free_point(point* point_)

}

//主程式中

#include

#include

"point.h"

#include

"stdio.h"

using

namespace std;

intmain()

c語言實現繼承是通過結構體巢狀實現的。

typedef struct person person;

typedef struct person_act person_act;

//虛函式表結構

struct person_act

;//基類

struct person

;void person_eat(void* thi)

;void person_drink(void* thi)

;person* person_init(

);//建構函式

void person_die(person* per)

;//析構函式

說明:

c語言實現多型需要借助自定義的虛函式表結構體,結構體中為虛函式指標,表示需要實現多型的虛函式;

結構體person為定義的基類,並自定義建構函式和析構函式。

#include"person.h"

#include"stdlib.h"

#include"string.h"

#include"stdio.h"

void person_eat(void* thi)

void person_drink(void* thi)

person_act p_act =

;//建構函式,需要顯示呼叫

person* person_init(

)//析構函式,需要顯示呼叫

void person_die(person* per)

說明:

在cpp檔案中定義建構函式和析構函式,但是在實際使用時,不像類一樣自動呼叫,需要人為顯示呼叫。

在建構函式中,將實際的函式寫入其中,作為此結構體的」虛函式「。

#pragma once

#include"person.h"

typedef struct worker worker;

typedef struct workerprivate workerprivate;

//派生類

struct worker

;void worker_eat(void* thi)

;void worker_drink(void* thi)

;worker* worker_init(const int num)

;//建構函式

void worker_die(worker* wor)

;int get_num(worker* wor)

;

說明:

定義派生類worker,將基類person的物件作為結構體的成員實現派生,注意需要將其寫在本結構體自身成員的前面——實現繼承。

另外,此處將派生類的成員資訊重新定義乙個結構體,其宣告放在.**件中,定義放在.cpp檔案中,實現資料的封裝。在.**中列下介面函式get_num,作為外部函式介面,訪問資料。

#include"worker.h"

#include"stdlib.h"

#include"stdio.h"

#include"string.h"

struct workerprivate

;void worker_eat(void* thi)

void worker_drink(void* thi)

person_act w_act =

;worker* worker_init(const int num)

int get_num(worker* wor)

說明:

此處定義的結構體:workerprivate是為了實現資料封裝,在此處定義。並在cpp檔案中重新定義其對應的虛函式,將其在建構函式中寫入worker對應的結構體。

get_num是結構體對外的介面函式,用來訪問其」私有資料「——num

#include 

#include"person.h"

#include"worker.h"

#include"stdlib.h"

int main(

)

說明:

基類測試:例項化基類物件,並通過基類物件呼叫其對應的函式

派生類測試:例項化派生類物件,直接用物件訪問其資料成員num不可以,實現了資料封裝;但可以通過函式get_num訪問其數成員。另外,可以通過派生類物件呼叫其自身的函式。

多型測試:多型是指函式會根據指標的內容決定呼叫哪個函式,而不是根據指標的型別決定。通過基類指標指向派生類物件,呼叫時仍是呼叫派生類的函式,不是呼叫基類的函式——即實現了虛函式的功能。

所有有c++編碼規範的公司,都會對指標有大篇幅的約束,因為指標無所不能,剛才也看到了,就連程式的基礎執行都依賴指標

虛函式表結構體也是通過指標實現

C語言實現封裝 繼承 多型

c語言中雖然沒有類,但有struct和指標。我們可以在乙個struct中存入資料和函式指標,以此來模擬類行為。typedef struct parent parent typedef struct child child include include 模擬乙個類a typedef struct a...

C語言 用C語言實現C 繼承與多型

繼承就是當建立乙個類時,不需要重新編寫新的資料成員和成員函式,只需指定新建的類繼承了乙個已有的類的成員即可。這個已有的類稱為基類,新建的類稱為派生類。舉個例子 人是動物,人具有動物的行為和屬性,但人也有動物所不具備的行為和屬性。動物行為 屬性會動體力人 行為屬性 會動體力 會學習智力 本實現使用了組...

C語言實現C 中的類

華科複試面試題 c語言如何實現c 中的類 include c 語言沒有類,但可以用結構體充當乙個類 與類不同,結構體只能定義變數,不能夠定義函式,可以通過函式指標的方法來實現其功能 定義 類 的成員變數以及方法 typedef struct personperson 定義函式功能 void eatf...