第8講構(gòu)造函數(shù)和析構(gòu)_第1頁
第8講構(gòu)造函數(shù)和析構(gòu)_第2頁
第8講構(gòu)造函數(shù)和析構(gòu)_第3頁
第8講構(gòu)造函數(shù)和析構(gòu)_第4頁
第8講構(gòu)造函數(shù)和析構(gòu)_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第8講構(gòu)造函數(shù)和析構(gòu)函數(shù)教學(xué)目的與要求:

了解類對象的創(chuàng)建和撤消過程。掌握構(gòu)造函數(shù)和析構(gòu)函數(shù)的定義和使用。教學(xué)內(nèi)容提要:

1、構(gòu)造函數(shù);

2、析構(gòu)函數(shù);教學(xué)重點:構(gòu)造函數(shù)和析構(gòu)函數(shù)的定義和使用。教學(xué)難點:構(gòu)造函數(shù)的定義和使用。教學(xué)進度:P61~P70教學(xué)過程:在聲明對象時,也可以對其數(shù)據(jù)成員初始化。對象數(shù)據(jù)成員的初始化需要通過類的特殊成員函數(shù)來進行,這個成員函數(shù)就是構(gòu)造函數(shù)。構(gòu)造函數(shù)是類的特殊成員函數(shù),用于在聲明類的對象時對其進行初始化。構(gòu)造函數(shù)的函數(shù)名應(yīng)與類名相同,構(gòu)造函數(shù)中的參數(shù)根據(jù)需要可有可無。不能為其指定返回值類型,甚至連void也不可以。構(gòu)造函數(shù)應(yīng)被聲明為公有函數(shù),它只能在創(chuàng)建類的對象時由系統(tǒng)自動調(diào)用,對對象進行初始化,程序的其他部分不能調(diào)用。classCylinder{public:Cylinder();voidsetcylinder(doubler,doubleh);例8-1圓柱體類構(gòu)造函數(shù)不帶參數(shù)的情形【

8.1構(gòu)造函數(shù)】doublegetradius();doublegetheight();doublevolume();doublesurface_area();private:doubleradius;doubleheight;};Cylinder::Cylinder(){radius=5.0;height=10.0;}classCylinder{public:Cylinder(doubler,doubleh);voidsetcylinder(doubler,doubleh);圓柱體類構(gòu)造函數(shù)帶有參數(shù)的情形例8-2doublegetradius();doublegetheight();doublevolume();doublesurface_area();private:doubleradius;doubleheight;};Cylinder::Cylinder(doubler,doubleh){radius=r;height=h;}在聲明對象時,就必須利用實參對對象進行初始化。例如:Cylindercylinder1(2.0,3.0); Cylindercylinder1;//錯誤,沒有提供用于初始化的實參在聲明對象時,如果相應(yīng)的構(gòu)造函數(shù)沒有形參,括號應(yīng)該省略。(續(xù))若是聲明對象數(shù)組,應(yīng)該采用下述形式:Cylindercylinder1[3]={Cylinder(1.0,2.0),Cylinder(3.0,4.0),Cylinder(5.0,6.0)};構(gòu)造函數(shù)也可以定義為內(nèi)聯(lián)函數(shù),可以帶默認形參值,也可以重載。//這個程序在【例3-9】的基礎(chǔ)上增加了構(gòu)造函數(shù)//EXAMPLE.H#include<iosteam.h>classCylinder{public:Cylinder(){}//構(gòu)造函數(shù)

Cylinder(doubler,doubleh);//構(gòu)造函數(shù)

voidsetcylinder(doubler,doubleh);doublegetradius(){returnradius;}構(gòu)造函數(shù)的重載第1部分說明:對類Cylinder的構(gòu)造函數(shù)進行了重載。沒有形參,函數(shù)體也為空。有兩個形參。例8-3doublegetheight(){returnheight;}doublevolume();doublesurface_area();private:doubleradius;doubleheight;};//EXAMPLE.CPP#include″EXAMPLE.h″constdoublePI=3.1415926;Cylinder::Cylinder(doubler,doubleh){radius=r;height=h;}第1部分第2部分(續(xù))voidCylinder::setcylinder(doubler,doubleh){radius=r;height=h;}doubleCylinder::volume(){doublevol;vol=PI*radius*radius*height;returnvol;}doubleCylinder::surface_area(){doublearea;area=2*PI*radius*height+2*PI*radius*radius;returnarea;}voidmain(){Cylindercylinder1(5.0,10.0),cylinder2;//聲明對象cylinder2.setcylinder(10.2,15.6);第2部分帶有兩個實參,系統(tǒng)將自動調(diào)用第2個構(gòu)造函數(shù)給對象cylinder1初始化。在聲明時調(diào)用第一個構(gòu)造函數(shù)。(續(xù))注

