




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第五章特殊函數(shù)和組員5.1對(duì)象組員初始化 類對(duì)象能夠做其它類數(shù)據(jù)組員,稱為對(duì)象組員。如:class A 類名1 組員1; 類名2 組員2; ;當(dāng)A類產(chǎn)生對(duì)象時(shí)要初始化A所有組員,因此會(huì)調(diào)用A類 函數(shù)。結(jié)構(gòu)1第1頁(yè)第1頁(yè)A類結(jié)構(gòu)函數(shù)定義下列:A:A(參數(shù)0):組員1(參數(shù)1),組員2(參數(shù)2),組員n(參數(shù)表n) /其它操作 注:假如初始化列表某項(xiàng)參數(shù)表為空,則列表中相應(yīng)項(xiàng)能夠省略。2第2頁(yè)第2頁(yè)如有:class dateint year,month,day;public:;class student int num;string name; date birthday;public: ;則st
2、udent類結(jié)構(gòu)函數(shù)形式為:student(int n,string s,date d) :num(n),name(s),birthday(d)3第3頁(yè)第3頁(yè)若對(duì)象B是類A子對(duì)象,則在建立A類對(duì)象時(shí): 執(zhí)行B結(jié)構(gòu)函數(shù)(初始化類中其它組員)結(jié)構(gòu)順序 執(zhí)行B結(jié)構(gòu)函數(shù)(初始化B數(shù)據(jù)組員) 執(zhí)行B析構(gòu)函數(shù) 執(zhí)行A析構(gòu)函數(shù)析構(gòu)順序注意:1)A產(chǎn)生對(duì)象時(shí),先調(diào)用對(duì)象組員結(jié)構(gòu)函數(shù),初始化對(duì)象組員,然后才執(zhí)行A結(jié)構(gòu)函數(shù),對(duì)象組員初始化順序與這些對(duì)象在類A中闡明順序,與它們?cè)诔跏蓟斜碇许樞驘o(wú)關(guān)。4第4頁(yè)第4頁(yè)注意:2)A類結(jié)構(gòu)函數(shù)中未給出組員對(duì)象初始化,組員對(duì)象產(chǎn)生時(shí)調(diào)用無(wú)參結(jié)構(gòu)函數(shù),若此時(shí)組員所在類中沒(méi)有無(wú)
3、參結(jié)構(gòu)函數(shù)則報(bào)錯(cuò)!5第5頁(yè)第5頁(yè)找出下面程序錯(cuò)誤,更正后分析程序結(jié)果(lt5_1b.cpp)class A int x1, y1;public: A(int c,int d) x1=c;y1=d; coutA結(jié)構(gòu)!x1y1endl; ;class B A a; int x2,y2;public: B()coutB默認(rèn)結(jié)構(gòu)!; B(int c=0,int d=0) x2=c; y2=d; coutB結(jié)構(gòu)!x2y2endl;void main()B b; A()coutA默認(rèn)結(jié)構(gòu)endl;B b(1,2);B(int c,int d,int x,int y):a(x,y)B b(1,2,10,10
4、0);6第6頁(yè)第6頁(yè)練習(xí):給出程序運(yùn)營(yíng)結(jié)果(lt5_1.cpp)#include using namespace std;class Pointint x,y;public:Point():x(0),y(0)Point(int a,int b):x(a),y(b) cout結(jié)構(gòu):x,yendl;class RectanglePoint a;Point b;public:Rectangle(int x,int y,int m,int n): a(x,y) ,b(m,n) ;void main()Rectangle a(1,1,5,5);b(m,n),a(x,y)7第7頁(yè)第7頁(yè)對(duì)象組員初始化8第8
5、頁(yè)第8頁(yè)const組員和引用組員初始化數(shù)據(jù)組員不能在定義時(shí)初始化數(shù)據(jù)組員操作語(yǔ)句必須放在組員函數(shù)中引用組員不能初始化為常量引用const組員和引用組員 必須在結(jié)構(gòu)函數(shù)初始化列表中初始化 (lt5_1c.cpp)初始化列表形式結(jié)構(gòu)函數(shù):A:A(參數(shù)0):組員1(參數(shù)1),組員2(參數(shù)2) 9第9頁(yè)第9頁(yè)using namespace std;class exampleconst int num;int data;int &ref;public:example(int n,int f):num(n),data(f),ref(data)cout結(jié)構(gòu).endl;void show()cout對(duì)象中數(shù)據(jù)
6、是: num,data,refendl;void main()int x=1,y=2;example a(x,y);a.show();10第10頁(yè)第10頁(yè)定義時(shí)使用了static,則組員為靜態(tài)組員5.2靜態(tài)組員闡明:1)靜態(tài)數(shù)據(jù)組員必須在類體外按照下列格式:類型 類名:靜態(tài)組員名=值;進(jìn)行初始化,不可在結(jié)構(gòu)函數(shù)中初始化11第11頁(yè)第11頁(yè)class Teststatic int x;int n;public:Test()Test(int a,int b)x=a;n=b;static int func()return x;static void sfunc( Test &r, int a) r.
7、n=a;int Getn()return n;int Test:x=25;靜態(tài)數(shù)據(jù)組員初始化12第12頁(yè)第12頁(yè)4、靜態(tài)組員是類組員不是對(duì)象組員2、static組員所有者是類,被該類所有對(duì)象所共有, 所有對(duì)象均可訪問(wèn)靜態(tài)組員 。靜態(tài)組員仍然遵循public,private, protected訪問(wèn)準(zhǔn)則。 3、靜態(tài)組員不依賴于對(duì)象而存在 對(duì)象不存在時(shí)靜態(tài)組員已存在5、靜態(tài)組員函數(shù)沒(méi)有this指針, 不可直接使用非靜態(tài)組員, 必須通過(guò)對(duì)象(或者指向?qū)ο笾羔槪┦褂梅庆o態(tài)組員。13第13頁(yè)第13頁(yè)class Teststatic int x;int n;public:Test()Test(int a,
8、int b)x=a; n=b;static int func()return x;static void sfunc ( Test &r, int a) r.n=a;int Getn()return n;int Test:x=25;類中任何函數(shù)都能夠訪問(wèn)靜態(tài)組員。靜態(tài)組員函數(shù)不能直接訪問(wèn)非靜態(tài)數(shù)據(jù)組員,能夠通過(guò)對(duì)象來(lái)訪問(wèn)。( int a) n=a;error : illegal reference to data member Test:n in a static member function.14第14頁(yè)第14頁(yè)7、未定義對(duì)象時(shí),能夠通過(guò)類使用靜態(tài)組員按下列格式: 類名:靜態(tài)數(shù)據(jù)組員名 類
9、名:靜態(tài)組員函數(shù)();6、靜態(tài)組員函數(shù)不能闡明為虛函數(shù)(第8章)15第15頁(yè)第15頁(yè)class Testint n; public:Test()Test(int a,int b)x=a;n=b;static int func()return x;static void sfunc( Test &r, int a) r.n=a;int Getn()return n;int Test:x=25;void main()coutTest:func();通過(guò)類使用靜態(tài)組員函數(shù)static int x;coutTest:x();通過(guò)類使用靜態(tài)組員函數(shù)或者數(shù)據(jù)組員16第16頁(yè)第16頁(yè)class Testin
10、t n; static int x;public:Test()Test(int a,int b)x=a;n=b;static int func()return x;static void sfunc( Test &r, int a) r.n=a;int Getn()return n;int Test:x=25;void main()Test b,c;b.sfunc(b,58);cout b.Getn();cout b.func();cout c.func();Test a(24,56);cout a.func() b.func() c.func()endl;58252524242417第17頁(yè)
11、第17頁(yè)class Testint n;static int x;public:Test()Test(int a,int b)x=a;n=b;static int func() n=x+1; return x;static void sfunc(Test &ref,int a)ref.n=a;int Getn()return n;找出下面類定義中錯(cuò)誤,并闡明原因int Test:x=100;靜態(tài)組員函數(shù)不能直接訪問(wèn)非靜態(tài)數(shù)據(jù)組員靜態(tài)數(shù)據(jù)組員必須進(jìn)行初始化18第18頁(yè)第18頁(yè)1,靜態(tài)組員函數(shù)與類名連用,可通過(guò)對(duì)象使用class teststatic int x;int n;public:test
12、(int a=0, int b=0) x=a; n=b;static int func() return x; int getn() return n;int test:x =25;void main()couttest:func () endl;test b;cout b.func() endl;與類名連用通過(guò)對(duì)象使用19第19頁(yè)第19頁(yè)2,在沒(méi)有建立對(duì)象之前,靜態(tài)組員已經(jīng)存在class teststatic int x;int n;public:test(int a=0, int b=0) x=a; n=b;static int func() return x; int getn() re
13、turn n;int test:x =25;void main()couttest:func () endl;test b, c;cout b.func() endl;20第20頁(yè)第20頁(yè)3,靜態(tài)組員是類組員不是對(duì)象組員class teststatic int x;int n;public:test(int a=0, int b=0) x=a; n=b;static int func() return x; int getn() return n;int test:x =25;void main()couttest:func () endl;test b, c;cout b.func() en
14、dl;21第21頁(yè)第21頁(yè)4,靜態(tài)組員被類所有對(duì)象共享class teststatic int x;int n;public:test(int a=0, int b=0) x=a; n=b;static int func() return x; int getn() return n;int test:x =25;void main()couttest:func () endl;test b, c;cout b.func() c.func() endl;22第22頁(yè)第22頁(yè)5,靜態(tài)組員函數(shù)不可使用非靜態(tài)組員class teststatic int x;int n;public:test(int
15、 a=0, int b=0) x=a; n=b;static int func()return n; ;int test:x =25;void main()couttest:func () endl;test b, c;cout b.func() c.func() endl;錯(cuò)誤23第23頁(yè)第23頁(yè)特點(diǎn):一旦定義始終存在于內(nèi)存中, 直到程序結(jié)束才釋放復(fù)合語(yǔ)句(用括起來(lái)多條語(yǔ)句)內(nèi)定義對(duì)象只在復(fù)合語(yǔ)句內(nèi)有效,復(fù)合語(yǔ)句執(zhí)行完畢,對(duì)象釋放內(nèi)存。5.2靜態(tài)組員-靜態(tài)對(duì)象靜態(tài)對(duì)象是由static申明類對(duì)象24第24頁(yè)第24頁(yè)class testint n;public:test(int i)n=i;co
16、ut結(jié)構(gòu):nendl;test()cout析構(gòu):nendl;int getn()return n;void inc()+n;void main()cout循環(huán)開(kāi)始endl; for(int i=0;i2;i+) couti=i時(shí):endl; test b(3); b.inc(); coutb.n=b.getn()endl; cout循環(huán)結(jié)束endl; coutmain結(jié)束endl;bi=0時(shí):34循環(huán)開(kāi)始結(jié)構(gòu):3b=4析構(gòu)4i=0時(shí):bi=1時(shí):3425第25頁(yè)第25頁(yè)class testint n;public:test(int i)n=i;cout結(jié)構(gòu):nendl;test()cout析構(gòu)
17、:nendl;int getn()return n;void inc()+n;void main()cout循環(huán)開(kāi)始endl; for(int i=0;i2;i+) couti=i時(shí):endl; test b(3); b.inc(); coutb.n=b.getn()endl; cout循環(huán)結(jié)束endl; coutmain結(jié)束endl;static test b(3);bi=0時(shí):34循環(huán)開(kāi)始結(jié)構(gòu):3b=4i=0時(shí):i=1時(shí):i=1時(shí):5b=5循環(huán)結(jié)束main結(jié)束析構(gòu)526第26頁(yè)第26頁(yè)void main()cout循環(huán)開(kāi)始endl; for(int i=0;i2;i+) couti=i時(shí):
18、endl; test b(3); b.inc(); coutb.n=“ b.getn()endl; cout循環(huán)結(jié)束endl; coutmain結(jié)束endl;void main()cout循環(huán)開(kāi)始endl; for(int i=0;i2;i+) couti=i時(shí):endl; static test b(3); b.inc(); coutb.n=“ b.getn()endl; cout循環(huán)結(jié)束endl; coutmain結(jié)束endl;27第27頁(yè)第27頁(yè)注:靜態(tài)對(duì)象結(jié)構(gòu)函數(shù)與析構(gòu)函數(shù)調(diào)特點(diǎn):1、結(jié)構(gòu)函數(shù)在代碼執(zhí)行過(guò)程中,第一次碰到它變量定義時(shí)被調(diào)用,但直到整個(gè)程序結(jié)束之前僅調(diào)用一次。2、析構(gòu)函數(shù)
19、在整個(gè)程序退出之前被調(diào)用,同樣也只調(diào)用一次。即:碰到定義則調(diào)用結(jié)構(gòu)函數(shù), 程序結(jié)束調(diào)用析構(gòu)函數(shù)結(jié)構(gòu)函數(shù)只調(diào)用一次, 析構(gòu)函數(shù)也只調(diào)用一次28第28頁(yè)第28頁(yè)課程回顧2一個(gè)const對(duì)象只能訪問(wèn) 組員函數(shù)。3.設(shè)類Test中存在組員static int x,則下列哪種初始化方式是正確( )A.Test:int x=25;B.int x=25;C.int Test:x=25;D.int Test x=25;constC4.類A中存在公有靜態(tài)數(shù)據(jù)組員x,設(shè)a和b是類A兩個(gè)對(duì)象,在執(zhí)行a.x=10之后,b.x 值為( ) A.未初始化 B.等于a.x C.等于0 D.隨機(jī) B1.若類組員函數(shù)用關(guān)鍵字s
20、tatic進(jìn)行修飾,這樣組員函數(shù)稱為 。靜態(tài)組員函數(shù)29第29頁(yè)第29頁(yè) 5.關(guān)于類靜態(tài)組員函數(shù)描述錯(cuò)誤是( ) A.在創(chuàng)建對(duì)象前不存在 B.能夠被非靜態(tài)組員函數(shù)訪問(wèn) C.能夠直接被靜態(tài)組員函數(shù)訪問(wèn) D.不是對(duì)象組員A30第30頁(yè)第30頁(yè)6.找出下列程序中錯(cuò)誤,并闡明原因#include class test private:int x; public:test(int a) x=a; void set(int a)x=a; void get()coutxendl; main() const test a(3); a.set(5); a.get();不能改變常對(duì)象數(shù)據(jù)組員值31第31頁(yè)第31頁(yè)
21、5.3友元函數(shù) 友元提供應(yīng)了不同類或者對(duì)象組員函數(shù)之間、類組員函數(shù)與普通函數(shù)之間進(jìn)行數(shù)據(jù)共享機(jī)制。也就是說(shuō),經(jīng)過(guò)友元,一個(gè)普通函數(shù)或者類組員函數(shù)能夠訪問(wèn)到封裝于另一個(gè)類中數(shù)據(jù),這相稱于給類封裝開(kāi)了一個(gè)小孔,經(jīng)過(guò)它能夠看到類內(nèi)部一些屬性。 假如友元是普通函數(shù)或者類組員函數(shù),則稱為友元函數(shù)。友元函數(shù)是擁有組員函數(shù)一切權(quán)利非組員函數(shù)。也就是說(shuō),友元函數(shù)不是類組員函數(shù),但能夠像組員函數(shù)同樣直接訪問(wèn)類私有組員。 假如友元是一個(gè)類,則稱為友元類,友元類所有組員函數(shù)都為友元函數(shù)。32第32頁(yè)第32頁(yè)友元三種形式:1、普通函數(shù)作一個(gè)類友元2、將a類組員函數(shù)作b類友元3、將一個(gè)類a作為另一個(gè)類b友元33第33頁(yè)
22、第33頁(yè)一個(gè)友元類/函數(shù)/組員函數(shù) 能夠通過(guò)對(duì)象使用另一個(gè)類私有組員。友元函數(shù)能夠訪問(wèn) 對(duì)象私有組員,公有組員和保護(hù)組員。友元能夠是一個(gè)類或函數(shù)。友元需通過(guò)對(duì)象、對(duì)象引用、對(duì)象指針來(lái)使用類組員。34第34頁(yè)第34頁(yè)在類外對(duì)fun函數(shù)進(jìn)行定義, 此時(shí)fun函數(shù)不是類組員函數(shù)友元申明能夠在類private和public部分普通友元函數(shù)能夠通過(guò)對(duì)象、對(duì)象引用、對(duì)象指針使用對(duì)象私有組員。一、普通函數(shù)作一個(gè)類友元在類體內(nèi)用friend對(duì)普通函數(shù)fun進(jìn)行申明, 則fun成為類友元函數(shù)。35第35頁(yè)第35頁(yè)在類體內(nèi)需要對(duì)友元函數(shù)進(jìn)行申明class pointdouble x,y;public:point(
23、double xi,double yi) x=xi; y=yi; friend double dist(point &p1,point &p2);double dist(point &p1,point &p2)double dx=p1.x-p2.x;double dy=p1.y-p2.y;return sqrt(dx*dx+dy*dy);在類中申明,但不是組員函數(shù)point &p1,point &p2友元函數(shù)不是類組員函數(shù),沒(méi)有this指針,因此必須用對(duì)象或者引用或者對(duì)象指針做參數(shù)。point:dist( point &p1,point &p2 )36第36頁(yè)第36頁(yè)class pointdo
24、uble x,y;public:point(double m,double n) x=m; y=n; friend double dist(point &p1,point &p2);double dist(point & p1,point & p2)double dx=p1.x-p2.x;double dy=p1.y-p2.y;return sqrt(dx*dx+dy*dy);void main()point p1(1.1,2.2),p2(3.3,4.4);cout dist(p1,p2)endl; 37第37頁(yè)第37頁(yè)引用做形參和對(duì)象做形參區(qū)別是什么?對(duì)象做形參,形參對(duì)象 內(nèi)存, 會(huì)引起 函
25、數(shù)調(diào)用 引用做形參,形參 內(nèi)存, 復(fù)制結(jié)構(gòu)函數(shù)對(duì)象做形參,形參改變 引起實(shí)參改變。引用做形參,形參改變 引起實(shí)參改變指針做形參也可實(shí)現(xiàn),注意其形式什么?復(fù)制結(jié)構(gòu)不分派分派不調(diào)用會(huì)不會(huì)38第38頁(yè)第38頁(yè)class pointdouble x,y;public:point(double m,double n) x=m; y=n; friend double dist(point &p1,point &p2);double dist(point & p1,point & p2)double dx=p1 . x-p2 . x;double dy=p1 . y-p2 . y;return sqrt(d
26、x*dx+dy*dy);void main()point p1(1.1,2.2),p2(3.3,4.4);cout dist( p1, p2)*-&39第39頁(yè)第39頁(yè)假定f()是類A中組員函數(shù)能夠在類B中申明 將類A組員函數(shù)f()申明為類B友元申明函數(shù)f時(shí)需限定該函數(shù)f是類A組員函數(shù)并在f中訪問(wèn)B組員時(shí),需通過(guò)對(duì)象、引用、指針來(lái)進(jìn)行訪問(wèn)。二、A類組員函數(shù)作B類友元class A void f(B &);;class B friend void A:f(B &); ;將A類組員函數(shù)f闡明為該類友元40第40頁(yè)第40頁(yè)class Two;class Oneint x;public:One(int
27、 a):x(a);int Getx()return x;void func( Two &);/ 不完全類申明(Two) ,以便在類One中引用Two直接使用類Two私有組員class Twoint y;public:Two(int b):y(b)int Gety()return y;friend void One:func(Two &);void One:func(Two & r)r.y=x;錯(cuò)誤:不完全申明類不能實(shí)例化。不能存取沒(méi)有完全申明類組員。41第41頁(yè)第41頁(yè)#include using namespace std;class Two;class Oneint x;public:On
28、e(int a):x(a);int Getx()return x;void func( Two &);class Two int y;public: Two(int b):y(b) int Gety()return y; friend void One:func(Two &);void One:func(Two & r)r.y=x;void main()One obj1(5);Two obj2(8);coutobj1.Getx(), obj2.Gety()endl;obj1.func(obj2);cout調(diào)用友元組員函數(shù)后: endl;coutobj1.Getx(), obj2.Gety()e
29、ndl;5,8調(diào)用友元組員函數(shù)后:5,542第42頁(yè)第42頁(yè)在類B中申明類A為B友元格式:class B friend class A 二、將一個(gè)類A闡明為另一類B友元注:假如類A為類B友元,則類A中所有組員函數(shù)均居于友元函數(shù)功效。43第43頁(yè)第43頁(yè)class Two;class Oneint x;public:One(int a):x(a);int Getx()return x;void func( Two &);class Twoint y;public:Two(int b):y(b)int Gety()return y; void One:func(Two & r)r.y=x;frie
30、nd void One:func(Two &);friend class One;思考:此申明能不能省略?思考:func函數(shù)定義可否放在類Two定義前?不能不能void main()One obj1(5);Two obj2(8);coutobj1.Getx(), obj2.Gety()endl;obj1.func(obj2);cout調(diào)用友元組員函數(shù)后: endl;coutobj1.Getx(), obj2.Gety()endl;44第44頁(yè)第44頁(yè)總結(jié):1、友元申明與訪問(wèn)控制無(wú)關(guān)。 在private和public后申明效果相同。2、友元關(guān)系是不能傳遞。 若A是B友元,B是C友元, A不能自動(dòng)
31、稱為C友元。注意:此時(shí)哪個(gè)類能訪問(wèn)哪個(gè)類組員?3、友元關(guān)系是單向。 若A是B友元,B不一定是A友元。 45第45頁(yè)第45頁(yè)友元三種形式:1,普通函數(shù)作一個(gè)類友元2,a類組員函數(shù)作b類友元3,a類作為b類友元課程回顧46第46頁(yè)第46頁(yè)1.下列關(guān)于友元描述錯(cuò)誤是( )A.組員函數(shù)不可作友元B.類能夠作友元C.普通函數(shù)能夠作友元D.靜態(tài)函數(shù)能夠作友元2.友元函數(shù)能夠存取類_、公有組員和保護(hù)組員。3.在C+中,即使友元提供了類之間數(shù)據(jù)進(jìn)行訪問(wèn)一個(gè)方式,但它破壞了面向?qū)ο蟪绦蛟O(shè)計(jì)_特性。4.假如類A被申明成類B友元,則( ) A.類A組員即類B組員 B.類B組員即類A組員C.類A組員函數(shù)不得訪問(wèn)類B組
32、員 D.類B不一定是類A友元A私有組員封裝D47第47頁(yè)第47頁(yè)5.對(duì)于友元描述正確是( )A友元是本類組員函數(shù)B友元不是本類組員函數(shù)C友元不是函數(shù)D友元不能訪問(wèn)本類私有組員B48第48頁(yè)第48頁(yè)6、給岀下列程序執(zhí)行結(jié)果。#include class sampleint n;public:sample(int i)n=i;friend int add(sample &s1,sample &s2);int add(sample &s1,sample &s2)return s1.n+s2.n;void main()sample s1(10),s2(20);coutadd(s1,s2)endl;49
33、第49頁(yè)第49頁(yè)7、先找出程序中錯(cuò)誤,更正后分析程序執(zhí)行結(jié)果:#include class B;class Aint i;friend B;void disp()coutiendl;class Bpublic:void set(int n)A a; a.i=n;a.disp();void main()B b;b.set(2);50第50頁(yè)第50頁(yè)作業(yè)書本126頁(yè),編程題第1題51第51頁(yè)第51頁(yè)const變量定義時(shí)需要初始化。const int x=0;const int *p=&x;int * const p=&x;const int * const p=&x;5.4const對(duì)象const
34、可限定變量、指針、對(duì)象、函數(shù)、函數(shù)參數(shù)、數(shù)據(jù)組員、組員函數(shù)。表示不可改變。52第52頁(yè)第52頁(yè)常量組員(const組員 ) 包括:常量數(shù)據(jù)組員(const數(shù)據(jù)組員)常引用 (const限定引用)靜態(tài)常數(shù)據(jù)組員(static const 數(shù)據(jù)組員)一、常量組員尤其注意:1)靜態(tài)常數(shù)據(jù)組員仍保留靜態(tài)組員特性,需要在類外初始化。2)常量數(shù)據(jù)組員和常引用組員必須在結(jié)構(gòu)函數(shù)初始化列表中進(jìn)行初始化。53第53頁(yè)第53頁(yè)#include (lt5_7.cpp)using namespace std;class Baseint x;const int a; /常數(shù)據(jù)組員static const int b;
35、/ 靜態(tài)常數(shù)據(jù)組員const int &r;/常引用public: Base(int,int); void show()coutx,a,b endl; void display(const double &r )coutrendl;const int Base:b=125;Base:Base(int i,int j):x(i),a(j) void main()Base a1(104,118),a2(119,140);a1.show();a2.show();a1.display(3.14159);先找出程序中錯(cuò)誤并更正,然后分析程序結(jié)果。常數(shù)據(jù)組員必須通過(guò)初始化列表來(lái)取得初值,r(x)常數(shù)據(jù)組員
36、和常引用都必須通過(guò)初始化列表來(lái)取得初值,r endl;54第54頁(yè)第54頁(yè)#include using namespace std;class Baseint x;const int a;static const int b;const int &r;public: Base(int,int); void show()coutx,a,b ,r endl; void display(const double &ref )coutrefendl;const int Base:b=125;Base:Base(int i,int j):x(i),a(j) ,r(x)void main()Base a1
37、(104,118),a2(119,140); double x=3.14159;a1.show();a2.show();a1.display(x); 常引用做函數(shù)參數(shù),只是把實(shí)參值提供應(yīng)函數(shù)使用,不允許函數(shù)改變對(duì)象值。55第55頁(yè)第55頁(yè)引用作形參特點(diǎn):形參改變會(huì)使得實(shí)參改變,引用作形參,形參不分派內(nèi)存有時(shí)需要使用引用作形參但要求形參不能改變此時(shí)可使用const限定二、const引用作參數(shù)56第56頁(yè)第56頁(yè)對(duì)象申明前加上const限定,普通定義格式為: 類名 const 對(duì)象名;或者 const 類型 對(duì)象名三、常對(duì)象(const對(duì)象)比如:base const a(25,68);const
38、 base b(21,32);注意:1)在定義常對(duì)象時(shí)必須進(jìn)行初始化。2)常對(duì)象數(shù)據(jù)組員不能被更新。57第57頁(yè)第57頁(yè)分析下列程序結(jié)果(lt5_7a.cpp)#include using namespace std;class baseint x,y;public:base() :x(0),y(0) base(int a,int b):x(a),y(b)void show()coutx,yendl;cout+x,+yendl;void main()base a;a.show();base b(10,20);b.show();58第58頁(yè)第58頁(yè)#include using namespace
39、 std;class baseint x,y;public:base() :x(0),y(0) base(int a,int b):x(a),y(b)void set(int a,int b)x=a;y=b; void show()coutx,yendl;void main()base a; a.show(); const base b(10,20);編譯錯(cuò)誤:不能改變常對(duì)象數(shù)據(jù)組員值找出程序中錯(cuò)誤,并闡明原因a.set(1,2);b.set(1,2);b.show();思考:如何顯示b數(shù)據(jù)組員?編譯錯(cuò)誤:常對(duì)象只能調(diào)用常組員函數(shù)。59第59頁(yè)第59頁(yè)四、常組員函數(shù)使用const關(guān)鍵詞申明函數(shù)
40、為常組員函數(shù),申明格式下列:類型 函數(shù)名(參數(shù)表)const;注:1)const是函數(shù)闡明一個(gè)構(gòu)成部分,因此在定義部分也要帶const關(guān)鍵詞。2)常組員函數(shù)不能更新對(duì)象數(shù)據(jù)組員,也不能調(diào)用該類中沒(méi)有用const修飾組員函數(shù)。3)一個(gè)const對(duì)象能夠調(diào)用const函數(shù),但不能調(diào)用非const組員函數(shù)。4) const限定函數(shù)與未加const限定同名函數(shù)可重載。5) const不可限定析構(gòu)函數(shù)和結(jié)構(gòu)函數(shù)。60第60頁(yè)第60頁(yè)#include /lt5_7c.cppusing namespace std;class baseint x,y;public:base():x(0),y(0)base(i
41、nt a,int b):x(a),y(b)void set(int a,int b)x=a;y=b; void show() coutx,yendl; cout +x,+yendl;void main()base a; a.show(); const base b(10,20); /常對(duì)象 b.show();const常組員函數(shù)編譯錯(cuò)誤:常對(duì)象只能調(diào)用常組員函數(shù)!錯(cuò)誤:在常組員函數(shù)中,不能更新對(duì)象數(shù)據(jù)組員61第61頁(yè)第61頁(yè)#include (lt5_7d.cpp)using namespace std;class baseint x,y;public:base():x(0),y(0)base
42、(int a,int b):x(a),y(b)void set(int a,int b)x=a;y=b; void show() const /const組員函數(shù)coutx,yendl; void show() /非const組員函數(shù)coutx,yendl; cout+x,+yendl;void main()base a; a.show(); const base b(10,20); b.show();const限定函數(shù)與未加const限定同名函數(shù)可重載。const組員函數(shù)與同名非const組員函數(shù)可重載。62第62頁(yè)第62頁(yè)練習(xí):找出程序中錯(cuò)誤并更正,然后分析程序結(jié)果(lt5_8.cpp)#
43、include using namespace std;class basedouble x,y;const double p;public:base(double m,double n,double d):p(d)x=m;y=n;void show();void show()const;void base:show()coutx,y,p=pendl;void base:show() constcoutx,y,const p=pendl;void main()base a(8.9,2.5,3.1416);const base b(2.5,8.9,3.14);a.show();b.show();
44、63第63頁(yè)第63頁(yè)總結(jié):1,const數(shù)據(jù)組員必須在結(jié)構(gòu)函數(shù)初始化列表中進(jìn)行初始化。2,const組員函數(shù)內(nèi)不可出現(xiàn)任何改變變量值語(yǔ)句。3,const對(duì)象只能使用const組員函數(shù),不同對(duì)象能夠使用全部組員函數(shù)。4, const引用做參數(shù)時(shí),函數(shù)體內(nèi)不可出現(xiàn)改變參數(shù)值語(yǔ)句。64第64頁(yè)第64頁(yè)比如:假設(shè)已定義point類。則point p3;表示申明了一個(gè)含有3個(gè)元素?cái)?shù)組p,其中每一個(gè)元素都是point類一個(gè)對(duì)象。5.5 數(shù)組和類類可定義對(duì)象數(shù)組和對(duì)象指針數(shù)組。一、對(duì)象數(shù)組對(duì)象數(shù)組是指元素都是對(duì)象數(shù)組。也就是說(shuō),假如某一個(gè)類有若干個(gè)對(duì)象,則能夠把這一系列被創(chuàng)建對(duì)象用一個(gè)數(shù)組來(lái)存儲(chǔ)。申明格式:
45、類名 數(shù)組名對(duì)象個(gè)數(shù)65第65頁(yè)第65頁(yè)分析下列程序結(jié)果(lt5_10.cpp)class pointint x,y;public:point():x(0),y(0)point(int a):x(a)y=0;point(int a,int b):x(a),y(b);int getx()return x;int gety()return y;void main()point a3; for(int i=0;i3;i+) coutai:ai.getx(),ai.gety()endl; point b3=1,2,3; for( i=0;i3;i+) coutbi:bi.getx(),bi.gety(
46、)endl; point c3=point(1,2),point(3,4),point(5,6); for( i=0;i3;i+) coutci:ci.getx(),ci.gety()endl;/每個(gè)元素調(diào)用無(wú)參結(jié)構(gòu)函數(shù)/每個(gè)元素調(diào)用1個(gè)參數(shù)結(jié)構(gòu)函數(shù)/每個(gè)元素調(diào)用2個(gè)參數(shù)結(jié)構(gòu)函數(shù)編譯器會(huì)調(diào)用適當(dāng)結(jié)構(gòu)函數(shù)來(lái)建立數(shù)組每一個(gè)分量66第66頁(yè)第66頁(yè)分析下列程序結(jié)果(lt5_10a.cpp)class pointint x,y;public:point(int a,int b):x(a),y(b);int getx()return x;int gety()return y;void main()poi
47、nt a3=point(1,2),point(3,4),point(5,6); point *p=a; /用指針指向數(shù)組元素 for(int i=0;i3;i+) coutai:ai.getx(),ai.gety()getx()(p+i)-gety()67第67頁(yè)第67頁(yè)二、類對(duì)象指針數(shù)組int *p; 表示定義一個(gè)指針變量int p5;表示定義一個(gè)數(shù)組。int *p5 表示定義一個(gè)有5個(gè)元素?cái)?shù)組,其中每個(gè)元素都 是一個(gè)指針。這類數(shù)組稱為指針數(shù)組。若類point已經(jīng)正擬定義下列:class pointint x,y;public:point():x(0),y(0)point(int a):x(
48、a)y=0;point(int a,int b):x(a),y(b);int getx()return x;int gety()return y;則:point a(1,2);point *p;p=&a; /指向已有對(duì)象p=new point;p=new point(1,2);能夠直接用動(dòng)態(tài)分派對(duì)象初始化68第68頁(yè)第68頁(yè)則若定義對(duì)象指針數(shù)組:point *p3;p0=new point(1,2);p1=new point(3,4);p2=new point(5,6);若類point已經(jīng)正擬定義下列:class pointint x,y;public:point():x(0),y(0)poi
49、nt(int a):x(a)y=0;point(int a,int b):x(a),y(b);int getx()return x;int gety()return y;思考:假如輸出p0所指對(duì)象組員?coutgetx(),” gety()endl;可通過(guò)循環(huán)使用pi指向?qū)ο?。思考:假如輸出每個(gè)指針?biāo)笇?duì)象組員?coutgetx(),” gety()endl;for(i=0;i3;i+)69第69頁(yè)第69頁(yè)練習(xí):分析下列程序結(jié)果(lt5_11.cpp):#include using namespace std;class pointint x,y;public:point():x(0),y(0
50、)point(int a):x(a)y=0;point(int a,int b):x(a),y(b);int getx()return x;int gety()return y;void main()point *p3=new point(1,2),new point(3,4),new point(5,6); for(int i=0;i3;i+) coutpi:getx(),gety()endl;思考:該程序是否有不合理 地方?for( i=0;ib?a:b;void main()int (*p)(int ,int );p=max; cout max(10,20) b?a:b;定義一個(gè)指向類A
51、組員函數(shù)指針定義形式:類型 (類名: *指針名)(參數(shù)類型);比如:int (A : *p)(int ,int );函數(shù)指針指向類組員函數(shù)語(yǔ)句:指針名=類名:函數(shù)名比如:p=A:max;通過(guò)p調(diào)用max語(yǔ)句 (對(duì)象. *函數(shù)指針)(參數(shù))73第73頁(yè)第73頁(yè)例題:按要求完畢程序填空,并分析程序結(jié)果(lt5_12.cpp)#include using namespace std;class Aint n;public:A(int i):n(i)int add(int a)return n+a; ;void main() int (A:*p)(int); A x(10); p=A:add; cou
52、t(x.*p)(15)endl; A *q=&x; cout*p)(15)endl;定義一個(gè)指向A類組員函數(shù)指針,該組員函數(shù)返回值為int型,有一個(gè)int型參數(shù)。使p指向組員函數(shù)add定義一個(gè)指針變量q并使其指向?qū)ο髕74第74頁(yè)第74頁(yè)練習(xí):給出下列程序運(yùn)營(yíng)結(jié)果#include class samplepublic:int x;int y;void disp()coutx=x,y=yendl;void main()int sample:*pc;void (sample:*qc)();sample s;pc=&sample:x;s.*pc=10;pc=&sample:y;s.*pc=20;qc
53、=sample:disp;s.disp();/(s.*qc)();75第75頁(yè)第75頁(yè)5.7求解一元二次方程1、建立項(xiàng)目:equation2、建立頭文獻(xiàn)equation.h 該頭文獻(xiàn)中申明Findroot類,并包括程序需要頭文獻(xiàn)3、建立eqution.cpp文獻(xiàn),實(shí)現(xiàn)Findroot類,并包括equation頭文獻(xiàn)4、建立find.cpp文獻(xiàn),設(shè)計(jì)主函數(shù),輸入方程系數(shù),并求解。包括equation.h頭文獻(xiàn)5、編譯、連接、運(yùn)營(yíng)76第76頁(yè)第76頁(yè)2、建立頭文獻(xiàn)equation.h 該頭文獻(xiàn)中申明Findroot類,并包括程序需要頭文獻(xiàn)#include #include using namesp
54、ace std;class Findrootfloat a,b,c,d;double x1,x2;public:Findroot(float x,float y,float z);void set(float,float,float);void find();void show();77第77頁(yè)第77頁(yè)3、建立eqution.cpp文獻(xiàn),實(shí)現(xiàn)Findroot類,并包括equation頭文獻(xiàn)#include equation.hFindroot:Findroot (float x,float y,float z):d(b*b-4*a*c)a=x;b=y;c=z;void Findroot:set
55、 (float x,float y,float z)a=x;b=y;c=z;void Findroot:find()if(d0)x1=(-b-sqrt(d)/(2*a);x2=(-b+sqrt(d)/(2*a);else if(d=0) x1=x2=(-b)/(2*a);elsex1=(-b)/(2*a);x2=sqrt(-d)/(2*a);void Findroot:show()cout方程ax2+bx+c=0根為:0) coutx1=x1,x2=x2endl;else if(d=0) coutx1=x2=x1endl;else coutx1=x1+x2iendl;coutx1=x1-x2i
56、endl;78第78頁(yè)第78頁(yè)4、建立find.cpp文獻(xiàn),設(shè)計(jì)主函數(shù),輸入方程系數(shù),并求解。包括equation.h頭文獻(xiàn)#include equation.hvoid read(float &,float &,float&);void main()float a,b,c;cout這是一個(gè)求一元二次方程跟程序:endl;doread(a,b,c);if(a=0) break;Findroot obj(a,b,c);obj.find();obj.show();while(a!=0);void read(float &a,float &b,float &c)cout請(qǐng)輸入方程系數(shù):endl;co
57、uta;if(a=0) return ;coutb;coutc;79第79頁(yè)第79頁(yè)作業(yè)書本125頁(yè):改錯(cuò)題第2題 完畢程序題 編程題第3題書本126頁(yè):編程題第3題80第80頁(yè)第80頁(yè)結(jié)構(gòu)函數(shù)定義:class A int apublic: A(int x):a(x) A(int x)a=x;const限定數(shù)據(jù)組員初始化必須用?形式?尚有什么數(shù)據(jù)組員必須用此種形式初始化?const限定數(shù)據(jù)組員和引用數(shù)據(jù)組員初始化必須使用初始化列表形式。第五章復(fù)習(xí)81第81頁(yè)第81頁(yè)靜態(tài)組員 不依賴于對(duì)象而存在。靜態(tài)數(shù)據(jù)組員:定義時(shí)加static靜態(tài)數(shù)據(jù)組員必須初始化使用語(yǔ)句:int A:b=0;靜態(tài)對(duì)象:st
58、atic對(duì)象一旦定義存在于內(nèi)存中,直到程序結(jié)束才釋放內(nèi)存。82第82頁(yè)第82頁(yè)靜態(tài)組員 不依賴于對(duì)象而存在。靜態(tài)組員使用注意:非靜態(tài)對(duì)象 靜態(tài)組員 可否使用?靜態(tài)對(duì)象 靜態(tài)組員 可否使用?靜態(tài)對(duì)象 非靜態(tài)組員 可否使用?靜態(tài)組員函數(shù) 非靜態(tài)組員 可否使用?非靜態(tài)組員函數(shù) 靜態(tài)組員 可否使用?通過(guò)類如何使用靜態(tài)公有組員?83第83頁(yè)第83頁(yè)靜態(tài)組員 不依賴于對(duì)象而存在。其它注意事項(xiàng): p108 1-7靜態(tài)函數(shù)沒(méi)有this指針不能申明為virtual函數(shù)84第84頁(yè)第84頁(yè)友元普通函數(shù)作友元類組員函數(shù)作友元類作友元類A友元(函數(shù),組員函數(shù),類)能夠訪問(wèn)類A中所有組員,但必須通過(guò)對(duì)象引用指針來(lái)訪問(wèn)。
59、友元應(yīng)在類A中利用friend申明85第85頁(yè)第85頁(yè)const對(duì)象const數(shù)據(jù)組員const組員函數(shù)const指針const變量const引用加const限定后不可改變,任何企圖改變都造成錯(cuò)誤,并應(yīng)注意他們初始化!86第86頁(yè)第86頁(yè)使用規(guī)則:const對(duì)象 不能調(diào)用任何非const組員函數(shù)const組員函數(shù)只能調(diào)用const組員函數(shù)非const對(duì)象能夠調(diào)用const組員函數(shù)非const對(duì)象能夠使用const數(shù)據(jù)組員const組員函數(shù)中不能使用任何試圖改變變量值語(yǔ)句。const數(shù)據(jù)組員必須在結(jié)構(gòu)函數(shù)初始化列表中進(jìn)行初始化87第87頁(yè)第87頁(yè)數(shù)組和類類能夠定義對(duì)象數(shù)組對(duì)象數(shù)組中每個(gè)元素都是對(duì)象,其使用方式和對(duì)象使用方式相同。對(duì)象數(shù)組初始化形式: 類名 數(shù)組名=類名(參數(shù)).;對(duì)象數(shù)組名表示數(shù)組首地址能夠定義指向?qū)ο笾羔榩使其指向某個(gè)對(duì)象能夠用指針p指向數(shù)組中元素88第88頁(yè)第88頁(yè)定義指針數(shù)組int *p5;表示該數(shù)組中5個(gè)元素均為指針。函數(shù)指針定義形式:int (*p)(int,int);int (A:*P)(int,int);int max(int a,int b) int A:max(int a,int b) 89第89頁(yè)第89頁(yè)習(xí)題五-190第90頁(yè)第90頁(yè)習(xí)題五-191第91頁(yè)第91頁(yè)習(xí)題五-192第92頁(yè)第92頁(yè)習(xí)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- Photoshop平面設(shè)計(jì)基礎(chǔ) 課件 任務(wù)5.2 制作餅干宣傳海報(bào)
- 肉羊飼料定制銷售協(xié)議
- 房屋租賃合同標(biāo)準(zhǔn)化管理規(guī)范文本范本精裝修家具家電
- 成都臨街商鋪?zhàn)赓U及品牌宣傳合作合同
- 離婚協(xié)議書范本涉及知識(shí)產(chǎn)權(quán)分割
- 保安員整改方案
- 產(chǎn)業(yè)園區(qū)場(chǎng)地調(diào)研合作協(xié)議
- 餐飲企業(yè)租賃及品牌授權(quán)合同
- 店面承重柱改造方案
- 寧夏農(nóng)學(xué)面試題及答案
- 預(yù)防住院患者非計(jì)劃性拔管的集束化護(hù)理措施課件
- 云南省保山市2024-2025學(xué)年高一上學(xué)期期末考試 地理 含解析
- (高清版)DB11∕T2274-2024水務(wù)工程施工現(xiàn)場(chǎng)安全生產(chǎn)管理導(dǎo)則
- ISO 37001-2025 反賄賂管理體系要求及使用指南(中文版-雷澤佳譯-2025)
- GB/T 45133-2025氣體分析混合氣體組成的測(cè)定基于單點(diǎn)和兩點(diǎn)校準(zhǔn)的比較法
- 2025年1月國(guó)家開(kāi)放大學(xué)行管??啤缎姓M織學(xué)》期末紙質(zhì)考試試題及答案
- 《種衣劑知識(shí)培訓(xùn)》課件
- 經(jīng)典名方小陷胸湯的古今文獻(xiàn)考證
- 2025屆湖北省路橋集團(tuán)限公司校園招聘190人易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 人教版八年級(jí)英語(yǔ)上冊(cè) Unit 2 Section A 教案(同步教學(xué)設(shè)計(jì))
- 臨床輸血護(hù)理指南
評(píng)論
0/150
提交評(píng)論