《C++面向對象程序設計》答案-第三章-譚浩強-清華大學出版社_第1頁
《C++面向對象程序設計》答案-第三章-譚浩強-清華大學出版社_第2頁
《C++面向對象程序設計》答案-第三章-譚浩強-清華大學出版社_第3頁
《C++面向對象程序設計》答案-第三章-譚浩強-清華大學出版社_第4頁
《C++面向對象程序設計》答案-第三章-譚浩強-清華大學出版社_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

2:#include 〈iostream〉usingnamespacestd;classDate{public:Date(int,int,int);Date(int,int);Date(int);Date();voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}Date::Date(intm,intd):month(m),day(d){year=2005;}Date::Date(intm):month(m){day=1;year=2005;}Date::Date(){month=1;day=1;year=2005;}voidDate::display(){cout<〈month〈〈"/"〈<day〈<"/"<<year〈<endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2。display();d3.display();d4。display();return0;}3:#include〈iostream>usingnamespacestd;classDate{public:Date(int=1,int=1,int=2005);voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}voidDate::display(){cout〈〈month<<”/”<<day<〈"/"<〈year〈〈endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2.display();d3。display();d4。display();return0;}4:#include〈iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voiddisplay();private:intnum;}}}}floatscore;};voidStudent::display(){cout<<num〈<""〈<score<〈endl;}intmain(){Studentstud[5]={Student(101,78.5),Student(102,85。5),Student(103,98。5),Student(104,100.0),Student(105,95.5)};Student*p=stud;for(inti=0;i〈=2;p=p+2,i++)p->display();return0;}5:#include<iostream〉usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}intnum;floatscore;};voidmain(){Studentstud[5]={Student(101,78。5),Student(102,85.5),Student(103,98.5),Student(104,100。0),Student(105,95.5)};voidmax(Student*);Student*p=&stud[0];max(p);}voidmax(Student*arr){floatmax_score=arr[0].score;intk=0;for(inti=1;i〈5;i++)if(arr[i].score>max_score){max_score=arr[i]。score;k=i;}cout<〈arr[k]。num〈<””〈<max_score〈〈endl;6:#include 〈iostream〉usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<〈num〈<""〈〈score〈<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78。5);stud.display();stud。change(101,80。5);stud。display();return0;}7:解法一#include〈iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats) {num=n;score=s;}voiddisplay(){cout<<num<〈"”〈<score<<endl;}〃可改為:voiddisplay()const{cout〈<num<<""〈<score〈<endl;}private:intnum;floatscore;};intmain(){constStudentstud(101,78。5);stud.display();//stud。change(101,80。5);studodisplay();return0;解法二:#include〈iostream〉usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats)const{num=n;score=s;}voiddisplay()const{cout<〈num<〈"”〈〈score<〈endl;}private:mutableintnum;mutablefloatscore;};intmain(){constStudentstud(101,78.5);stud。display();stud。change(101,80.5);stud.display();return0;}解法三:#include<iostream〉usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout〈〈num<<"”〈<score<<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78.5);Student*p=&stud;p—>display();p->change(101,80.5);p—>display();return0;8:#include<iostream〉usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<<num〈〈"”<<score〈<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78.5);voidfun(Student&);fun(stud);return0;}voidfun(Student&stu){stu。display();stu。change(101,80.5);stu.display();}9:#include〈iostream〉usingnamespacestd;classProduct{public:Product(intn,intq,floatp):num(n),quantity(q),price(p){};voidtotal();staticfloataverage();staticvoiddisplay();private:intnum;intquantity;floatprice;staticfloatdiscount;staticfloatsum;staticintn;};voidProduct::total(){floatrate=1。0;if(quantity>10)rate=0.98*rate;sum=sum+quantity*price*rate*(1—discount);n=n+quantity;}voidProduct::display(){cout<〈sum〈<endl;cout〈〈average()<〈endl;}floatProduct::average(){return(sum/n);}floatProduct::discount=0。05;floatProduct::sum=0;intProduct::n=0;intmain(){ProductProd[3]={Product(101,5,23.5),Product(102,12,24。56),Product(103,100,21。5)};for(inti=0;i〈3;i++)Prod[i]。total();Product::display();return0;}10:#include<iostream>usingnamespacestd;classDate;classTime{public:Time(int,int,int);friendvoiddisplay(constDate&,constTime&);private:inthour;intminute;intsec;};Time::Time(inth,intm,ints){hour=h;minute=m;sec=s;}classDate{public:Date(int,int,int);friendvoiddisplay(constDate&,constTime&);private:intmonth;intday;intyear;};Date::Date(intm,intd,inty){month=m;day=d;year=y;}voiddisplay(constDate&d,constTime&t){cout<〈d.month〈〈”/"〈〈d。day<<"/"〈<d。year<<endl;cout〈<t。hour<<”:”<〈t.minute〈〈":"〈<t。sec<〈endl;}intmain(){Timet1(10,13,56);Dated1(12,25,2004);display(d1,t1);return0;}11:#include<iostream>usingnamespacestd;classTime;classDate{public:Date(int,int,int);friendTime;private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}classTime{public:Time(int,int,int);voiddisplay(constDate&);private:inthour;intminute;intsec;};Time::Time(inth,intm,ints):hour(h),minute(m),sec(s){}voidTime::display(constDate&d){cout〈<d.month〈<"/”<<d。day<〈"/"<〈d.year<〈endl;cout<<hour〈<":”<<minute〈<”:"<<sec<〈endl;}intmain(){Timet1(10,13,56);Dated1(12,25,2004);t1.display(d1);return0;}12:#include<iostream>usingnamespacestd;template〈classnumtype>classCompare{public:Compare(numtypea,numtypeb);numtypemax();numtypemin();private:numtypex,y;};template〈classnumtype〉Compare〈numtype〉::Compare(numtypea,numtypeb){x=a;y=b;}template

溫馨提示

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

評論

0/150

提交評論