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

正文內(nèi)容

[工學(xué)]四川大學(xué)c第十一章構(gòu)造函數(shù)和析構(gòu)函數(shù)(編輯修改稿)

2024-11-14 23:51 本頁面
 

【文章內(nèi)容簡(jiǎn)介】 ass Xyz 類中定義了一個(gè)構(gòu)造函數(shù)和一個(gè)析構(gòu)函數(shù)。 class Xyz { public: Xyz() //constructor definition { name=new char[20]。 } ~Xyz() //destructor definition { delete name。 } protected: char *name。 } 該類定義的構(gòu)造函數(shù)在對(duì)象之外分配一段堆內(nèi)存空間,撤銷時(shí),由析構(gòu)函數(shù)收回堆內(nèi)存。 注意,析構(gòu)函數(shù)以調(diào)用構(gòu)造函數(shù)相反的順序被調(diào)用。 例如: 在 程序中為每個(gè)類增加析構(gòu)函數(shù)則有: //…ch11 include class Student { public: Student() { cout“Constructing student.\n”。 semeshours=100。 gpa=。 } ~Studeng() { cout “Destructing student.\n”。 } protected: int semeshours。 float gpa。 }。 class Teacher { public: Teacher() { cout“Constructing teacher.\n”。 } ~Teacher() { cout “Destructing teacher.\n”。 } }。 class Tutorpair { public: Tutorpair() { cout“Constructing tutorpair.\n”。 nomeetings=0。 } ~Tutorpair() { cout“Destructing tutorpair.\n”。 } protected: Student student。 Teacher teacher。 int nomeetings。 }。 void main() { Tutorpair tp。 cout“Back in main.\n”。 } Constructing student. Constructing teacher. Constructing tutorpair. Back in main. Destructing tutorpair. Destructing teacher. Destructing student. 運(yùn)行結(jié)果: 不帶參數(shù)的構(gòu)造函數(shù)不能完全滿足初始化的要求,因?yàn)檫@樣創(chuàng)建的類對(duì)象具有相同的初始化值。 如果需要對(duì)類對(duì)象按不同特征初始化不同的值,應(yīng)采用帶參數(shù)的構(gòu)造函數(shù)。如下面程序所示: // class Tdate1 { public: Tdate1(int y,int m,int d)。 ~Tdate1()。 void print()。 167。 帶參數(shù)的構(gòu)造函數(shù) private: int year,month,day。 }。 Tdate1::Tdate1(int y,int m,int d) { year=y。 month=m。 day=d。 cout“Constructor called.\n”。 } Tdate1::~Tdate1() { cout“Destructor called.\n”。 } void Tdate1::print() { coutyear“.”month“.”dayendl。 } include include “” void main() { Tdate1 today(2021,5,1),tomorrow(2021,5,2)。 cout“Today is ”。 ()。 cout“Tomorrow is ”。 ()。 } 結(jié)果: Constructor called. Constructor called. Today is Tomorrow is Destructor called. Destructor called. 注意 : 構(gòu)造函數(shù)可采用以下兩種方式將值賦給其成員。 在構(gòu)造函數(shù)體內(nèi)進(jìn)行成員變量的賦值, 如前例中所示,又如: class X { int a,b。 //default private public: X(int i,int j) { a=i。 b=j。 //value assigned within constructor } }。 使用函數(shù)體前的初始值表 , 例如: class X { int a,b。 public: X(int i,int j):a(i),b(j) { } }。 下面是使用構(gòu)造函數(shù)創(chuàng)建對(duì)象,使用析構(gòu)函數(shù)撤銷對(duì)象的另一個(gè)例子。 //…ch11 include include class Student {
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號(hào)-1