第3章(類與對象)_第1頁
第3章(類與對象)_第2頁
第3章(類與對象)_第3頁
第3章(類與對象)_第4頁
第3章(類與對象)_第5頁
已閱讀5頁,還剩118頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、c+中的類中的類n類是具有相同屬性和行為的一組對象的集合,它為屬于該類的全部類是具有相同屬性和行為的一組對象的集合,它為屬于該類的全部對象提供了統(tǒng)一的抽象描述,其內(nèi)部包括對象提供了統(tǒng)一的抽象描述,其內(nèi)部包括屬性和行為屬性和行為兩個主兩個主要部分。要部分。n利用類可以實(shí)現(xiàn)數(shù)據(jù)的封裝、隱藏、繼承與派生。利用類可以實(shí)現(xiàn)數(shù)據(jù)的封裝、隱藏、繼承與派生。n利用類易于編寫大型復(fù)雜程序,其模塊化程度比利用類易于編寫大型復(fù)雜程序,其模塊化程度比C中采用函數(shù)更高。中采用函數(shù)更高。31類類函數(shù)是將邏輯上相關(guān)的語句與數(shù)據(jù)封裝,用于函數(shù)是將邏輯上相關(guān)的語句與數(shù)據(jù)封裝,用于完成特定的功能。完成特定的功能。而類則是邏輯上相

2、關(guān)的函數(shù)與數(shù)據(jù)的封裝,它而類則是邏輯上相關(guān)的函數(shù)與數(shù)據(jù)的封裝,它是對所要處理的問題的描述。是對所要處理的問題的描述。31類的定義類的定義 類是一種用戶自定義類型,聲明形式:類是一種用戶自定義類型,聲明形式:class 類名稱類名稱 public: 公有成員(外部接口)公有成員(外部接口) private: 私有成員私有成員 protected: 保護(hù)型成員保護(hù)型成員公有類型成員公有類型成員在關(guān)鍵字在關(guān)鍵字public后面聲明,它們是類與外部的后面聲明,它們是類與外部的接口,任何外部函數(shù)都可以訪問公有類型數(shù)據(jù)和函接口,任何外部函數(shù)都可以訪問公有類型數(shù)據(jù)和函數(shù)。數(shù)。私有類型成員私有類型成員在關(guān)鍵字

3、在關(guān)鍵字private后面聲明,只允許本類中的函后面聲明,只允許本類中的函數(shù)訪問,而類外部的任何函數(shù)都不能訪問。數(shù)訪問,而類外部的任何函數(shù)都不能訪問。如果緊跟在類名稱的后面聲明私有成員,則關(guān)鍵字如果緊跟在類名稱的后面聲明私有成員,則關(guān)鍵字privateprivate可以省略??梢允÷?。保護(hù)類型保護(hù)類型與與private類似,其差別表現(xiàn)在繼承與派生時對類似,其差別表現(xiàn)在繼承與派生時對派生類的影響不同,第派生類的影響不同,第5章講。章講。類的成員類的成員class Clock public: void SetTime(int NewH, int NewM, int NewS); void Show

4、Time( ); private: int Hour, Minute, Second;/不允許初始化不允許初始化;成員數(shù)據(jù)成員數(shù)據(jù)成員函數(shù)成員函數(shù)void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS;void Clock : ShowTime( ) coutHour:Minute:Second;成員數(shù)據(jù)成員數(shù)據(jù)n與一般的變量聲明相同,但需要將它放與一般的變量聲明相同,但需要將它放在類的聲明體中在類的聲明體中。class complexprivate:double real;d

5、ouble imag;public:void init (double r,double i)real=r; imag=i; double realcomplex()return real;double imagcomplex()return imag;double abscomplex() double t;t=real*real+imag*imag;return sqrt(t); ;結(jié)構(gòu)與類結(jié)構(gòu)與類 P39結(jié)構(gòu)的擴(kuò)充:結(jié)構(gòu)的擴(kuò)充:struct Savingsunsigned accountNumber;float balance;void fn()Savings a;Savings b;a

6、. accountNumber=1;b. accountNumber=2;結(jié)構(gòu)與類的區(qū)別區(qū)別結(jié)構(gòu)與類的區(qū)別區(qū)別:類中成員的缺省存儲屬性類中成員的缺省存儲屬性為私有的為私有的結(jié)構(gòu)體中的缺省存儲屬性結(jié)構(gòu)體中的缺省存儲屬性為共有的為共有的.3.1.2成員函數(shù)的定義成員函數(shù)的定義n1. 外聯(lián)成員函數(shù)(外聯(lián)函數(shù))外聯(lián)成員函數(shù)(外聯(lián)函數(shù))n例如:例如:void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS;n全局函數(shù):全局函數(shù):n#includenusing namespace std;n

