面向對象程序設計 C++山師 第六章習題答案

上傳人:xins****2008 文檔編號:141343454 上傳時間:2022-08-24 格式:DOC 頁數(shù):22 大小:92.50KB
收藏 版權申訴 舉報 下載
面向對象程序設計 C++山師 第六章習題答案_第1頁
第1頁 / 共22頁
面向對象程序設計 C++山師 第六章習題答案_第2頁
第2頁 / 共22頁
面向對象程序設計 C++山師 第六章習題答案_第3頁
第3頁 / 共22頁

下載文檔到電腦,查找使用更方便

20 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《面向對象程序設計 C++山師 第六章習題答案》由會員分享,可在線閱讀,更多相關《面向對象程序設計 C++山師 第六章習題答案(22頁珍藏版)》請在裝配圖網(wǎng)上搜索。

1、第六章習題答案一、選擇填空 1、A 2、C 3、D 4、B 5、D 6、A 7、C 8、A 9、D 10、A 11、C 12、A 13、B 14、C 15、C 16、D 17、B 18、C 19、A 20、D 21、C 22、B二、判斷下列描述的正確性,對者劃,錯者劃。 1、 2、 3、 4、 5、 6、 7、 8、 9、 10、 11、 12、 13、 14、 15、 16、 17、 18、 19、 20、 21、 22、三、分析下列程序的輸出結果。1、運行該程序輸出結果如下所示。Default constructor calledConstructor calleda=0,b=0a=4,b

2、=82、運行該程序輸出結果如下所示。a=7,b=93、運行該程序輸出結果如下所示。1044、運行該程序輸出結果如下所示。1035,789.5045、運行該程序輸出結果如下所示。10,1,2,3,4,5,6,7,8111,12,13,14,15,16,17,18,1919,18,17,16,15,14,13,12,116、運行該程序輸出結果如下所示。Starting1:Default constructor called.Default constructor called.Default constructor called.Eding1:Starting2:Constructor: a=5,

3、b=6Constructor: a=7,b=8Constructor: a=9,b=10Ending2:Destructor called.a=9,b=10Destructor called.a=7,b=8Destructor called.a=5,b=6Destructor called.a=5,b=6 Destructor called.a=3,b=4Destructor called.a=1,b=27、運行該程序輸出結果如下所示。Default constructor called.Default constructor called.Default constructor called

4、.Default constructor called.Destructor called.Constructor1 called.Destructor called.Constructor2 called.Destructor called.x=0,y=0x=5,y=0x=2,y=3Destructor called.Destructor called.Destructor called.8、運行該程序輸出結果如下所示。Constructor called.0Constructor called.5Destructor called.55Destructor called.59、運行該程序輸

5、出結果如下所示。Constructor called.55Destructor called.510、運行該程序輸出結果如下所示。Default Constructor called.Constructor:real=6.8,imag=0Constructor:real=5.6,imag=7.90+0I6.8+0I5.6+7.9IConstructor:real=1.2,imag=3.4Constructor:real=5,imag=0Default Constructor called.1.2+3.4I5+0I0+0I11、答:該程序中用string.h中所包含的函數(shù)有3種,它們是strcp

6、y( )、strcat( )和strlen( ).該程序中使用了函數(shù)重載。它們是下述兩個構造函數(shù)重載:String( )和String(const char * str)。類中成員函數(shù)Setc( )的功能是用來設置字符數(shù)組中某位置的一個指定字符。類中成員函數(shù)Getc( )的功能是用來從某個字符數(shù)組中獲取指定位置的字符。類中成員函數(shù)Append( )的功能是在一個字符數(shù)組中追加一個指定的字符串,即將指定的字符串添加到已知串的后邊。不行。該程序中有兩處使用了new運算符。運行該程序輸出結果如下所示。empty.a string.9a string.ithis a string.四、改正以下程序中的

