內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)6-靜態(tài)成員和友元(共19頁)_第1頁
內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)6-靜態(tài)成員和友元(共19頁)_第2頁
內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)6-靜態(tài)成員和友元(共19頁)_第3頁
內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)6-靜態(tài)成員和友元(共19頁)_第4頁
內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)6-靜態(tài)成員和友元(共19頁)_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上內(nèi)蒙古科技大學(xué)面向?qū)ο蟮某绦蛟O(shè)計(jì)實(shí)驗(yàn)報(bào)告  課程名稱:面向?qū)ο蟮某绦蛟O(shè)計(jì)姓名:陳偉光實(shí)驗(yàn)名稱:靜態(tài)成員和友元學(xué)號:任課教師:劉月峰專業(yè):軟件工程指導(dǎo)教師:劉月峰班級:15-軟件一班  一、實(shí)驗(yàn)?zāi)康?#160; 1理解靜態(tài)成員(靜態(tài)數(shù)據(jù)成員、靜態(tài)成員函數(shù))的作用與使用;2理解友元(友元函數(shù)、友元類)的作用于使用。二、實(shí)驗(yàn)環(huán)境編譯器:Visual C+ 6.0.操作系統(tǒng):Windows 7 旗艦版 三、實(shí)驗(yàn)內(nèi)容 二、實(shí)驗(yàn)內(nèi)容2.1練習(xí)(一):1理解下面的程序,并在VC+6.0下運(yùn)行查看結(jié)果,回

2、答程序后面的問題。#include <iostream.h>#include <string.h>class CStudentpublic:CStudent(char *n, int a);CStudent();static void SetAge(int age);private:char *name;int age;static int nTotalObj;int CStudent:nTotalObj = 0;CStudent:CStudent(char *n, int a):age(a)int nLen = strlen(n);name = new charnLe

3、n+1;strcpy(name,n);namenLen = 0;nTotalObj+;CStudent:CStudent()delete name;nTotalObj-;void CStudent:SetAge(int age)this->age = age;void main()CStudent stu1("張三",25);CStudent str2("李四",26);cout<<"CStudent:nTotalObj="<<CStudent:nTotalObj<<endl;問題一:以上程序

4、編譯能通過嗎,為什么?問題二:成員變量nTotalObj在程序中起什么作用,它是如何實(shí)現(xiàn)的?問題三:如果規(guī)定該程序的主函數(shù)和類CStudent中的成員變量的屬性不允許改變,應(yīng)該如何改正該程序?2理解下面的程序,并在VC+6.0下運(yùn)行查看結(jié)果,回答程序后面的問題。#include <iostream.h>#include <string.h>class CStudentpublic:CStudent(char *n, int a);CStudent();private:char *name;int age;CStudent:CStudent(char *n, int a)

5、:age(a)int nLen = strlen(n);name = new charnLen+1;strcpy(name,n);namenLen = 0;CStudent:CStudent()delete name;class CTeacherpublic:CTeacher(char *tn, int ta);CTeacher();void SetStuAge(int a); private:char *name;int age;CStudent stu;CTeacher:CTeacher(char *tn, int ta):age(ta)int nLen = strlen(tn)

6、;name = new charnLen+1;strcpy(name,tn);namenLen = 0;CTeacher:CTeacher()delete name;void CTeacher:SetStuAge(int a)stu.age = a;void main()CStudent stu("張三",25);CTeacher tea("李四",26);問題一:以上程序有兩大錯(cuò)誤,試指出來,并改正之?2.2練習(xí)(二):1某商店經(jīng)銷一種貨物。貨物成箱購進(jìn),成箱賣出,購進(jìn)和賣出時(shí)以重量為單位,各箱的重量不一樣,因此,商店需要記錄下貨物庫存的總重量。試用C

7、+模擬商店貨物購進(jìn)和賣出的情況。(提示:將總重量定義為靜態(tài)成員)四、實(shí)驗(yàn)過程練習(xí)1 題目提供源代碼如下編譯結(jié)果如下分析之后得出結(jié)論首先靜態(tài)成員函數(shù)SetAge中不可以使用指針,也不可以直接使用非靜態(tài)數(shù)據(jù)成員age 此處應(yīng)該使用對象名或者類名間接引用age 其次nTotalObj為私有屬性,不可以直接訪問,且題目要求不可以修改其屬性,則添加一個(gè)成員函數(shù),使其返回值等于nTotalObj,則代替了原先nTotalObj的功能 修改之后的代碼如下然后再次編譯運(yùn)行結(jié)果如下運(yùn)行結(jié)果正確,且不影響nTotalObj的計(jì)數(shù)器功能,代碼修改正確 練習(xí)2題目提供源代碼如下編譯之后結(jié)

