c++課件第九章關(guān)于類和對象的進一步討論_第1頁
c++課件第九章關(guān)于類和對象的進一步討論_第2頁
c++課件第九章關(guān)于類和對象的進一步討論_第3頁
c++課件第九章關(guān)于類和對象的進一步討論_第4頁
c++課件第九章關(guān)于類和對象的進一步討論_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第9章 關(guān)于類和對象的進一步討論9.1構(gòu)造函數(shù)9.1.1對象的初始化在建立一個對象時,常常要做一些初始化工作,例如數(shù)據(jù)成員賦初值等。類的數(shù)據(jù)成員是不能在聲明類時初始化的。#include using namespace std;class Timeint hour;/int hour=0;int minute;int sec;void main()Time t1=14,56,30;error C2552: t1 : non-aggregates cannot be initialized with initializer list上面這種寫法是錯誤的。如果數(shù)據(jù)成員是公有的,則可以用初始化列表形

2、式初始化。#include using namespace std;class Timepublic:int hour;int minute;int sec;void main()Time t1=14,56,30;9.1.2構(gòu)造函數(shù)的作用C+提供構(gòu)造函數(shù)來處理對象的初始化,構(gòu)造函數(shù)是一種特殊的成員函數(shù),不需要用戶來調(diào)用它,而是建立對象時自動執(zhí)行。構(gòu)造函數(shù)與類同名,并且沒有返回類型。構(gòu)造函數(shù)可以根據(jù)需要進行重載。例9.1在例8.3基礎(chǔ)上定義構(gòu)造成員函數(shù)。#include using namespace std;class Timepublic: Time() hour=0;minute=0;se

3、c=0; void set_time(); void show_time();private: int hour; int minute; int sec;void Time:set_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;int main()Time t1;t1.set_time();t1.show_time();Time t2;t2.show_time();return 0;10 25 5410:25:540:0:09.1.3帶參數(shù)的構(gòu)造函數(shù)例9.2 有兩個長方體,其長、寬、高分

4、別為(1)12,20,25(2)15,30,21。求它們的體積。編一個基于對象的程序,在類中用帶參數(shù)的構(gòu)造函數(shù)。#include using namespace std;class Boxpublic:Box(int,int,int);int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return(height*width*length); int main()Box box1(12,25,30)

5、;coutThe volume of box1 is box1.volume()endl;Box box2(15,30,21);coutThe volume of box2 is box2.volume()endl;return 0;The volume of box1 is 9000The volume of box2 is 94509.1.4用參數(shù)初始化表對數(shù)據(jù)成員初始化#include using namespace std;class Boxpublic:Box(int h,int w ,int len): height(h),width(w),length(len)int volum

6、e();private:int height;int width;int length;int Box:volume()return(height*width*length); int main()Box box1(12,25,30);coutThe volume of box1 is box1.volume()endl;Box box2(15,30,21);coutThe volume of box2 is box2.volume()endl;return 0;9.1.5構(gòu)造函數(shù)的重載在一個類中可以定義多個構(gòu)造函數(shù),以便對類對象提供不同的初始化方法。例9.3#include using na

7、mespace std;class Boxpublic:Box();Box(int h,int w ,int len): height(h),width(w),length(len)int volume();private:int height;int width;int length;Box:Box()height=10;width=10;length=10;int Box:volume()return(height*width*length); int main()Box box1;coutThe volume of box1 is box1.volume()endl;Box box2(1

8、5,30,21);coutThe volume of box2 is box2.volume()endl;return 0;The volume of box1 is 1000The volume of box2 is 94509.1.6使用默認參數(shù)的構(gòu)造函數(shù)例9.4#include using namespace std;class Boxpublic:Box(int w=10,int h=10,int len=10);int volume();private:int height;int width;int length;Box:Box(int w,int h,int len)height