7、class student;nvoid add(student & ,int );nvoid add(student & ,int ,int );nclass studentnnprivate:n int math;n int data;npublic:n void output();n friend void add(student & ,int );n friend void add(student & ,int ,int );nvoid add(student &s,int MATH)nn s.math = MATH;nnvoid add(stud

8、ent &s,int MATH,int DATA)nn s.math = MATH;n s.data = DATA;nnvoid student:output()nn coutmath:mathendl;n cout“data:”dataendl;nint main()nn student s1,s2;n add(s1,90);n add(s2,90,91);n s1.output();n s2.output();n return 0;n3.1.2成員函數(shù)的定義成員函數(shù)的定義n2. 內(nèi)聯(lián)成員函數(shù)(內(nèi)聯(lián)函數(shù)、內(nèi)部函數(shù)、內(nèi)內(nèi)聯(lián)成員函數(shù)(內(nèi)聯(lián)函數(shù)、內(nèi)部函數(shù)、內(nèi)置函數(shù))置函數(shù))n在類中說明原形

9、,可以在類外給出函數(shù)體實(shí)在類中說明原形,可以在類外給出函數(shù)體實(shí)現(xiàn),并在函數(shù)名前使用類名加以限定。也可現(xiàn),并在函數(shù)名前使用類名加以限定。也可以直接以直接在類中給出函數(shù)體在類中給出函數(shù)體,形成,形成內(nèi)聯(lián)成員函內(nèi)聯(lián)成員函數(shù)數(shù)。nP41(1)在類定義體內(nèi)定義內(nèi)聯(lián)成員函數(shù))在類定義體內(nèi)定義內(nèi)聯(lián)成員函數(shù)(隱式聲明隱式聲明)n(2)使用關(guān)鍵字)使用關(guān)鍵字inline定義內(nèi)聯(lián)成員函數(shù)定義內(nèi)聯(lián)成員函數(shù)(顯示聲明顯示聲明)內(nèi)聯(lián)成員函數(shù)舉例內(nèi)聯(lián)成員函數(shù)舉例(隱式隱式)class Point public: void Init(int initX,int initY) X=initX; Y=initY; int Ge

10、tX( ) return X; int GetY( ) return Y; private: int X,Y;內(nèi)聯(lián)成員函數(shù)舉例內(nèi)聯(lián)成員函數(shù)舉例(顯式顯式)class Point public: void Init(int initX,int initY); int GetX( );); int GetY( );); private: int X,Y;inline void Point: Init(int initX,int initY) X=initX; Y=initY;inline int Point:GetX( ) return X;inline int Point:GetY( ) ret

11、urn Y;n3.C程序的多文件結(jié)構(gòu)程序的多文件結(jié)構(gòu)n三個文件來保存:三個文件來保存:n(1)類的定義:)類的定義:.hn(2)類的實(shí)現(xiàn):)類的實(shí)現(xiàn):*.cppn(3)類的使用:)類的使用:.cppn舉例:舉例:P42例例3.12對象對象n類的對象是該類的某一特定實(shí)體,即類類型的類的對象是該類的某一特定實(shí)體,即類類型的變量。變量。n聲明形式:聲明形式: 類名類名 對象名;對象名;n例:例: Clock myClock;類中成員的訪問方式類中成員的訪問方式n類中成員互訪類中成員互訪u直接使用成員名直接使用成員名n類外訪問類外訪問u使用使用“對象名對象名.成員名成員名”方式訪問方式訪問 publi

12、c 屬性的屬性的成員成員例例: 類的應(yīng)用舉例類的應(yīng)用舉例#includeclass Clock ./類的聲明類的聲明 public: void SetTime(int NewH, int NewM, int NewS); void ShowTime( ); private: int Hour, Minute, Second;void Clock : SetTime(int NewH, int NewM, int NewS) Hour=NewH; Minute=NewM; Second=NewS;void Clock : ShowTime( ) coutHour:Minute:month=mon

13、th;this-day=day;this-year=year;n帶缺?。J(rèn))形參值的成員函數(shù)和重載成帶缺?。J(rèn))形參值的成員函數(shù)和重載成員函數(shù)員函數(shù)帶缺省形參值的函數(shù)帶缺省形參值的函數(shù)nVoid clock:SetTime(int NewH=0,int NewM=0,int NewS=0)nnHour=NewH;nMinute=NewM;nSecond=NewS;nn例例3.4P48 帶默認(rèn)參數(shù)的成員函數(shù)帶默認(rèn)參數(shù)的成員函數(shù)n例例3.5P49 重載成員函數(shù)舉例重載成員函數(shù)舉例n構(gòu)造函數(shù)的作用是在對象被創(chuàng)建時使用特定的值構(gòu)造對象,或者構(gòu)造函數(shù)的作用是在對象被創(chuàng)建時使用特定的值構(gòu)造對象,或者說

