///
//////
//////
//////
/////
//////
//////
//////
////
//基類
class object
virtual
double calcuarae() = 0; //計算表面積
virtual
double calcuvolume() = 0;//計算體積
//有乙個共同的屬性
double height;
};//圓柱體
class cylinder :public object
virtual
double calcuarae();
virtual
double calcuvolume();
double radius;
};//長方體
class cuboid :public object
virtual
double calcuarae();
virtual
double calcuvolume();
double weith;
double length;
};
實現:
///
//////
//////
//////
////
//////
//////
////////
//基類建構函式
object::object(double h)
:height(h)
//////
//////
//////
//////
//////
//////
//////
//////
//////
///cylinder::cylinder(double h, double r)
:object(h)
,radius(r)
double cylinder::calcuarae()
double cylinder::calcuvolume()
//////
//////
//////
//////
//////
//////
//////
//////
//////
///cuboid::cuboid(double h, double w, double l)
:object(h)
,weith(w)
,length(l)
double cuboid::calcuarae()
double cuboid::calcuvolume()
多型:
qvectorvec;
vec<< new cylinder(2,3);
vec<< new cuboid(2,3,4);
for(int i = 0 ; i < vec.size();i++)
c++的多型是依靠虛函式來實現的。
在c++中使用class來封裝資料與方法的 在c語言中使用的struct來封裝資料與方法的,在標準的c執行庫裡一組函式fopen(), fclose(),fread(), and fwrite() ,就是在操作file結構的,每個函式都會傳入乙個file的指標。
封裝比較好辦
//抽象物件
typedef
struct
shape;
//建構函式
shape *shape_new(int16_t x, int16_t y)
//方法
void shape_moveby(shape * const me, int16_t dx, int16_t dy)
c語言裡的繼承有點像c++的組合,子類包函父類的物件。
//具體物件
typedef
struct
rectangle;
//建構函式
rectangle*rectangle_new(int16_t x, int16_t y,uint16_t width, uint16_t height)
//方法,使用基類的方法
應用:
rectangle* r1 = rectangle_new(1,1,2,3);
shape_moveby((shape *)r1, -2, 3);//向上型別轉換,upcasting在c++裡是安全的
先封裝:
//基類
typedef struct
object;
//虛表類
struct objectvtbl
;//cylinder子類
typedef struct
cylinder;
//cuboid子類
typedef struct
cuboid;
再把所有的方法實現了:
這些方法只在模組內使用不expouse給使用者
static
double cylinder_area(object * const o)
static
double cylinder_volume(object * const o)
//////
//////
//////
//////
//////
//////
//////
//////
//////
///////
static
double cuboid_volume(object * const o)
static
double cuboid_area(object * const o)
再把建構函式給實現了:
cuboid * new_cuboid(double l,double w,double h)
; cuboid * c = (cuboid *)malloc(sizeof(cuboid));
c->super
.vptr = &vtbl;
c->super
.height = h;
c->length = l;
c->width = w;
return c;
}cylinder * new_cylinder(double r,double h)
; cylinder * c = (cylinder *)malloc(sizeof(cylinder));
c->super
.vptr = &vtbl;
c->super
.height = h;
c->radius = r;
return c;
}
最後把要後繫結的方法實現了:
static
double calcu_area(object * const o)
static
double calcu_volume(object * const o)
使用:
cylinder *cylinder = new_cylinder(2,6);
cuboid *cuboid = new_cuboid(2,6,5);
object *table[2];
table[0] = (object *)cylinder;
table[1] = (object *)cuboid;
for(int i = 0 ; i < 2 ;i++)
object oriented programming 是一種設計方法而非一種語言
封裝與繼承,c語言可以輕鬆的handle,但是多型的話相對複雜,而且沒有帶來任何效能上的提公升。
如果是寫框架之類的可以考慮使用多型來隱藏細節,一般的應用不要使用。
C 語言物件導向程式設計 繼承
繼承就是基於乙個已有的類 一般稱作父類或基類 再去重新宣告或建立乙個新的類,這個類可以稱為子類或派生類。子類或派生類可以訪問父類的資料和函式,然後子類裡面又新增了自己的屬性和資料。在 c 語言裡面,可以通過結構體巢狀的方式去實現類的單繼承 暫不考慮多重繼承 但有一點注意事項,就是在結構體巢狀時,父類...
c 物件導向程式設計 物件導向
什麼是物件導向 是對現實世界理解和抽象的方法。物件導向程式設計的特點 易維護,易擴充套件,靈活性好,重用 類 對事物的抽象定義,即事物具有的共同特徵和行為。物件 即對類進行例項 類是抽象的,物件是具體的 類的定義 語法 訪問修飾符 class 類名類的成員 包括字段,屬性,方法,常量,事件和索引器等...
程式語言 C 中的物件導向
最近終於學到了c 了,真的很高興啊!畢竟是要從面向過程跳到物件導向的時候了,雖然以前學過c 但是還是覺得自己的物件導向的思想很不做,希望通過對c 的學習能夠打下乙個堅實的基礎,然後在這個基礎上再學習其他新的物件導向語言就不是事兒了。一 介紹篇 c 是一種安全的 穩定的 簡單的 優雅的,由c和c 衍生...