9、=h;width=w;length=len;int Box:volume()return(height*width*length); int main()Box box1;coutbox1:box1.volume()endl;Box box2(15);coutbox2:box2.volume()endl;Box box3(15,30);coutbox3:box3.volume()endl;Box box4(15,30,20);coutbox4:box4.volume()endl;return 0;box1:1000box2:1500box3:4500box4:9000構(gòu)造函數(shù)的二義性問題:(1

10、) 定義下面兩個構(gòu)造函數(shù)Box();Box(int h=10,int w=10,int len=10);在執(zhí)行 Box box1;時產(chǎn)生二義性。(2) 定義下面構(gòu)造函數(shù)Box();Box(int h=10,int w=10,int len=10);Box(int,int);在執(zhí)行 Box box1;Box box2(15,30);時產(chǎn)生二義性。(3) 定義下面兩個構(gòu)造函數(shù)Box();Box(int h,int w=10,int len=10);Box(int,int); 在執(zhí)行 Box box1;/正確 Box box2(15);/調(diào)用第二個構(gòu)造函數(shù)。 Box box3(15,30);/產(chǎn)生二

11、義性。9.2析構(gòu)函數(shù)析構(gòu)函數(shù)的作用是在撤銷對象占用的內(nèi)存之前完成一些清理工作。它不返回任何值,沒有函數(shù)類型,沒有函數(shù)參數(shù),不能被重載。例9.5 包含構(gòu)造函數(shù)和析構(gòu)函數(shù)的C+程序。#include #include using namespace std;class Studentpublic:Student(int n,string nam,char s)num=n;name=nam;sex=s;coutConstructor called.endl; Student()coutDestructor called.numendl;void display()coutnum:numendl;co

12、utname:nameendl;coutsex:sexendlendl;private: int num; string name; char sex;int main()Student stud1(10010,Wang_li,f);stud1.display();Student stud2(10011,Zhang_fun,m);stud2.display();return 0;Constructor called.num:10010name:Wang_lisex:fConstructor called.num:10011name:Zhang_funsex:mDestructor called

13、.10011Destructor called.100109.3調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)的順序先構(gòu)造的后析構(gòu),后構(gòu)造的先析構(gòu)。(參見上例)(1)在全局范圍中定義的對象,它的構(gòu)造函數(shù)在所有函數(shù)(包括main())執(zhí)行之前調(diào)用。當main()函數(shù)執(zhí)行完畢或調(diào)用exit()函數(shù)時,調(diào)用析構(gòu)函數(shù)。(2)如果在函數(shù)中定義靜態(tài)局部對象則只在程序第一次調(diào)用此函數(shù)時調(diào)用構(gòu)造函數(shù)。當main()函數(shù)執(zhí)行完畢或調(diào)用exit()函數(shù)時,調(diào)用析構(gòu)函數(shù)。9.4對象數(shù)組例9.6 對象數(shù)組的使用方法。#include using namespace std;class Boxpublic:Box(int h=10,int w

14、=12,int len=15) :height(h),width(w),length(len) int volume();private:int height;int width;int length;int Box:volume()return(height*width*length);int main()Boxa3=Box(10,12,15),Box(15,18,20),Box(16,20,26);coutvolume of a0 is a0.volume()endl;coutvolume of a0 is a1.volume()endl;coutvolume of a0 is a2.vo

15、lume()endl;return 0;volume of a0 is 1800volume of a0 is 5400volume of a0 is 83209.5對象指針9.5.1指向?qū)ο蟮闹羔?.5.2指向?qū)ο蟪蓡T的指針1. 指向?qū)ο髷?shù)據(jù)成員的指針2. 指向?qū)ο蟪蓡T函數(shù)的指針例9.7 有關(guān)對象指針的使用方法。#include using namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;void get_time();Time:Time(int h,int m,int s)hour=h

16、;minute=m;sec=s;void Time:get_time()couthour:minute:secendl;int main()Time t1(10,13,56);int *p1=&t1.hour;/指向?qū)ο髷?shù)據(jù)成員的指針cout*p1get_time();void (Time:*p3)();/定義指向Time類成員函數(shù)的指針p3=&Time:get_time;(t1.*p3)();/ (p2-*p3)();return 0;1010:13:5610:13:5610:13:569.5.3 this 指針在每一個成員函數(shù)中都包含一個特殊的指針,這個指針的名字是固定的,稱為this,它