14、將對象說將對象初始化初始化為一個特定的狀態(tài)。為一個特定的狀態(tài)。n在對象創(chuàng)建時在對象創(chuàng)建時由系統(tǒng)自動調(diào)用由系統(tǒng)自動調(diào)用。n如果程序中未聲明,則系統(tǒng)自動產(chǎn)生出一個如果程序中未聲明,則系統(tǒng)自動產(chǎn)生出一個缺省形式缺省形式的構(gòu)造函數(shù)的構(gòu)造函數(shù)n允許為允許為內(nèi)聯(lián)內(nèi)聯(lián)函數(shù)、函數(shù)、重載重載函數(shù)、函數(shù)、帶缺省形參值帶缺省形參值的函數(shù)的函數(shù)3.3 構(gòu)造函數(shù)與析構(gòu)函數(shù)構(gòu)造函數(shù)與析構(gòu)函數(shù)3.3.1 構(gòu)造函數(shù)構(gòu)造函數(shù)-由于類的封裝性由于類的封裝性, ,不能象普通變量一樣初始化不能象普通變量一樣初始化struct Savingsunsigned accountNumber; float balance;Savings A

15、=1, 2000.0;Savings B(2,3000.0);構(gòu)造函數(shù)舉例構(gòu)造函數(shù)舉例:P50 例例3.6 略略class Clockpublic:Clock (int NewH, int NewM, int NewS);/構(gòu)造函數(shù)構(gòu)造函數(shù)void SetTime(int NewH, int NewM, int NewS);void ShowTime( );private:int Hour,Minute,Second;構(gòu)造函數(shù)的實(shí)現(xiàn):構(gòu)造函數(shù)的實(shí)現(xiàn):Clock:Clock(int NewH, int NewM, int NewS)Hour=H;Minute=M;Second=S;建立對象時構(gòu)造

16、函數(shù)的作用:建立對象時構(gòu)造函數(shù)的作用:void main( ) Clock c (0,0,0); /隱含調(diào)用構(gòu)造函數(shù),將初始值作為實(shí)參隱含調(diào)用構(gòu)造函數(shù),將初始值作為實(shí)參。 c.ShowTime( );/ ex10_2.h#include class Demo intx,y;public:Demo(int a, int b) x=a;y=b; coutConstructor demo(int,int) be calledn;Demo( )coutConstructor demo() be alledn; void show() coutX=xtY=yendl; ;重載構(gòu)造函數(shù):重載構(gòu)造函數(shù):P5

17、1 例例3.7 略略#include “ex10_2.h”void main()Demo d1(3,5); /Ad1.show();Demo d2; /Bd2.show(); 該程序的輸出為:該程序的輸出為:Constructor demo(int,int) be calledX=3 Y=5Constructor demo() be alledX=-858993460 Y=-858993460 帶默認(rèn)參數(shù)的構(gòu)造函數(shù):帶默認(rèn)參數(shù)的構(gòu)造函數(shù):P52例例3.8 略略note: 必須保證函數(shù)形式參數(shù)默認(rèn)后,函數(shù)形式不必須保證函數(shù)形式參數(shù)默認(rèn)后,函數(shù)形式不能與其他構(gòu)造函數(shù)完全相同,否則系統(tǒng)調(diào)用時會出能與

18、其他構(gòu)造函數(shù)完全相同,否則系統(tǒng)調(diào)用時會出現(xiàn)二義性。現(xiàn)二義性。如:如:#includeclass Pointint xVal,yVal;public:Point(int x=0,int y=0)xVal=x;yVal=y;Point()xVal=2;yVal=3;void print();void Point:print()coutxValendl;coutyValprint();/或者:下面這樣寫,就會error Point *p2=new Point();p2-print();正確:警告錯誤都不會有。正確:警告錯誤都不會有。#includeclass Pointint xVal,yVal;p

19、ublic:Point(int x=0)xVal=x;yVal=0;Point(int x,int y)xVal=x;yVal=y;void print();void Point:print()coutxValendl;coutyValprint();默認(rèn)構(gòu)造函數(shù):默認(rèn)構(gòu)造函數(shù):P53例例3.9 略略對象數(shù)組:對象數(shù)組: P54例例3.10 略略 P55例例3.11 略略4. 拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),其形參為本類的拷貝構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),其形參為本類的對象引用。對象引用。作用:使用一個對象(參數(shù)指定的對象),去初始化一個正在被建立的同類作用:使用一個

20、對象(參數(shù)指定的對象),去初始化一個正在被建立的同類型對象型對象class 類名類名 public : 類名(形參);類名(形參);/構(gòu)造函數(shù)構(gòu)造函數(shù) 類名(類名類名(類名 &對象名);對象名);/拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù) .;類名類名: 類名(類名類名(類名 &對象名)對象名)/拷貝構(gòu)造函數(shù)的實(shí)現(xiàn)拷貝構(gòu)造函數(shù)的實(shí)現(xiàn) 函數(shù)體函數(shù)體 例例3-2 拷貝構(gòu)造函數(shù)舉例拷貝構(gòu)造函數(shù)舉例#include class Point public: Point(int xx=0,int yy=0)X=xx; Y=yy; Point(Point& p); int GetX()return