7、錯誤。1、該程序中point類的構造函數(shù)定義不正確,在main()中隊數(shù)據(jù)成員的訪問不正確,修改如下:#include class pointint x1,x2;public: point(int x,int y)point:x=x;point:y=y; void disp() coutx1endl; coutx2endl;/;void main()point data(5,5);data.disp();2、在main()函數(shù)中的p.x+=5和p.y+=6兩個語句是錯誤的,因為保護數(shù)據(jù)僅能被類的成員函數(shù)訪問。五、按下列要求編寫程序。1、程序內容如下所示。#include class Testp

8、ublic:Test()Test(int i,int j=20)t1=i;t2=j;t+=j-i;static int fun(Test&T);friend int add(Test&T);private:int t1,t2;static int t;int Test:fun(Test&T)t+=T.t;return t;int add(Test&T)int n=T.t1+T.t2;return n;int Test:t=5;void main()Test a1,a2(10),a3(15,25);coutadd(a2)endl;coutTest:fun(a2)endl;2、程序內容如下所示。#

9、include #include class Product char *name; int price; int quantity;public: Product(char *n,int p,int q) name =new charstrlen(n)+1; strcpy(name,n);price=p;quantity=q;Product() if(name) delete name; name=0;void buy(int money) int n,r; n=money/price; if(nquantity) cout數(shù)量不夠endl; else quantity-=n; r=mone

10、y%price; cout產品:name單價:price元 顧客; coutmoney元,買了n臺,剩余r元endl;void get() const cout產品:name單價:price元 剩余quantity臺endl;void main() Product p1(電視機,2000,15); p1.buy(7000); p1.get(); p1.buy(4500); p1.get();3、程序內容如下所示。#include #include class CDate private: int m_nDay; int m_nMonth; int m_nYear; public: CDate(

11、); CDate(int day,int month,int year); void Display(); void AddDay(); void SetDate(int day,int month,int year); CDate(); private: bool IsLeapYear(); /判斷該年是否為閏年;CDate:CDate()CDate:CDate(int day,int month,int year) m_nDay=day; m_nMonth=month; m_nYear=year;void CDate:Display() char day5; char month5; ch

12、ar year5; _itoa(m_nDay,day,10); _itoa(m_nMonth,month,10); _itoa(m_nYear,year,10); coutday/month/year31) if(m_nMonth=12) m_nYear+; m_nMonth=1; m_nDay=1;else m_nMonth+; m_nDay=1;void CDate:SetDate(int day,int month,int year) m_nDay=day; m_nMonth=month; m_nYear=year;CDate:CDate()bool CDate:IsLeapYear()

13、 bool bLeap; if (m_nYear%4!=0)bLeap=false; else if(m_nYear%100!=0)bLeap=true;else if(m_nYear%400!=0) bLeap=false; else bLeap=true; return bLeap;void main() CDate d; d.SetDate(2001,2,28); cout:; d.Display(); d.AddDay(); cout:; d.Display();4、程序內容如下所示。#include class Tc private:double unlead,lead,total;

14、int unprice,price; public:Tc()unprice=17;price=16;void getdata() coutunlead; coutlead; total=unprice*unlead+price*lead;void disp() cout總收入:totalendl;void main() Tc A; A.getdata(); A.disp();5、程序內容如下所示。#include class CFactorialint value;int fact; public:CFactorial(int val);void CalculateFactorial();vo

15、id Display();CFactorial:CFactorial(int val) value=val; fact=1;void CFactorial:CalculateFactorial() int i=value; while(i1) fact*=i-;void CFactorial:Display() coutvalue!=factendl;void main() CFactorial A(5); A.CalculateFactorial(); A.Display();6、程序內容如下所示。#include #include class rectangle private:float

16、 ledge,sedge; public:rectangle();rectangle(float a,float b) ledge=a;sedge=b;float area() return ledge*sedge;void addarea(rectangle r1,rectangle r2) cout總面積:r1.ledge*r1.sedge+ r2.ledge*r2.sedgeendl;void main() rectangle A(3.5,2.5),B(4.2,3.8),C; C.addarea(A,B);7、程序內容如下所示。#include #include class rectan

