課堂習(xí)題講解二(5_6章)_第1頁(yè)
課堂習(xí)題講解二(5_6章)_第2頁(yè)
課堂習(xí)題講解二(5_6章)_第3頁(yè)
課堂習(xí)題講解二(5_6章)_第4頁(yè)
課堂習(xí)題講解二(5_6章)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、#include class Catpublic: Cat(int age=0):itsAge(age)HowManyCats+; Cat()HowManyCats-; int GetAge()return itsAge; void SteAge(int age)itsAge=age; static int GetHowMany()return HowManyCats;private: int itsAge; static int HowManyCats;int Cat:HowManyCats=0;void function()cout“There are ”Cat:GetHowMany()“

2、 cats alive! n“; 5-7 聲明一個(gè)Cat類,擁有靜態(tài)數(shù)據(jù)成員HowManyCats,記錄Cat的個(gè)體數(shù)目;聲明靜態(tài)成員函數(shù)GetHowMany(),存取HowManyCats。測(cè)試這個(gè)類。void main()const int MaxCats=5;Cat *CatHouseMaxCats; int i;for(i=0;iMaxCats;i+) CatHousei=new Cat(i); function();for(i=0;iMaxCats;i+) cout. GetHowMany()endl; delete CatHousei; function();int main()C

3、at c1(2);c1.print();coutc1.getNumOfCats()endl;Cat c2;c2.print();coutCat:getNumOfCats()endl;return 0;#include class X;class Y public: void g(X *); ;class Xprivate: int i; public: X()i=0; friend void h(X *); friend void Y:g(X *); friend class Z;void h(X *x)x-i+=10;coutii+; coutii+=5; cout(*x).iendl;5-

4、13解一:void main()X x;Z z;z.f(&x);Y y;y.g(&x);h(&x);5-13改錯(cuò):#include #ifndef MY_X_Y_Z_Hclass Xprivate: int i; public: X()i=0; friend void h(X *); friend void Y:g(X *); friend class Z; void h(X *x)x-i+=10;void Y:g(X *x)x-i+;class Y public: void g(X *); ;class Z public: void f(X *x)x-i+=5;#endifvoid main

5、() X x; Z z; Y y; coutz.f(*x)endl;couty.g(x)endl;couth(&x)endl;/! 先聲明后實(shí)現(xiàn)先聲明后實(shí)現(xiàn)/5-14 在習(xí)題基礎(chǔ)上加常成員函數(shù)在習(xí)題基礎(chǔ)上加常成員函數(shù)Out(),并用常對(duì)象進(jìn)行調(diào)用并用常對(duì)象進(jìn)行調(diào)用#include class Carprivate:int weight;public: Car(int j)weight=j; ;class Boatprivate:int weight; public:Boat(int j)weight=j;friend int totalWeight(Car &aCar,Boat &aBoat)

6、;int totalWeight(Car &aCar,Boat &aBoat)return aCar.weight+aBoat.weight; void main()Car c1(4); Boat b1(5);couttotalWeight(c1,b1)endl;void Out ( )const coutweightendl;const Car c2(66); c2.Out();class Boat;friend int totalWeight(Car &aCar,Boat &aBoat);/5-14 用友元函數(shù)重載求解,及改為成員函數(shù)求解用友元函數(shù)重載求解,及改為成員函數(shù)求解#includ

7、e static int a=0;class Carprivate:int weight;public: Car(int j)weight=j;friend int totalWeight(Car &aCar); class Boatprivate:int weight; public:Boat(int j)weight=j;friend int totalWeight(Boat &aBoat);int totalWeight(Car &aCar)return a+=aCar.weight; int totalWeight(Boat &aBoat)return a+=aBoat.weight;