21、X; int GetY()return Y; private: int X,Y;Point:Point (Point& p) X=p.X; Y=p.Y; cout拷貝構(gòu)造函數(shù)被調(diào)用拷貝構(gòu)造函數(shù)被調(diào)用endl;例例3-2 拷貝構(gòu)造函數(shù)舉例拷貝構(gòu)造函數(shù)舉例n(1)當(dāng)用類的一個對象去初始化該類的另一個對當(dāng)用類的一個對象去初始化該類的另一個對象時系統(tǒng)自動調(diào)用它實(shí)現(xiàn)拷貝賦值象時系統(tǒng)自動調(diào)用它實(shí)現(xiàn)拷貝賦值。void main(void) Point A(1,2); Point B(A); /拷貝構(gòu)造函數(shù)被調(diào)用拷貝構(gòu)造函數(shù)被調(diào)用 coutB.GetX()endl;例例3-2 拷貝構(gòu)造函數(shù)舉例拷貝構(gòu)造

22、函數(shù)舉例n(2)用類的一個對象去初始化另一個對象時的另用類的一個對象去初始化另一個對象時的另外一種形式外一種形式。void main(void) Point A(1,2); Point B=A; /拷貝構(gòu)造函數(shù)被調(diào)用拷貝構(gòu)造函數(shù)被調(diào)用 coutB.GetX()endl;拷貝構(gòu)造函數(shù)舉例拷貝構(gòu)造函數(shù)舉例(例例3-2) n(3)對象作為函數(shù)參數(shù)傳遞時,調(diào)用拷貝構(gòu)對象作為函數(shù)參數(shù)傳遞時,調(diào)用拷貝構(gòu)造函數(shù)造函數(shù)。例如:。例如:void fun1(Point p) coutp.GetX()endl; void main() Point A(1,2); fun1(A); /調(diào)用拷貝構(gòu)造函數(shù)調(diào)用拷貝構(gòu)造函數(shù)

23、 拷貝構(gòu)造函數(shù)舉例拷貝構(gòu)造函數(shù)舉例(例例4-2)n(4)當(dāng)函數(shù)的返回值是類對象時,系統(tǒng)自動調(diào)用拷當(dāng)函數(shù)的返回值是類對象時,系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)貝構(gòu)造函數(shù)。例如:例如:Point fun2() Point A(1,2); return A; /調(diào)用拷貝構(gòu)造函數(shù)調(diào)用拷貝構(gòu)造函數(shù)void main() Point B; B=fun2(); /調(diào)用調(diào)用fun2函數(shù),系統(tǒng)自動調(diào)用函數(shù),系統(tǒng)自動調(diào)用拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)如果程序員如果程序員沒有為類聲明沒有為類聲明拷貝初始化構(gòu)造函數(shù),拷貝初始化構(gòu)造函數(shù),則編譯器自己則編譯器自己生成一個拷貝構(gòu)造函數(shù)生成一個拷貝構(gòu)造函數(shù)。例如:。

24、例如:#include class Point public: Point(int xx=0,int yy=0)X=xx; Y=yy; /Point(Point& p); int GetX()return X; int GetY()return Y; private: int X,Y;/*Point:Point (Point& p) X=p.X; Y=p.Y; cout拷貝構(gòu)造函數(shù)被調(diào)用拷貝構(gòu)造函數(shù)被調(diào)用endl;*/Point fun2() Point A(1,2);coutA.GetX()endl; return A; /調(diào)用拷貝構(gòu)造函數(shù)調(diào)用拷貝構(gòu)造函數(shù)void main(

25、) Point B; B=fun2(); 拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)這個構(gòu)造函數(shù)執(zhí)行的功能是:用作為初始值的這個構(gòu)造函數(shù)執(zhí)行的功能是:用作為初始值的對象的每個數(shù)據(jù)成員的值,初始化將要建立的對象對象的每個數(shù)據(jù)成員的值,初始化將要建立的對象的對應(yīng)數(shù)據(jù)成員。的對應(yīng)數(shù)據(jù)成員。深拷貝構(gòu)造函數(shù)和深拷貝構(gòu)造函數(shù)和淺拷貝構(gòu)造函數(shù)淺拷貝構(gòu)造函數(shù)淺拷貝構(gòu)造函數(shù)淺拷貝構(gòu)造函數(shù):系統(tǒng)使用的默認(rèn)拷貝構(gòu)造函數(shù),系統(tǒng)使用的默認(rèn)拷貝構(gòu)造函數(shù),都是淺拷貝構(gòu)造函數(shù)都是淺拷貝構(gòu)造函數(shù)#include#includeusing namespace std;class Person public: Person(char *na) /

26、構(gòu)造函數(shù)構(gòu)造函數(shù) coutcall constructorendl; name=new charstrlen(na)+1; / 使用使用new進(jìn)行動態(tài)內(nèi)存分配進(jìn)行動態(tài)內(nèi)存分配 if(name!=0) strcpy(name,na); /*Person(Person&p) / 深拷貝構(gòu)造函數(shù)深拷貝構(gòu)造函數(shù) coutcall copy constructorendl; name=new charstrlen()+1; / 復(fù)制資源復(fù)制資源 if(name!=0) strcpy(name,); / 復(fù)制對象空間復(fù)制對象空間 */void printname()cout

