freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

7133程序設(shè)計實習(xí)(編輯修改稿)

2025-11-05 09:39 本頁面
 

【文章內(nèi)容簡介】 { //添加一件武器 if( nWeaponIdNum amp。amp。 nWeaponIdNum % 20 == 0 ) { //滿 20件就重新分配空間 int * pTmp = new int[nWeaponIdNum+20]。 memcpy( pTmp,pWeapons, nWeaponIdNum * sizeof(int))。 delete [] pWeapons。 pWeapons = pTmp。 } pWeapons[nWeaponIdNum++] = nWeaponId。 } 構(gòu)造函數(shù)和析構(gòu)函數(shù)什么時候被調(diào)用? chunk ck1,ck2。 // main 執(zhí)行前即調(diào)用構(gòu)造函數(shù)void MyFunction() { static chunk ck。 //函數(shù)執(zhí)行前調(diào)用構(gòu)造函數(shù) chunk tmpCk。 // tmpCk 構(gòu)造函數(shù)調(diào)用 ....//函數(shù)退出時 tmpCk消亡, 調(diào)用析構(gòu)函數(shù) } main () { chunk tmpCk2。 chunk * pCk。 MyFunction ()。 pCk = new chunk。 // 構(gòu)造函數(shù)被調(diào)用 .... delete pCk 。 // 析 構(gòu)函數(shù)被調(diào)用 } //main函數(shù)退出時 tmpCk2消亡, 調(diào)用析構(gòu)函數(shù)。 程序結(jié)束前, ck ,ck1, ck2 的析構(gòu)函數(shù)被調(diào)用 class Demo { int id。 public: Demo(int i) { id = i。 printf( id=%d,Construct\n,id)。 } ~Demo() { printf( id=%d,Destruct\n,id)。 } }。 Demo d1(1)。 void fun() { static Demo d2(2)。 Demo d3(3)。 printf( fun \n)。 } void main () { Demo d4(4)。 printf( main \n)。 { Demo d5(5)。 } fun()。 printf( endmain \n)。 } 輸出: id=1,Construct id=4,Construct main id=5,Construct id=5,Destruct id=2,Construct id=3,Construct fun id=3,Destruct endmain id=4,Destruct id=2,Destruct id=1,Destruct 關(guān)于復(fù)制構(gòu)造函數(shù)和析構(gòu)函數(shù)的一個例子 include class CMyclass { public: CMyclass() {}。 CMyclass( CMyclass amp。 c) { cout copy constructor endl。 } ~CMyclass() { cout destructor endl。 } }。 void fun(CMyclass obj_ ) { cout fun endl。 } CMyclass c。 CMyclass Test( ) { cout test endl。 return c。 } void main(){ CMyclass c1。 fun( c1)。 Test()。 } 運行結(jié)果: copy constructor fun destructor //參數(shù)消亡 test copy constructor destructor // 返回值臨時對象消亡 destructor // 局部變量消亡 destructor // 全局變量消亡 /// 標題:五子棋作業(yè) _學(xué)號 _姓名 復(fù)制構(gòu)造函數(shù)和靜態(tài)變量的一個例子 class Apple { private : int nWeight。 static int nTotalWeight。 static int nTotalNumber。 public: Apple( int w) 。 ~Apple( ) 。 static void PrintTotal()。 }。 Apple::Apple( int w) { nWeight = w。 nTotalWeight += w。 nTotalNumber ++。 } Apple::~Apple() { nTotalWeight = nWeight。 nTotalNumber 。 } void Apple::PrintTotal () { printf( “%d,%d”, nTotalWeight, nTotalNumber)。 } int Apple::nTotalWeight = 0。 int Apple::nTotalNumber = 0。 int main () { Apple p1(3), p2( 5)。 Apple::nTotalWeight = 6。 // Wrong , 私有 Apple::PrintTotal()。 ()。 } 上面 Apple類的不足之處:在使用 Apple類的過程中,有時會調(diào)用復(fù)制構(gòu)造函數(shù)生成臨時的隱藏的 Apple對象 (比如,調(diào)用一個以 Apple類對象作返回值的函數(shù)時)那么臨時對象在消亡時會調(diào)用析構(gòu)函數(shù),減少nTotalNumber 和 nTotalWeight的值,可是這些臨時對象在生成時卻沒有增加 nTotalNumber 和 nTotalWeight的值。 因此要為 Apple類寫一個復(fù)制構(gòu)造函數(shù) Apple::Apple( Apple amp。 a ) { nWeight = 。 nTotalWeight += 。 nTotalNumber ++。 } 對象的賦值 缺省的情況下,對象間的賦值執(zhí)行的是按位拷貝 CHero c1,c2。 c1 = c2。 // c1的所有成員變量變成和 c2一樣 如果賦值運算符被重載,那么情況就會不一樣了,以后會介紹 構(gòu)造函數(shù)和析構(gòu)函數(shù)小結(jié) ? 構(gòu)造函數(shù) ? 定義 ? 調(diào)用方式 (隱式 ) ? 復(fù)制構(gòu)造函數(shù) ? 析造函數(shù) – 定義 – 調(diào)用時機 ? 各種構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用時機 構(gòu)造函數(shù)(調(diào)用方式) ? 定義對象變量時: CHero c1, c2(2),c3(3,5)。 創(chuàng)建新變量對象時: CHero * pc1 = new CHero 。 CHero * pc2 = new CHero (3,4); ? 創(chuàng)建數(shù)組對象時 : Test array1[3] = { 1, Test(1,2) }。 Test * pArray[3] = { new Test( 4), new Test(1
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1