17、gle private:float ledge,sedge; public:rectangle();rectangle(float a,float b) ledge=a;sedge=b;float area() return ledge*sedge;void showlength() cout周長:(ledge+sedge)*2endl;rectangle tlength(rectangle r2) rectangle temp; temp.ledge=ledge+r2.ledge; temp.sedge=sedge+r2.sedge; return temp;void main() rect

18、angle A(3.5,2.5),B(4.2,3.8); coutA; A.showlength(); coutB; B.showlength(); rectangle C=A.tlength(B); coutC; C.showlength();8、程序內容如下所示。#include #include class Line private:double x1,x2,y1,y2; public:Line();Line(double a,double b,double c,double d) x1=a;y1=b;x2=c;y2=d; cout線段端點x1,y1)-(x2,y2)0&r0&t1) c

19、out0&r=1) cout=1 | r0&t1) coutendl; else coutendl;void main() Line A(2,2,18,18),B(1,12,19,5); twoline(A,B);9、本題涉及兩個類student和cdegree,前者為學生類,包含學生的學號(no),姓名(name)和成績(degree),而成績degree是類cdegree的對象。cdegree類有3個數(shù)據(jù)成員,分別為數(shù)學(math),英語(english)和物理(phy)分數(shù)。程序內容如下所示。#include class student int no; char name10; clas

20、s cdegree public: int math; int english; int phy;degree;public: void getdata() coutno; coutname; coutdegree.math; coutdegree.english; coutdegree.phy; void disp() cout學號:noendl; cout姓名:nameendl; cout數(shù)學分數(shù):degree.mathendl; cout英語分數(shù):degree.englishendl; cout物理分數(shù):degree.phyendl;void main() student stud; s

21、tud.getdata(); stud.disp();10、程序內容如下所示。#include class Studentint english,computer,total; public:void getscore();void display();void sort(Student *);Student();void Student:getscore() coutenglish; coutcomputer; total=english+computer;void Student:sort(Student *p) int tmp,i,j; for(j=0;j2;j+)for(i=0;i2;

22、i+) if (totaltotal) tmp=total; total=p-total; p-total=tmp; tmp=english; english=p-english; p-english=tmp; tmp=computer; computer=p-computer; p-computer=tmp;void Student:display() cout英語=english計算機=computer總分=totalendl;void main() Student *A3; for (int j=0;j3;j+) Aj=new Student; cout學生j+1getscore();i