27、nameendl;Person()delete name; / 析構(gòu)函數(shù)的定義,參見析構(gòu)函數(shù)的定義,參見3.3.2小節(jié)小節(jié)private:char *name;/因?yàn)槭侵羔樢驗(yàn)槭侵羔?不能使用淺拷貝構(gòu)造函數(shù)不能使用淺拷貝構(gòu)造函數(shù); / 類定義的結(jié)束類定義的結(jié)束 void main() Person wang(wang); Person li(wang); wang.printname(); li.printname();深拷貝構(gòu)造函數(shù)和深拷貝構(gòu)造函數(shù)和淺拷貝構(gòu)造函數(shù)淺拷貝構(gòu)造函數(shù)小結(jié):小結(jié):(1)深拷貝和淺拷貝可以簡單理解為:如)深拷貝和淺拷貝可以簡單理解為:如果一個類擁有資源,果一個類擁有資

28、源,當(dāng)這個類的對象發(fā)生復(fù)制過當(dāng)這個類的對象發(fā)生復(fù)制過程的時候,資源重新分配程的時候,資源重新分配,這個過程就是,這個過程就是深拷貝深拷貝,反之,反之,沒有重新分配資源沒有重新分配資源,就是,就是淺拷貝淺拷貝(2)在實(shí)際中應(yīng)該)在實(shí)際中應(yīng)該避免淺拷貝避免淺拷貝。5. 數(shù)據(jù)成員初始化數(shù)據(jù)成員初始化構(gòu)造函數(shù)可以采用以下幾種方法對數(shù)據(jù)成員初始構(gòu)造函數(shù)可以采用以下幾種方法對數(shù)據(jù)成員初始化:化:(1)在構(gòu)造函數(shù)的函數(shù)體中進(jìn)行初始化,例如:)在構(gòu)造函數(shù)的函數(shù)體中進(jìn)行初始化,例如:Circle:Circle(float r) radius=r; (2)使用構(gòu)造初始化表對數(shù)據(jù)成員進(jìn)行初始化,)使用構(gòu)造初始化表對

29、數(shù)據(jù)成員進(jìn)行初始化,其格式為:其格式為::():(),()如:如:Circle:Circle(float r):radius(r) n例例3.13對常量數(shù)據(jù)成員和引用數(shù)據(jù)成員初始化對常量數(shù)據(jù)成員和引用數(shù)據(jù)成員初始化實(shí)例。實(shí)例。nclass SillyClassnn public:n SillyClass(int&i):ten(10),refI(i)n n protected:n const int ten; / 常量數(shù)據(jù)成員常量數(shù)據(jù)成員n int&refI; / 引用數(shù)據(jù)成員引用數(shù)據(jù)成員n;nvoid main()nn int i ;n SillyClass sc(i);nn若

30、把構(gòu)造函數(shù)定義為下列方式:若把構(gòu)造函數(shù)定義為下列方式:nSillyClass()nn ten = 10; / error ,常量不能重新賦值,常量不能重新賦值n refI = i; / error , 引用不能重新指派引用不能重新指派n;n(3)混合初始化)混合初始化nStudent:Student(int n,int a,char *pname):number(n),age(a)nnStrcpy(name,pname);6. 類類型和基本數(shù)據(jù)類型的轉(zhuǎn)換類類型和基本數(shù)據(jù)類型的轉(zhuǎn)換n(1) 構(gòu)造函數(shù)用作類型轉(zhuǎn)換構(gòu)造函數(shù)用作類型轉(zhuǎn)換(基本數(shù)據(jù)類型類類型基本數(shù)據(jù)類型類類型)n前提:有一個前提:有一個

31、只帶一個形參的構(gòu)造函數(shù)只帶一個形參的構(gòu)造函數(shù),才可以實(shí),才可以實(shí)現(xiàn)從現(xiàn)從參數(shù)類型向該類類型參數(shù)類型向該類類型的轉(zhuǎn)換,轉(zhuǎn)換是的轉(zhuǎn)換,轉(zhuǎn)換是隱式的隱式的。例如:例如:nclass AnnPublic:n A();n A(int);n;nnf(A a)nf(1);n#include nclass B nnint i; npublic: nB()cout 調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)B()!n ; n; nclass A nnint i; npublic: nA()nncout 調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)A()!n ;n nA(int a) i=a; cout i= i t 調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)A(in