17、是指向被調(diào)用對象的指針。a為Box類的一個對象。int Box:volume()return (height*width*length);a.volume();被C+把它處理為:int Box:volume(Box *this)return (this-height*this-width*this-length);a. volume(&a);這樣,各個對象在成員函數(shù)中都使用自己的數(shù)據(jù),互不干擾。9.6共用數(shù)據(jù)的保護9.6.1常對象定義對象時指定對象為常對象。常對象的所有數(shù)據(jù)成員的值不能改,所以常對象必須有初值。 Time const t1(12,34,46);等價寫法:const Time t

18、1(12,34,46);9.6.2常對象成員1. 常數(shù)據(jù)成員在類體中定義了常數(shù)據(jù)成員hour:const int hour;則只能通過構(gòu)造函數(shù)的參數(shù)初始化列表對常數(shù)據(jù)成員進行初始化。Time:Time(int h)hour=h;/非法Time:Time(int h):hour(h)/合法2. 常成員函數(shù)int get_hour()constreturn hour;常成員函數(shù),只能引用本類中的數(shù)據(jù)成員,而不能修改數(shù)據(jù)成員。#include using namespace std;class Timepublic:Time(int,int,int);const int hour;int minut

19、e;int sec;int get_hour()constreturn hour;void test()couthour;Time:Time(int h,int m,int s):hour(h)minute=m;sec=s;void fun(Time &t)t.minute=18;int main()Time t1(10,13,56);Time const t2(12,34,46);fun(t1);coutt2.get_hour()minute=88;/不能通過pt修改所指對象的數(shù)據(jù)成員 /error C2166: l-value specifies const objectpt=&t2;t1

20、.minute=88;/合法(3)指向常對象的指針變量應用于函數(shù)參數(shù)(好處是省空間)。void main()void fun(const Time *p);Time t1(10,13,56);fun(&t1);void fun(const Time *p)p-minute=19;/錯誤coutminuteendl;9.6.5對象的常引用例9.8 對象的常引用#include using namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;Time:Time(int h,int m,int s)

21、hour=h;minute=m;sec=s;void fun(const Time &t) /形參t是實參的引用,形參tt.hour=18;/不占空間。const保證不修改實參數(shù)據(jù)成員/error C2166: l-value specifies const objectint main()Time t1(10,13,56);fun(t1);coutt1.hourendl;return 0;9.6.6 const型數(shù)據(jù)的小結(jié)9.7對象的動態(tài)建立和釋放Box *pt=new Box;delete pt;pt=new Box(12,15,18);delete pt;9.8對象的賦值和復制9.8.1

22、對象的賦值例9.9 對象的賦值。#include using namespace std;class Boxpublic:Box(int=10,int=10,int=10);Box(const Box& b)/復制構(gòu)造函數(shù)height=b.height;/也稱拷貝初始化構(gòu)造函數(shù)width=b.width;length=b.length;int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return

23、(height*width*length);int main()Box box1(15,30,25),box2; coutbox1:box1.volume()endl;box2=box1; coutbox2:box2.volume()endl;Box box3(box1);coutbox3:box2.volume()endl;return 0;box1:11250box2:11250box3:11250特別聲明:類的數(shù)據(jù)成員不能包含動態(tài)分配數(shù)據(jù),否則在賦值時會產(chǎn)生嚴重后果。9.8.2對象的復制復制構(gòu)造函數(shù)用在用一個已有對象去建立一個新對象。(1) 一般用途見上例。(2) 當函數(shù)的參數(shù)為類的對象