8、 void main()Car c1(4); Boat b1(5); totalWeight(c1);couttotalWeight(b1)endl; int totalWeight();int totalWeight();int Car:totalWeight()return weight;int Boat:totalWeight()return weight;coutc1.totalWeight()+b1.totalWeight(); class Studentpublic: Student(int num,string name1,float score1); void modify(f

9、loat score1); void Print() const ; static void Output();private: int number; string name; float score; static int count; static float sum;補(bǔ)充題:三文件結(jié)構(gòu)補(bǔ)充題:三文件結(jié)構(gòu)/s.hStudent-number: int -name: string - score: float- count : int- sum: float+Student(int,string,float)+modify(score1: float ):void +Print(): vo

10、id + Output(): void#include#includeusing namespace std;#includes.hStudent:Student(int num,string name1,float score1) number=num; name=name1; score=score1; count+; sum+=score;void Student:modify(float score1) score=score1;void Student:Print() const coutnnumber:number name: name score:score;void Stude

11、nt:Output()coutn學(xué)生總數(shù)學(xué)生總數(shù):count 總分?jǐn)?shù)總分?jǐn)?shù): sumendl;int Student:count =0;float Student:sum=0;/s.cpp#includes.hvoid main() Student m1(2004001,96),m2; m1.Print(); m1.modify(90); m1.Print(); Student:Output(); /定義一個(gè)學(xué)生類對(duì)象數(shù)組定義一個(gè)學(xué)生類對(duì)象數(shù)組 Student ms3=Student(05071001,張三張三,76), Student(05071002,李四李四,87), Student(0

12、5071003,王五王五,90); for(int i=0;i3;i+)msi.Print(); ms1.modify(78); ms1.Print(); /main.cpp改:改:m1中實(shí)參中實(shí)參2 “2”沒(méi)有無(wú)參構(gòu)造函數(shù),不能聲明無(wú)參對(duì)象沒(méi)有無(wú)參構(gòu)造函數(shù),不能聲明無(wú)參對(duì)象 05071001 5071001#include /6-20 class SimpleCircleint *itsRadius;public:SimpleCircle(int i=5)itsRadius=new int(i); SimpleCircle(SimpleCircle &i) /itsRadius=i.itsR

13、adius; int j=i.GetRadius(); itsRadius=new int(j); SimpleCircle()delete itsRadius;void SetRadius(int i) *itsRadius=i;int GetRadius()constreturn *itsRadius;void main()SimpleCircle a,b(10),c(b);coutb b.GetRadius()endl;coutc c.GetRadius()endl; a.SetRadius(100); couta a.GetRadius()endl;itsRadius=&i;#incl

14、udevoid main() /int p5;/auto型型 int *p=new int5; /或或 int *p;p=new int5; /int *p=new int1,2,3,4,5; /語(yǔ)法錯(cuò)語(yǔ)法錯(cuò) /p =1,2,3,4,5; for (int i=0;i=4;i+) pi=i*i; cout*(p+i)“ “; coutendl; delete p;補(bǔ)充題補(bǔ)充題1:創(chuàng)建有:創(chuàng)建有5個(gè)元素的個(gè)元素的int型動(dòng)態(tài)數(shù)組型動(dòng)態(tài)數(shù)組p,對(duì)其賦值并對(duì)其賦值并輸出輸出, 釋放該數(shù)組釋放該數(shù)組.CPU:register靜態(tài)區(qū):extern static動(dòng)態(tài)堆區(qū):new,malloc動(dòng)態(tài)堆棧區(qū)au

15、toA- int X+A(int )+A(A &)+void get()輸出輸出this指針的值和指針的值和X#includeusing namespace std;class Apublic:A(int x1):x(x1) void A(A &a) this=a; void get()cout this=thisendl; cout x=xendl; cout x=xendl; private: int x;void main()A a(10),b(2),c(a);a.get();b.get ();c.get ();A(A &a) * this=a; 補(bǔ)充題補(bǔ)充題2:顯示:顯示this指針的值指針的值補(bǔ)充題補(bǔ)充題3:讀入三個(gè)浮點(diǎn)數(shù),將整數(shù)、小數(shù)部分分別輸出:讀入三個(gè)浮點(diǎn)數(shù),將整數(shù)、小數(shù)部分分別輸出#includevoid splitfloat(float x,c

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論