32、t)!n ; A(B y,int a=10) i=a; cout i= i t 調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)A(B)!n ; ; nvoid main() nnA a1(10); nA a2=20; /調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)A(int a); na2=50;/右邊的右邊的50被轉(zhuǎn)化成一個被轉(zhuǎn)化成一個A對象對象,調(diào)用了調(diào)用了n / A(int); n /相當(dāng)于相當(dāng)于: A temp(50); a2 = temp;nB b; nA a3=b; /調(diào)用構(gòu)造函數(shù)調(diào)用構(gòu)造函數(shù)A(B, int); 因?yàn)橐驗(yàn)閕nt有默認(rèn)值有默認(rèn)值. na3=b;/右邊生成了一個臨時對象右邊生成了一個臨時對象(和前面的和前面的

33、a2 = 50一樣一樣)調(diào)用了調(diào)用了A(B, int); nnn(2)類類型)類類型轉(zhuǎn)換函數(shù)轉(zhuǎn)換函數(shù)(類類型基本數(shù)據(jù)類類類型基本數(shù)據(jù)類型型),步驟如下:步驟如下:n(a)在類定義體中在類定義體中聲明聲明noperator type();n(b)定義定義轉(zhuǎn)換函數(shù)的函數(shù)體轉(zhuǎn)換函數(shù)的函數(shù)體n類名類名:operator type()nnreturn type類型的值類型的值;nn(c) 使用使用類型轉(zhuǎn)換函數(shù),類似于基本類型強(qiáng)制轉(zhuǎn)類型轉(zhuǎn)換函數(shù),類似于基本類型強(qiáng)制轉(zhuǎn)換。換。n#includenclass RMBnnpublic:n RMB(double value = 0.0); / 構(gòu)造函數(shù)用作類型轉(zhuǎn)換

34、構(gòu)造函數(shù)用作類型轉(zhuǎn)換n operator double()n return yuan + jf / 100.0; / 類類型轉(zhuǎn)換函數(shù)類類型轉(zhuǎn)換函數(shù)n void display()n cout (yuan + jf / 100.0) endl; nprotected:n unsigned int yuan;n unsigned int jf;n;n nRMB:RMB(double value)nn yuan = value;n jf = ( value yuan ) * 100 + 0.5;nn nvoid main()nn RMB d1(2.0), d2(1.5), d3;n d3 = RMB

35、(double)d1 + (double)d2); / 顯式轉(zhuǎn)換顯式轉(zhuǎn)換n d3 = d1 + d2; n / 隱式轉(zhuǎn)換隱式轉(zhuǎn)換,系統(tǒng)首先會調(diào)用類類型轉(zhuǎn)換函數(shù)把對象,系統(tǒng)首先會調(diào)用類類型轉(zhuǎn)換函數(shù)把對象d1和和d2隱式轉(zhuǎn)換為隱式轉(zhuǎn)換為double數(shù)數(shù)據(jù)類型,據(jù)類型,n/ 然后進(jìn)行相加,加完的結(jié)果再調(diào)用構(gòu)造函數(shù)隱式轉(zhuǎn)換為然后進(jìn)行相加,加完的結(jié)果再調(diào)用構(gòu)造函數(shù)隱式轉(zhuǎn)換為RMB類類型,賦給類類型,賦給d3。n d3.display();n3.3.2 析構(gòu)函數(shù)析構(gòu)函數(shù)n完成對象被刪除前的一些清理工作。完成對象被刪除前的一些清理工作。n在對象的生存期結(jié)束的時刻系統(tǒng)在對象的生存期結(jié)束的時刻系統(tǒng)自動調(diào)用自動調(diào)

36、用它,然它,然后再釋放此對象所屬的空間。后再釋放此對象所屬的空間。n如果程序中未聲明析構(gòu)函數(shù),編譯器將如果程序中未聲明析構(gòu)函數(shù),編譯器將自動產(chǎn)生自動產(chǎn)生一個缺省的析構(gòu)函數(shù)一個缺省的析構(gòu)函數(shù)。n析構(gòu)函數(shù)被自動調(diào)用的析構(gòu)函數(shù)被自動調(diào)用的三種情況三種情況:n(1)一個動態(tài)分配的對象被刪除,即使用)一個動態(tài)分配的對象被刪除,即使用delete刪除對象時。刪除對象時。n(2)程序運(yùn)行結(jié)束時。)程序運(yùn)行結(jié)束時。n(3)一個編譯器生成的臨時對象不再需要時。)一個編譯器生成的臨時對象不再需要時。構(gòu)造函數(shù)和析構(gòu)函數(shù)舉例構(gòu)造函數(shù)和析構(gòu)函數(shù)舉例#includeclass Point public: Point(in

37、t xx,int yy); Point( ); /.其它函數(shù)原形其它函數(shù)原形 private: int X,int Y;Point:Point(int xx,int yy) X=xx; Y=yy;Point:Point( )/.其它函數(shù)的實(shí)現(xiàn)略其它函數(shù)的實(shí)現(xiàn)略/例例#include class Q int x,y; public: Q(int a=0,int b=0) x=a; y=b; void P(void) coutxtyn; Q( ) cout 調(diào)用了析構(gòu)函數(shù)調(diào)用了析構(gòu)函數(shù)!n; ;void main(void) Q q(50,100); q.P( ); cout 退出主函數(shù)退出主函數(shù)