自動生成的默認構(gòu)造函數(shù)不帶任何參數(shù),在函數(shù)體內(nèi)也沒有任何語句,不執(zhí)行任何操作。

cout<<″Theradiusofthefirstcylinderis:\t″<<cylinder1.getradius()<<endl;cout<<″Theheightofthefirstcylinderis:\t″<<cylinder1.getheight()<<endl;cout<<″Thevolumeofthefirstcylinderis:\t″<<cylinder1.volume()<<endl;cout<<″Thesurfaceareaofthefirstcylinderis:\t″<<cylinder1.surface_area()<<endl;cout<<″Theradiusofthesecondcylinderis:\t″<<cylinder2.getradius()<<endl;cout<<″Theheightofthesecondcylinderis:\t″<<cylinder2.getheight()<<endl;cout<<″Thevolumeofthesecondcylinderis:\t″<<cylinder2.volume()<<endl;cout<<″Thesurfaceareaofthesecondcylinderis:\t″<<cylinder2.surface_area()<<endl;}第2部分系統(tǒng)在創(chuàng)建每一個對象時都需要自動調(diào)用相應(yīng)的構(gòu)造函數(shù)。其形式如下:類名::類名()

{}(續(xù))

//EXAMPLE3_13.H#include<iostream.h>classA{public:A(){cout<<″A()iscalled″<<endl;}A(intx,inty);private:inta,b;};A::A(intx,inty){a=x;b=y;cout<<″A(intx,inty)iscalled″<<endl;cout<<″A=″<<a<<′\t′<<″B=″<<b<<endl;}構(gòu)造函數(shù)的調(diào)用例8-4A()iscalledA(intx,inty)iscalledA=10B=10//EXAMPLE3_13.CPP#include″EXAMPLE3_13.H″voidmain(){Aa;Ab(10,10);}C++類中還有一個特殊的成員函數(shù),即析構(gòu)函數(shù)。析構(gòu)函數(shù)在對象終止時由系統(tǒng)自動調(diào)用,以釋放分配給對象的內(nèi)存。析構(gòu)函數(shù)的函數(shù)名應(yīng)為類名前加“~”,析構(gòu)函數(shù)中沒有參數(shù),也不能為之指定返回值類型。注一個類內(nèi)只能聲明一個析構(gòu)函數(shù)。它也是公有的成員函數(shù)。如果類中沒有聲明,編譯器也會自動生成一個帶空函數(shù)體的析構(gòu)函數(shù)。【

8.2析構(gòu)函數(shù)】析構(gòu)函數(shù)的執(zhí)行過程//EXAMPLE3_14.H#include<iostream.h>classA{public:A(){cout<<″A()iscalled″<<endl;}

~A(){cout<<″~A()iscalled″<<endl;}};//EXAMPLE3_14.CPP#include″EXAMPLE3_14.H″voidmain(){Aa;cout<<″theprogramwillend″<<endl;}A()iscalledTheprogramwiilend~A()iscalled例8-5只有在定義構(gòu)造函數(shù)時,才可以帶有成員初始化列表。如果僅僅是聲明構(gòu)造函數(shù)原型,則不能帶成員初始化列表。成員初始化列表可用于初始化類中的普通數(shù)據(jù)成員。甚至還是必須的(如后面將介紹的常數(shù)據(jù)成員等)。classA{public:A(intx):a(x){cout<<″classAconstructing\t\t″<<a<<endl;}

~A(){cout<<″classAdestructing″<<endl;}private:inta;};例:8-6課堂練習(xí)1#include<iostream.h>classstudent{intsage;public:student();student(int);intGetAge(){returnsage;}~student(){cout<<“Destructorcalled”<<endl;}};student::student(){cout<<”DefaultConstructorcalled.\n”;sage=0;}student::student(intage){cout<<”Constructorcalled.\n”;sage=age;}voidmain(){studenttom,jack(15);cout<<”Tomis“<<tom.GetAge()<<”yearsold.”<<endl;cout<<”Jackis“<jack.GetAge()<<”yearsold.”<<endl;}DefaultConstructorcalled.

Constructorcalled.Tomis0yearsold.Jackis15yearsoldDestructorcalledDestructorcalled課堂練習(xí)2#include<iostream.h>#include<string.h>classTdate{public:Tdate(intm,intd,inty);~Tdate(){cout<<”TdateDestructorcalled\n”;}protected:intmoth,day,year;};Tdate::Tdate(intm,intd,inty)

{month=m;day=d;year=y;cout<<”Tdateconstructorcalle

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論