8、果如下分析錯(cuò)誤原因有兩點(diǎn)錯(cuò)誤1 CTeacher類中的成員函數(shù)SetStuAge不能直接訪問CStudent類內(nèi)嵌成員stu的數(shù)據(jù)成員age錯(cuò)誤2 CTeacher類構(gòu)造函數(shù)的初始化含有錯(cuò)誤,內(nèi)嵌對象stu的數(shù)據(jù)成員也應(yīng)該包含在內(nèi) 修改之后代碼如下運(yùn)行結(jié)果如下成功運(yùn)行,修改正確 練習(xí)3根據(jù)題意,所編寫代碼如下嘗試測試運(yùn)行結(jié)果該代碼包含題目要求的所有功能,運(yùn)行完畢 五、實(shí)驗(yàn)總結(jié)實(shí)驗(yàn)源代碼:練習(xí)(一)  /修改之后代碼#include <iostream.h>#include <string.h>class CStudent

9、public:      CStudent(char *n, int a);      CStudent();      static void SetAge(CStudent stuN);      static int CSnTota10bj();         

10、           private:      char *name;      int age;      static int nTotalObj;int CStudent:nTotalObj = 0;CStudent:CStudent(char *n, int a):age(a)  

11、;    int nLen = strlen(n);      name = new charnLen+1;      strcpy(name,n);      namenLen = 0;      nTotalObj+;        &

12、#160;                           CStudent:CStudent()      delete name;      nTotalObj-; int CStudent:

13、CSnTota10bj()      int n;      n=nTotalObj;      return n; void CStudent:SetAge(CStudent stuN)      int a;      stuN.age = a;void main() 

14、60;    CStudent stu1("張三",25);      CStudent str2("李四",26);      cout<<"CStudent:nTotalObj="<<CStudent:CSnTota10bj()<<endl;練習(xí)(二) #include <iostream.h>#include &l

15、t;string.h>class CStudentpublic:      CStudent(char *n, int a);      CStudent();private:      char *name;public:      int age;CStudent:CStudent(char *n, int a):age(a)  

16、;    int nLen = strlen(n);      name = new charnLen+1;                             strcpy(name,n); &

17、#160;    namenLen = 0;CStudent:CStudent()      delete name;class CTeacherpublic:      CTeacher(char *tn,int ta,char *sn,int sa);      CTeacher();      void SetS

18、tuAge(int a);private:      char *name;      int age;      CStudent stu;                       &

19、#160;                  CTeacher:CTeacher(char *tn,int ta,char *sn="noname",int sa=0):age(ta),stu(sn,sa)      int nLen = strlen(tn);      nam

20、e = new charnLen+1;      strcpy(name,tn);      namenLen = 0;CTeacher:CTeacher()      delete name;void CTeacher:SetStuAge(int a)      stu.age = a;void main()    

21、  CStudent stu("張三",25);      CTeacher tea("李四",26);  /*錯(cuò)誤1 CTeacher類中的成員函數(shù)SetStuAge不能直接訪問CStudent類內(nèi)嵌成員stu的數(shù)據(jù)成員age錯(cuò)誤2 CTeacher類構(gòu)造函數(shù)的初始化含有錯(cuò)誤,內(nèi)嵌對象stu的數(shù)據(jù)成員也應(yīng)該包含在內(nèi)*/ 練習(xí)(三)  #include <iostream.h>class Goodsprivate:

22、    int weight;    static int totalWeight;public:    Goods(int wei);    static void PrintTotalWeight();    void sell();       void buy();int Goods:totalWeight = 0;Go

23、ods:Goods(int twei)    totalWeight = twei;       cout<<"初始總重量為:"<<totalWeight<<endl; void Goods:PrintTotalWeight()    cout<<"總重量為:"<<Goods:totalWeight<<endl;void Go

24、ods:buy()       cout<<"購入"<<endl;       cout<<"請輸入購入貨物的重量:"       cin>>weight;    Goods:totalWeight += weight;   

25、60;                     weight  = 0;void Goods:sell()       cout<<"售出"<<endl;       cout<&

26、lt;"請輸入售出貨物的重量:"       cin>>weight;    Goods:totalWeight -= weight;                           

27、                    weight  = 0;void main()       int twei=0;       int swit=1;       

28、;cout<<"請?jiān)O(shè)置初設(shè)總重量"<<endl;       cin>>twei;    Goods goods1(twei);       while(swit)                

29、0;                 cout<<"請選擇操作類型:0.退出  1.購入  2.售出"<<endl;              cin>>swit;  

30、60;            switch(swit)                                  case 0:co

31、ut<<endl;                     break;              case 1:goods1.buy();                            goods1.PrintTotalWeight();                  

溫馨提示

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

評論

0/150

提交評論