38、!n;輸出:輸出: 50 100 退出主函數(shù)退出主函數(shù)! 調(diào)用了析構(gòu)函數(shù)調(diào)用了析構(gòu)函數(shù)!析構(gòu)函數(shù)和構(gòu)造函數(shù)的調(diào)用順序析構(gòu)函數(shù)和構(gòu)造函數(shù)的調(diào)用順序#include #include class Studentpublic: Student(char* pName=no name,int ssId=0) strncpy(name,pName,40); name39= 0; id = ssId; cout Constructing new student pName endl; Student(Student& s) / 拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù) cout Constructing copy

39、 of endl; strcpy(name, copy of ); strcat(name,); id=s.id; Student() cout Destructing name endl; protected: char name40; int id;void fn(Student s) cout In function fn()n; / fn函數(shù)調(diào)用函數(shù)調(diào)用結(jié)束時,析構(gòu)對象結(jié)束時,析構(gòu)對象svoid main() Student randy(Randy,1234); / 調(diào)用構(gòu)造調(diào)用構(gòu)造函數(shù),創(chuàng)建對象函數(shù),創(chuàng)建對象randy Student wang(wang,5

40、678); / 調(diào)用構(gòu)調(diào)用構(gòu)造函數(shù),創(chuàng)建對象造函數(shù),創(chuàng)建對象wang cout Calling fn()n; fn(randy); / 調(diào)用調(diào)用fn函數(shù),參數(shù)傳遞時調(diào)函數(shù),參數(shù)傳遞時調(diào)用拷貝構(gòu)造函數(shù)用拷貝構(gòu)造函數(shù) cout Returned from fn()n;/ 主函數(shù)調(diào)用結(jié)束時,先析構(gòu)對象主函數(shù)調(diào)用結(jié)束時,先析構(gòu)對象wang,再析構(gòu)對象再析構(gòu)對象randy運(yùn)行結(jié)果:運(yùn)行結(jié)果:Constructing new student RandyConstructing new student wangCalling fn()Constructing copy of RandyIn function

41、 fn()Destructing copy of RandyReturned from fn()Destructing wangDestructing Randy例例4-3 類的應(yīng)用舉例類的應(yīng)用舉例一圓型游泳池如圖所示,現(xiàn)在需在其周圍一圓型游泳池如圖所示,現(xiàn)在需在其周圍建一圓型過道,并在其四周圍上柵欄。柵欄價格建一圓型過道,并在其四周圍上柵欄。柵欄價格為為35元元/米,過道造價為米,過道造價為20元元/平方米。過道寬度平方米。過道寬度為為3米,游泳池半徑由鍵盤輸入。要求編程計算米,游泳池半徑由鍵盤輸入。要求編程計算并輸出過道和柵欄的造價。并輸出過道和柵欄的造價。游泳池過道#include co

42、nst float PI = 3.14159;const float FencePrice = 35;const float ConcretePrice = 20;/聲明類聲明類Circle 及其數(shù)據(jù)和方法及其數(shù)據(jù)和方法class Circle private: float radius; public: Circle(float r); /構(gòu)造函數(shù)構(gòu)造函數(shù) float Circumference( ); /圓周長圓周長 float Area( ); /圓面積圓面積;/ 類的實(shí)現(xiàn)類的實(shí)現(xiàn)/ 構(gòu)造函數(shù)初始化數(shù)據(jù)成員構(gòu)造函數(shù)初始化數(shù)據(jù)成員radiusCircle:Circle(float r)ra

43、dius=r/ 計算圓的周長計算圓的周長float Circle:Circumference( ) return 2 * PI * radius; / 計算圓的面積計算圓的面積 float Circle:Area( ) return PI * radius * radius;void main ( ) float radius; float FenceCost, ConcreteCost; / 提示用戶輸入半徑提示用戶輸入半徑 coutradius; / 聲明聲明 Circle 對象對象 Circle Pool(radius); Circle PoolRim(radius + 3); / 計算

44、柵欄造價并輸出計算柵欄造價并輸出 FenceCost = PoolRim.Circumference( ) * FencePrice; cout Fencing Cost is ¥ FenceCost endl; / 計算過道造價并輸出計算過道造價并輸出 ConcreteCost = (PoolRim.Area( ) - Pool.Area( ))*ConcretePrice; cout Concrete Cost is ¥ ConcreteCost endl;運(yùn)行結(jié)果運(yùn)行結(jié)果Enter the radius of the pool: 10Fencing Cost is ¥2858.85Con

45、crete Cost is ¥4335.39#include#includeclass complexprivate:double real;double imag;public: void init (double r,double i) real=r; imag=i; double realcomplex() return real; double imagcomplex()return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(類與對象、內(nèi)聯(lián)成員函數(shù)類與對象、內(nèi)

46、聯(lián)成員函數(shù))void main()complex a ;a.init(1.1,2.2);cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.abscomplex()endl;#include#includeclass complexprivate:double real;double imag;public: complex (double r,double i) real=r; imag=i; double rea

47、lcomplex() return real; double imagcomplex()return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(構(gòu)造函數(shù))(構(gòu)造函數(shù))void main()complex a (1.1,2.2) ;cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.absco