23、nt i;for (j=0;j2;j+) for(i=0;isort(Ai+1);coutendl排序結果如下:endl;for (i=0;idisplay();11、程序內容如下所示。#include struct list /定義棧 int data; list *next;class Stack /定義一個棧操作類list *ptr; public:Stack()ptr=NULL;void push(int i);int pop();void Stack:push(int x) /入棧成員函數(shù) list *newnode=new list; newnode-data=x; newnode

24、-next=ptr; ptr=newnode;int Stack:pop() /出棧成員函數(shù) list *top; int value; value=ptr-data; top=ptr; ptr=ptr-next; delete top; return value;void main() Stack A; int arr=5,2,8,1,4,3,9,7,6; cout入棧順序:; for (int i=0;i9;i+) coutarri ;A.push(arri);coutendl出棧順序:;for (i=0;i9;i+) coutA.pop() ;coutendl;12、程序內容如下所示。#

25、include struct list int data; list *next;class Queue list *ptrf,*ptrb; /隊首和隊尾指針 public:Queue() ptrf=ptrb=NULL;void enqueue(int);int dequeue();void Queue:enqueue(int x) /入隊成員函數(shù) list *newnode=new list; newnode-data=x; newnode-next=NULL; if (ptrb=NULL) ptrf=ptrb=newnode; else ptrb-next=newnode; ptrb=ne

26、wnode;int Queue:dequeue() /出隊成員函數(shù) list *tmp; int value; value=ptrf-data; tmp=ptrf; ptrf=ptrf-next; delete tmp; return value;void main()Queue A; int arr=3,12,8,9,11; cout入隊順序;; for (int i=0;i5;i+) coutarri ; A.enqueue(arri); coutendl出隊順序:; for(i=0;i5;i+) coutA.dequeue() ; coutendl;13、程序內容如下所示。#includ

27、e#includetypedef struct tree int data; tree *left,*right,*father;*bstree;class Btree static int n; static int m;public: tree *root; Btree() root=NULL; void create_Btree(int); void inorder(bstree); /中序遍歷 void display() coutendl中序遍歷序列: endl;inorder(root);coutdata!=k) print(p-left,k); print(p-right,k);

28、 else coutk的雙親結點的內容為:father-datadata=x; newnode-right=newnode-left=NULL; if(root=NULL) root=newnode; else bstree back; bstree current=root; while(current!=NULL) back=current; if(current-datax) current=current-left; else current=current-right; if(back-datax) back-left=newnode;newnode-father=back; els

29、e back-right=newnode;newnode-father=back; int Btree:count(bstree p) if(p=NULL) return 0; else return count(p-left)+count(p-right)+1; /這是運用了函數(shù)嵌套即遞歸的方法。void Btree:inorder(bstree temp) /這是中序遍歷二叉樹,采用了遞歸的方法。 if(temp!=NULL) inorder(temp-left); coutdataright); void main() Btree A; int array=7,4,1,5,12,8,13

30、,11; int k; k=sizeof(array)/sizeof(array0); cout建立排序二叉樹順序: endl; for(int i=0;ik;i+) coutarrayi ; A.create_Btree(arrayi); coutendl; cout二叉樹節(jié)點個數(shù): A.count(A.root)endl; A.display(); 14、程序內容如下所示。#include #include struct list int data; list *next;class Stacklist *ptr; public:Stack()ptr=NULL;void push(int

31、i);int pop();int empty() if (ptr=NULL) return 1; else return 0;void Stack:push(int x) list *newnode=new list; newnode-data=x; newnode-next=ptr; ptr=newnode;int Stack:pop() list *top; int value; value=ptr-data; top=ptr; ptr=ptr-next; delete top; return value;class Queue list *ptrf,*ptrb; public: Queu

32、e() ptrf=ptrb=NULL;void enqueue(int);int dequeue();void Queue:enqueue(int x) list *newnode=new list; newnode-data=x; newnode-next=NULL; if (ptrb=NULL)ptrf=ptrb=newnode; else ptrb-next=newnode; ptrb=newnode;int Queue:dequeue() list *tmp; int value; value=ptrf-data; tmp=ptrf; ptrf=ptrf-next; delete tm

33、p; return value;void main() Stack S; Queue Q; char ch; cout 輸入數(shù)據(jù):; while(ch=getchar()!=.) S.push(ch); Q.enqueue(ch);while(!S.empty() & S.pop()=Q.dequeue();if (S.empty() cout輸入的是回文數(shù)據(jù)。endl;else cout輸入的不是回文數(shù)據(jù)。endl;15、程序內容如下所示。#include #include class magic int m44; int step; int first; int sum; public:

34、void getdata(); void setfirstmagic(); void generatemagic(); void printmagic();void magic:getdata() coutfirst; coutstep;void magic:setfirstmagic() int i,j; int tmp; tmp=first; for(i=0;i4;i+) for(j=0;j4;j+) mij=tmp; tmp+=step;void magic:generatemagic() sum=m00+m33; for (int i=0,j=0;i4;i+,j+)mij=sum-mij; for (i=0,j=3;i4;i+,j-) mij=sum-mij;void magic:printmagic() int i,j; for(i=0;i4;i+) for(j=0;j4;j+) coutsetw(5)mij; coutendl;void main() magic A; A.getdata(); A.setfirstmagic(); cout原來的魔方如下:endl; A.printmagic(); A.generatemagic(); cout最終的魔方如下:endl; A.printmagic();

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

相關資源

更多
正為您匹配相似的精品文檔
關于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對上載內容本身不做任何修改或編輯。若文檔所含內容侵犯了您的版權或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!