24、時。void fun(Box b) void main()Box box1(12,15,18);fun(box1);/此時調(diào)用復制構(gòu)造函數(shù)生成形參b(3) 當函數(shù)的返回值是類的對象時。Box f()Box box1(12,15,18);return box1;void main()Box box2;box2=f();調(diào)用函數(shù)f,在f的return語句中,由 /box1調(diào)用復制構(gòu)造函數(shù)生成臨時對象賦值給box2 (4)用戶沒有編制自己的復制構(gòu)造函數(shù),則系統(tǒng)自動生成一個復制構(gòu)造函數(shù)。在沒使用動態(tài)數(shù)據(jù)成員,用戶沒有必要編制自己的復制構(gòu)造函數(shù)。9.9靜態(tài)成員9.9.1靜態(tài)數(shù)據(jù)成員靜態(tài)數(shù)據(jù)成員是以關(guān)鍵字

25、static開頭,為該類所有對象所共享,在類的作用域范圍內(nèi),類體外使用int Box:height=10;語句,在內(nèi)存中申請一份空間。例9.10 引用靜態(tài)數(shù)據(jù)成員#include using namespace std;class Boxpublic:Box(int,int);int volume();static int height;int width;int length;Box:Box(int w,int len) width=w; length=len; int Box:volume()return(height*width*length); int Box:height=10;vo

26、id main()Box a(15,20),b(20,30);couta.heightendl;Box:height=5;coutb.heightendl;b.height=10;coutBox:heightendl;couta.volume()endl;1051030009.9.2靜態(tài)成員函數(shù)靜態(tài)成員函數(shù)沒有this指針,所以靜態(tài)成員函數(shù)主要用來訪問靜態(tài)數(shù)據(jù)成員。#include using namespace std;class Studentpublic:Student(int,int,int);void total();static float average();private:in

27、t num;int age;float score;static float sum;static int count;Student:Student(int m,int a,int s)num=m; age=a; score=s;void Student:total()sum+=score;count+;float Student:average() return(sum/count); float Student:sum=0;int Student:count=0;int main()Student stud3=Student(1001,18,70),Student(1002,19,79)

28、,Student(1005,20,98); coutn;for(int i=0;in;i+)studi.total();coutThe average score of n students is stud0.average()endl; return 0;please input the number of students:3The average score of 3 students is 82.33339.10友元9.10.1友元函數(shù)1. 將普通函數(shù)聲明為友元函數(shù)例9.12 友元函數(shù)的簡單例子。#include using namespace std;class Timepublic

29、:Time(int,int,int);friend void display(Time &);private:int hour;int minute;int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void display(Time &t)coutt.hour:t.minute:t.secendl;void main()Time t1(10,13,56);display(t1);10:13:562. 友元成員函數(shù)例9.13 友元成員函數(shù)的簡單例子。#include using namespace std;class Date;

30、class Timepublic:Time(int,int,int);void display(const Date&);private:int hour,minute,sec;class Datepublic:Date(int,int,int);friend void Time:display(const Date &);private:int month,day,year;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void Time:display(const Date &da)coutda.month/da.day/da.year

31、endl;couthour:minute:secendl;Date:Date(int m,int d,int y)month=m;day=d; year=y;void main()Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);12/25/200410:13:563. 一個函數(shù)(包括普通函數(shù)和成員函數(shù))可以被多個類聲明為“朋友”,這樣它就可以引用多個類中的私有數(shù)據(jù)。9.10.2友元類可以將一個類說明為另一個類的友元。例如:#includeclass oneprivate:int x;friend class two;public:one

32、(int k)x=k;class twopublic:void fun(one& o);void two : fun(one& o) couto.xendl;void main()two t;one o(88);t.fun(o);這樣,類two的所有成員函數(shù)都是類one的友元。(1)友元關(guān)系是單向的而不是雙向的。#includeclass oneprivate:int x;friend class two;public:one(int k)x=k;void fun_one(two& t);class twoint y;/friend class one;public: void fun(one

33、& o);two(int k)y=k;void two:fun(one& o)couto.xendl;void one:fun_one(two& t)coutt.yendl;/error C2248: y : cannot access private member/ declared in class twovoid main()two t(99);one o(88);t.fun(o);o.fun_one(t);(2)友元關(guān)系不能傳遞,如果B類是A類的友元,C類是B類的友員,不等于C類是A類的友元。#includeclass zeroprivate:int z;friend class one;public:z

溫馨提示

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

評論

0/150

提交評論