48、mplex()endl;#include#includeclass complexprivate:double real;double imag;public: complex (double r=0.0,double i=0.0) real=r; imag=i; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(帶缺省參數(shù)構(gòu)造函數(shù))(帶缺省參數(shù)構(gòu)造函數(shù)

49、)void main()complex a (1.1,2.2) ;cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.abscomplex()endl;complex b;cout“real of complex b=“b.realcomplex()endl;cout“image of complex b=“b.imagelcomplex()endl;cout“abs of complex b=“b.abscompl

50、ex()endl;#include#includeclass complexprivate: double real;double imag;public: complex (double r,double i) real=r; imag=i; complex () real=0.0; imag=0.0; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)

51、(構(gòu)造函數(shù)重載)(構(gòu)造函數(shù)重載)void main()complex a (1.1,2.2) ;cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.abscomplex()endl;complex b;cout“real of complex b=“b.realcomplex()endl;cout“image of complex b=“b.imagelcomplex()endl;cout“abs of comple

52、x b=“b.abscomplex()endl;#include#includeclass complexprivate: double real;double imag;public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex()cout“destructing.n”; double realcomplex() return real; double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; ret

53、urn sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(析構(gòu)函數(shù))(析構(gòu)函數(shù))void main()complex a (1.1,2.2) ;cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.abscomplex()endl;complex b;cout“real of complex b=“b.realcomplex()endl;cout“image of complex b=“b.imagelcomplex()endl;

54、cout“abs of complex b=“b.abscomplex()endl;#include#includeclass complexprivate: double real;double imag;public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex &c) real=c.real; imag=c.imag double realcomplex() return real; double imagcomplex() return imag; double abscomplex(

55、) double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(拷貝構(gòu)造函數(shù)(拷貝構(gòu)造函數(shù) -應(yīng)用應(yīng)用A)void main()complex a (1.1,2.2) ;cout“real of complex a=“a.realcomplex()endl;cout“image of complex a=“a.imagelcomplex()endl;cout“abs of complex a=“a.abscomplex()endl;complex b(a);cout“real of complex b=“b.realcomplex()e

56、ndl;cout“image of complex b=“b.imagelcomplex()endl;cout“abs of complex b=“b.abscomplex()endl;#include#includeclass complexprivate: double real;double imag;public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex &c) real=c.real; image=c.image double realcomplex() return real;

57、 double imagcomplex() return imag; double abscomplex() double t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(拷貝構(gòu)造函數(shù)(拷貝構(gòu)造函數(shù) -應(yīng)用)應(yīng)用)void display(complex p)cout“real of complex p=“p.realcomplex()endl;cout“image of complex p=“p.imagelcomplex()endl;cout“abs of complex p=“p.abscomplex()endl;void main

58、()complex a (1.1,2.2) ;display(a);#include#includeclass complexprivate: double real;double imag;public: complex (double r=0.0,double i=0.0) real=r; imag=i; complex (complex &c) real=c.real; image=c.image double realcomplex() return real; double imagcomplex() return imag; double abscomplex() doub

59、le t; t=real*real+imag*imag; return sqrt(t); ;例:復(fù)數(shù)例:復(fù)數(shù)(拷貝構(gòu)造函數(shù)(拷貝構(gòu)造函數(shù) -應(yīng)用應(yīng)用C)complex fun()complex a (1.1,2.2) ;return a;void main()complex p;p=fun();cout“real of complex p=“p.realcomplex()endl;cout“image of complex p=“p.imagelcomplex()endl;cout“abs of complex p=“p.abscomplex()endl;聚集(組合)的概念聚集(組合)的概念n

60、類中的成員數(shù)據(jù)是另一個類的對象。類中的成員數(shù)據(jù)是另一個類的對象。n可以在已有的抽象的基礎(chǔ)上實(shí)現(xiàn)更復(fù)雜的抽象。可以在已有的抽象的基礎(chǔ)上實(shí)現(xiàn)更復(fù)雜的抽象。n34 類的聚集對象成員類的聚集對象成員通過對復(fù)雜對象進(jìn)行分解、抽象,使我們能夠?qū)⒁粋€復(fù)雜對象通過對復(fù)雜對象進(jìn)行分解、抽象,使我們能夠?qū)⒁粋€復(fù)雜對象理解為簡單對象的組合。理解為簡單對象的組合。分解得到復(fù)雜對象的部件對象,這些部件對象比它高層的復(fù)雜分解得到復(fù)雜對象的部件對象,這些部件對象比它高層的復(fù)雜對象更容易理解和實(shí)現(xiàn)。然后由這些部件對象來對象更容易理解和實(shí)現(xiàn)。然后由這些部件對象來“裝配裝配”復(fù)雜復(fù)雜對象。對象。舉例舉例class Point private: float x,y; /點(diǎn)的坐標(biāo)點(diǎn)的坐標(biāo) public: Point(float h,float v); /構(gòu)造函數(shù)構(gòu)造函數(shù) f

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論