c (3)遠征之封裝篇(下)

2021-09-10 06:27:54 字數 1079 閱讀 9014

1、例項化物件陣列

class coordinate

public:

int m_ix;

int m_iy;

int mian(void)

coordinate coord[3]

coord[1].m_ix=10;

coordinate*p=new coordinate[3];

p[0].m_iy=20; p->m_ix=20;

deletep;

p=null;

return  0;

2、物件成員

class coordinate

public:

coordinate();

private:

int m_ix;

int m_iy;

class line

public:

line();

private:

coordinate m_coora;

coordinate m_coorb;

int main(void)

line *p=new line();

delete p;

p=null;

return 0;

結論:先例項化點,再例項化線

所以先銷毀線再銷毀點

座標類的建構函式需要有引數

class coordinate

public:

coordinate(int x,int y);

private:

int m_ix;

int m_iy;

class line

public:

line(int x1,int y1,int x2,int y2);

private:

coordinate m_coora;

coordinate m_coorb;

初始化列表

line::line (int x1,int y1,int x2,int y2):

m_coora(x1,y1),m_coorb(x2,y2)

cout<<"line"<

C 遠征之封裝篇(下)

2 2 物件陣列實踐 檔案 class coordinate 檔案 include include coordinate.h using namespace std coordinate coordinate for int j 0 j 3 j p delete p p null system pa...

c 遠征之封裝篇(拷貝建構函式)

拷貝建構函式 示例安排 1.定義乙個array類,資料成員為m icount,成員函式包括資料封裝 建構函式 拷貝建構函式和析構函式,通過此示例體會淺拷貝原理。2.增加資料成員m parr,並增加m parr位址檢視函式,同時改造建構函式 拷貝建構函式和析構函式,體會拷貝的原理和必要性。1.淺拷貝建...

mro之C3演算法

c3演算法 歸併演算法 class a object pass class b a pass class c a pass class d b pass class e c pass class f d,e pass print f.mro 第一步先找到繼承的父類的mro d d,b,a,o e e...