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

正文內(nèi)容

[工學(xué)]四川大學(xué)c++第十一章構(gòu)造函數(shù)和析構(gòu)函數(shù)-文庫(kù)吧

2025-09-19 23:51 本頁(yè)面


【正文】 width=5。 length=5。 } protected: int weight。 int high。 int width。 int length。 }。 void fn() { Desk da。 //constructor call //…… } 構(gòu)造函數(shù)可以在類(lèi)體外定義,例如: include class Desk { public: Desk()。 protected: int weight。 int high。 int width。 int length。 }。 Desk::Desk() //constructor definition { weight=10。 high=5。 width=5。 length=5。 coutweight“ ”high“ ”width“ ”lengthendl。 }。 void fn() { Desk da。 } void main() { fn()。 } 結(jié)果: 10 5 5 5 10 5 5 5 …… da 對(duì)象的內(nèi)存空間分配及初始化 weight high width length da 若將 fn() 函數(shù)改為 void fn() { Desk dd[5]。 } 則執(zhí)行定義數(shù)組語(yǔ)句時(shí),構(gòu)造 函數(shù)將被調(diào)用 5 次,即對(duì)每個(gè) 數(shù)組成員對(duì)象的創(chuàng)建都要調(diào)用 一次構(gòu)造函數(shù)。 10 5 5 5 …… 10 5 5 5 …… weight high width length weight high width length dd[0] dd[4] …… dd[5] 對(duì)象的內(nèi)存空間分配及初始化 輸出結(jié)果為: 10 5 5 5 10 5 5 5 10 5 5 5 10 5 5 5 10 5 5 5 注意: 類(lèi)體外定義構(gòu)造函數(shù),其函數(shù)名前要加上“類(lèi)名 ::”; 構(gòu)造函數(shù)無(wú)返回類(lèi)型; 在定義時(shí),若類(lèi)的數(shù)據(jù)成員是另一個(gè)類(lèi)的對(duì)象,則在調(diào)用構(gòu)造函數(shù)創(chuàng)建對(duì)象時(shí),對(duì)作為數(shù)據(jù)成員的對(duì)象先要自動(dòng)調(diào)用其自身的構(gòu)造函數(shù)。 下面程序的 Tutorpair類(lèi) (“幫教派對(duì)” )中,其成員包含有學(xué)生類(lèi)對(duì)象和老師類(lèi)對(duì)象。程序說(shuō)明了調(diào)用構(gòu)造函數(shù)的方法。 //…ch11 include class Student { public: Student() { cout“Constructing student.\n”。 semeshours=100。 gpa=。 } //…… protected: int semeshours。 float gpa。 }。 class Teacher { public: Teacher() { cout“Constructing teacher.\n”。 } }。 class Tutorpair { public: Tutorpair() { cout“Constructing tutorpair.\n”。 nomeetings=0。 } protected: Student student。 //class object as member data Teacher teacher。 //class object as member data int nomeetings。 }。 void main() { Tutorpair tp。 cout“Back in main.\n”。 } 運(yùn)行結(jié)果: Constructing student. Constructing teacher. Constructing Tutorpair. Back in main. 當(dāng)調(diào)用構(gòu)造函數(shù) Tutorpair() 創(chuàng)建的對(duì)象 tp 時(shí),首先根據(jù)對(duì)象成員被說(shuō)明的次序,依次調(diào)用 Student() 構(gòu)造函數(shù)和 Teacher() 構(gòu)造函數(shù)創(chuàng)建成員對(duì)象 Student 和Teacher, 然后執(zhí)行它自己的構(gòu)造函數(shù)的函數(shù)體,如運(yùn)行結(jié)果所示。 The constructor is used to initialize objects, and destructor is used to clean up objects and release resources before they are bee unusable. 例如在下面情況下需要使用析構(gòu)函數(shù):一個(gè)對(duì)象當(dāng)結(jié)束其生命期時(shí),比如在函數(shù)體內(nèi)定義的對(duì)象,當(dāng)該函數(shù)調(diào)用結(jié)束時(shí),局部對(duì)象被釋放。 構(gòu)造函數(shù)打開(kāi)一個(gè)文件,使用完文件時(shí),需要關(guān)閉文件。 從堆中分配了動(dòng)態(tài)內(nèi)存區(qū),在對(duì)象消失之前必須釋放。 167。 析構(gòu)函數(shù) 析構(gòu)函數(shù)的特點(diǎn): 無(wú)返回類(lèi)型; 無(wú)參數(shù); 不能隨意調(diào)用; 不能重載。 而構(gòu)造函數(shù)可以有參數(shù),也可以重載。 析構(gòu)函數(shù)與構(gòu)造函數(shù)的功能相對(duì)應(yīng),所以析構(gòu)函數(shù)名是構(gòu)造函數(shù)名前加一個(gè)邏輯反運(yùn)算符“ ~” 例如: 下面程序段在 cl
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1