C++面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集_第1頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集_第2頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集_第3頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集_第4頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集_第5頁(yè)
已閱讀5頁(yè),還剩75頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上C+面向?qū)ο蟪绦蛟O(shè)計(jì)習(xí)題集編程題:用面向?qū)ο蟮某绦蛟O(shè)計(jì)方法編制如下程序。設(shè)計(jì)一個(gè)Bank類(lèi),實(shí)現(xiàn)銀行某賬號(hào)的資金往來(lái)賬管理,包括建賬號(hào)、存入、取出等。Bank類(lèi)包括私有數(shù)據(jù)成員top(當(dāng)前指針)、date(日期)、money(金額)、rest(余額)和sum(累計(jì)余額)。另有一個(gè)構(gòu)造函數(shù)和3個(gè)成員函數(shù)bankinO(處理存入賬)、bankout()(處理取出賬)和disp( )(出明細(xì)賬)?!局R(shí)點(diǎn)】:2.2 2.3【參考分】:25分【難易度】:B【答案】:#include <iostream.h>#include <iomanip.h>#in

2、clude <string.h>const int Max=100;class Bank int top; char dateMax10;/日期 int moneyMax;/金額 int restMax;/余額 static int sum;/累計(jì)余額public: Bank()top=0;void bankin(char d ,int m) strcpy(datetop,d); moneytop=m; sum=sum+m; resttop=sum; top+;void bankout(char d,int m) strcpy(datetop,d); moneytop=-m; /取

3、出數(shù)用負(fù)數(shù)表示 sum=sum-m; resttop=sum; top+;void disp() int i; cout<<"日期 存入 取出余額"<<endl; for(i=0;i<top;i+) cout<<setw(10)<<datei; if(moneyi<0) cout<<""<<setw(6)<<-moneyi; else cout<<setw(6)<<moneyi <<"" cout<

4、<setw(6)<<resti<<endl;int Bank:sum=0;void main() Bank obj; obj.bankin("2005.2.5".1000); obj.bankin("2006.3.2",2000); obj.bankout("2007.4.1",500); obj.bankout("2007.10.5",800); obj.disp();編寫(xiě)一個(gè)程序,已有若干個(gè)學(xué)生數(shù)據(jù),包括學(xué)號(hào)、姓名、成績(jī),要求輸出這些學(xué)生數(shù)據(jù)并計(jì)算平均分?!局R(shí)點(diǎn)】:2.2 2.3

5、【參考分】:20分【難易度】:B【答案】: #include<iostream.h> #include<iomanip.h> #include<string.h> class Stud int no; char name10; int deg; static int sum; static int num; public: Stud(int n,char na,int d) no=n;deg=d; strcpy(name,na); sum+=d; num+; static double avg() return sum/num; void disp() co

6、ut<<setw(5)<<nO<<setw(8)<<name<<setw(6)<<deg<<endl; ; int Stud:sum=0j int Stud:num=O; void main() Stud s1(1,"Li",89),s2(2,"Chert",78),s3(3,"zheng",94); cout:<<"學(xué)號(hào) 姓名 成績(jī)"<<endl; s1.disp(); s2.disp(); s3.disp

7、();cout<<"平均分="<<Stud:avg()<<endl;有10個(gè)單詞存放在一維指針數(shù)組words中,編寫(xiě)一個(gè)程序,根據(jù)用戶(hù)的輸入找出所有與之從前向后匹配的單詞和個(gè)數(shù)。【知識(shí)點(diǎn)】:2.2【參考分】:25分【難易度】:B【答案】:設(shè)計(jì)一個(gè)Word類(lèi),包含一個(gè)私有數(shù)據(jù)成員words、一個(gè)構(gòu)造函數(shù)和一個(gè)公有成員函數(shù)lookup(),構(gòu)造函數(shù)用于給words賦初值,lookup()用于找出所有與之從前向后匹配的單詞和個(gè)數(shù)。程序如下:#include<iostream.h>#include<string.h>C1a

8、ss Word char words1012;public: Word() /構(gòu)造函數(shù)給words賦初值strcpy(words0,"elapse");strcpy(words1,"elucidate");strcpy(words2,"elude");strcpy(words3,"embody");strcpy(words4,"embrace");strcpy(words5,"embroider");strcpy(words6,"emrtge");strc

9、py(words7,"emphasize");strcpy(words8,"empower");strcpy(words9,"emulate"); void lookup(char s );void Word:lookup(char s ) char *w; int i,j,n=0; /n記錄相匹配的單詞個(gè)數(shù) cout<<"匹配的單詞:"<<endl; for(i=0;i<10;i+) /一個(gè)單詞一個(gè)單詞地匹配 for(w=wordsi,j=0;sj!='0'&

10、&*w!='0'&&*w=sj; j+,w+); if(sj='0') /匹配成功 n+; cout<<"t"<<wordsi <<endl; cout<<"相匹配的單詞個(gè)數(shù):"<<n<<endl;void main() Word obj; char str20; cout<<"輸入單詞:" cin>>str;obj.lookup(str); 創(chuàng)建一個(gè)Employee類(lèi),該類(lèi)中有字符數(shù)

11、組,表示姓名、街道地址、市、省和郵政編碼。把表示構(gòu)造函數(shù)、changenameO、display()的函數(shù)原型放在類(lèi)定義中,構(gòu)造函數(shù)初始化每個(gè)成員,display()數(shù)把完整的對(duì)象數(shù)據(jù)打印出來(lái)。其中的數(shù)據(jù)成員是保護(hù)的,函數(shù)是公共的?!局R(shí)點(diǎn)】:2.2 2.3 3.1【參考分】:20分【難易度】:B【答案】:#include<iostream.h>#include<string.h>class Employeeprotected: char name10; /姓名 char street20; /街道地址 char city10; /市 char prov10; /省 c

12、har post7; /郵政編碼 int no; /記錄序號(hào)public: Employee(char,char,char,char,char,int); void changename(char str); void display();Employee:Employee(char n,char s,char c,char pl,char p2,int num) strcpy(name,n); strcpy(Street,s); strcpy(city,c); Strcpy(prov,p1); strcpy(post,p2); no=num;void Employee:changename(

13、char n) strcpy(name,n);void Employee:display() cout<<"輸出記錄"<<no<<endl; cout<<"姓名:"<<name<<endl; cout<<"街道地址:"<<street<<endl; cout<<"市:"<<city<<endlj tout<<"省:"<<prov&

14、lt;<endl; cout<<"郵政編碼:"<<post<<endl;void main()Employee obj1("王華","中華路15號(hào)","武漢市","湖北省","",1);Employee obj2("楊麗","天津路30號(hào)","南京市","江蘇省","",2);obj1.display();obj2.display();

15、編寫(xiě)一個(gè)程序,已有若干個(gè)學(xué)生數(shù)據(jù),這些數(shù)據(jù)包括學(xué)號(hào)、姓名、語(yǔ)文成績(jī)、數(shù)學(xué)成績(jī)和英語(yǔ)成績(jī),求各門(mén)課程的平均分。要求設(shè)計(jì)不同的成員函數(shù)求各門(mén)課程的平均分,并使用成員函數(shù)指針調(diào)用它們?!局R(shí)點(diǎn)】:2.2 2.3 3.1【參考分】:20分【難易度】:C【答案】:設(shè)計(jì)一個(gè)學(xué)生類(lèi)student,包括no(學(xué)號(hào))、name(姓名)、degl(語(yǔ)文成績(jī))、deg2(數(shù)學(xué)成績(jī))、deg3(英語(yǔ)成績(jī))數(shù)據(jù)成員和3個(gè)靜態(tài)數(shù)據(jù)成員suml(累計(jì)語(yǔ)文總分)、sum2(累計(jì)數(shù)學(xué)總分)、sum3(累計(jì)英語(yǔ)總分);另外有一個(gè)構(gòu)造函數(shù)、3個(gè)求三門(mén)課程平均分的成員函數(shù)和一個(gè)disp( )成員函數(shù)。程序如下:#include<

16、;iostream.h>#include<iomanip.h>#include<string.h>const int N=4;class student int no;/學(xué)號(hào) char name10;/姓名 int degl;/語(yǔ)文成績(jī) int deg2;/數(shù)學(xué)成績(jī) int deg3;/英語(yǔ)成績(jī) static int suml;/語(yǔ)文總分 statac int sum2;/數(shù)學(xué)總分 static int sum3;/英語(yǔ)總分public:student(int n,char na,int dl,int d2,int d3) no=n; strcpy(name,na

17、); degl=dl;deg2=d2;deg3=d3; suml+=degl;sum2+=deg2;sum3+=deg3;double avgl()return(suml*1.0)/N;double avg2()return(sum2*1.0)/N;double avg3()return(sum3*1.0)/N;void disp() cout<<setw(4)<<no<<setw(10)<<name<<setw(6)<<degl<<setw(6) <<deg2<<setw(6)<

18、<deg3<<endl; ;int student:suml=0;int student:sum2=0;int student:sum3=0;void main()double (student:*fp)(); /定義成員函數(shù)指針student s1(1,"Li",67,89,90);student s2(2,"Na",67,89,90);student s3(3,"Zheng",67,89,90);student s4(4,"Chert",67,89,90);cout<<"

19、輸出結(jié)果"<<endl;cout<<"學(xué)號(hào) 姓名 語(yǔ)文數(shù)學(xué)英語(yǔ)"<<endl;s1.disp();S2.disp();S3.disp();S4.disp();fp=student:avgl;cout<<"語(yǔ)文平均分:"<<(S1.*fP)()<<endl;fp=student:avg2;cout<<"數(shù)學(xué)平均分:"<<(S1.*fp)()<<endl;fp=student:avg3;cout<<"

20、英語(yǔ)平均分:"<<(S1.*fP)()<<endl; 編寫(xiě)一個(gè)程序,輸入N個(gè)學(xué)生數(shù)據(jù),包括學(xué)號(hào)、姓名、成績(jī),要求只輸出成績(jī)?cè)?089分?jǐn)?shù)段的學(xué)生數(shù)據(jù)?!局R(shí)點(diǎn)】:2.2 2.3【參考分】:20分【難易度】:B【答案】:設(shè)計(jì)一個(gè)學(xué)生類(lèi)Stud,包括no(學(xué)號(hào))、name(姓名)和deg(成績(jī))數(shù)據(jù)成員,另有兩個(gè)普通成員函數(shù)setdata()和disp(),前者用于設(shè)置對(duì)象數(shù)據(jù),后者用于只輸出成績(jī)?cè)?089分?jǐn)?shù)段的學(xué)生數(shù)據(jù)。在main()函數(shù)中定義了一個(gè)對(duì)象數(shù)組用于存儲(chǔ)輸入的學(xué)生數(shù)據(jù)。程序如下:#include<iostream.h>#include&

21、lt;iomanip.h>#include<stringh>const int N=3;class Stud int no; char name10; int deg;publiC: void setdata(int n,char na,int d) no=n;deg=d; strcpy(name,na); void disp() if(deg>=80&&deg<=89) cout<<setw(5)<<no<<setw(8)<<name<<setw(5)<<deg<<

22、;endl; ;void main() Stud stN; int i,n,d; char na10; cout<<"輸入學(xué)號(hào)姓名成績(jī)"<<endl; for(i=0;i<N;i+) cout<<"第"<<i+l<<"個(gè)學(xué)生:" cin>>n>>na>>d; Sti.Setdata(n,na,d);cout<<"輸出數(shù)據(jù)"<<endl;cout<<"學(xué)號(hào) 姓名 成績(jī)&q

23、uot;<<endl;for(i=0;i<N;i+)sti.disp( );編寫(xiě)一個(gè)程序統(tǒng)計(jì)學(xué)生成績(jī),其功能包括輸入學(xué)生的姓名和成績(jī),按成績(jī)從高到低排列打印輸出,對(duì)前70的學(xué)生定為合格(PASS),而后30的學(xué)生定為不及格(FAIL)?!局R(shí)點(diǎn)】:2.2 2.3 3.1【參考分】:40分【難易度】:A【答案】:設(shè)計(jì)一個(gè)類(lèi)student,包含學(xué)生的姓名和成績(jī)等數(shù)據(jù),以及setname()、setdeg()、getname()和 getdeg()等4個(gè)成員函數(shù)。設(shè)計(jì)一個(gè)類(lèi)compute,包含兩個(gè)私有數(shù)據(jù)成員,即學(xué)生人數(shù)ns和student類(lèi)的對(duì)象數(shù)組na,另有3個(gè)公共成員函數(shù)ge

24、tdata( )、sort( )、disp( ),它們的功能分別用于數(shù)據(jù)獲取、按成績(jī)排序和輸出數(shù)據(jù)。程序如下:#include<iOStream.h>#include<iomanip.h>#include<string.h>const int Max=10;class student char name10; int deg;public: void setname(char na)strcpy(name,na); char *getname()return name; void setdeg(int d)deg=d; int getdeg()return

25、deg;class compute int ns; student naMax;public: void getdata(); void sort(); void disp();void compute:getdata() int i,tdeg; char tname10; cout<<"輸入學(xué)生人數(shù):" cin>>ns; cout<<"輸入學(xué)生姓名和成績(jī):"<<endl; for(i=O;i<ns;i+) cout<<"第"<<i+l<<&qu

26、ot;個(gè)學(xué)生:" cin>>tname>>tdeg; nai.setname(tname); mei.setdeg(tdeg); void compute:sort() int i,j,pick; student temp; for(i=0;i<ns-1;i+) pick=i; for(j=i+1;j<ns;J+) if(naJ.getdeg()>napick.getdeg() pick=j; temp=nai; nai=napick; napick=temp; void compute:disp() int cutoff,i; cout&l

27、t;<"輸出結(jié)果"<<endl; cout<<" 姓名 成績(jī)合格否"<<endl; cout<<” - - -”<<endl; cutoff=ns*7/10-1; for(i=0;i<ns;i+) cout<<setw(8)<<naigetname()<<setw(5)<<nai.getdeg(); if(i<=cutoff) cout<<"PASS"<<endl; else cout&

28、lt;<"FAIL"<<endl; void main() compute obj; obj.getdata(); obj.sort(); obj.disp()編寫(xiě)一個(gè)程序,輸入用戶(hù)的姓名和電話(huà)號(hào)碼,按姓名的詞典順序排列后,輸出用戶(hù)的姓名和電話(huà)號(hào)碼?!局R(shí)點(diǎn)】:2.2 2.3 3.1【參考分】:40分【難易度】:B【答案】:設(shè)計(jì)一個(gè)類(lèi)person,包含學(xué)生的姓名和電話(huà)號(hào)碼等數(shù)據(jù),以及semame()、setnum()、gemame()和gemum()等4個(gè)成員函數(shù)。設(shè)計(jì)一個(gè)類(lèi)compute,包含一個(gè)私有數(shù)據(jù)成員,即person類(lèi)的對(duì)象數(shù)組pn,另有3個(gè)公共

29、成員函數(shù)getdata()、sort()、disp(),它們的功能分別用于數(shù)據(jù)獲取、按姓名的詞典順序排序和輸出數(shù)據(jù)。程序如下:#include<iostreamh>#include<iomanip.h>#include<string.h>const int N=5; class person char name10; char num10;public: void setname(char na)strcpy(name,na); void setnum(char nu)strcpy(num,nu);) char *getname()return name;

30、char *getnum()return num;);class compute person pnN;public: void getdata(); void getsort(); void outdata();void compute:getdata() int i; char na10,nu10; cout<<"輸入姓名和電話(huà)號(hào)碼"<<endl; for (i=0; i<N; i+) cout<<" 第"<<i+1<<"個(gè)人:" cin>>na>

31、>nu; pni.setname(na); pni.setnum(nu); void compute:getsort() int i,j,k; person temp; for(i=0;i<N-1;i+) k=i; for(j=i+1;j<N;j+) if(strcmp(pnk.getname(),pnj.getname()>0) k=j; temp=pnk; pnk=pni; pni=temp; void compute:outdata() int i; cout<<"輸出結(jié)果:"<<endl; cout<<&q

32、uot; 姓名 電話(huà)號(hào)碼"<<endl; cout << " - -" << endl; for(i=0;i<N;i+) cout<<setw(11) <<pni.getname() <<setw(8) <<pni.getnum()<<endl;void main() compute obj; obj.getdata(); obj.getsort(); obj.outdata();編寫(xiě)一個(gè)程序,輸入n個(gè)學(xué)生的姓名和出生日期,并按年齡從大到小輸出?!局R(shí)點(diǎn)】:2.2

33、 2.3 3.1【參考分】:40分【難易度】:A【答案】:設(shè)計(jì)一個(gè)結(jié)構(gòu)體person,包含學(xué)生的姓名和出生日期等數(shù)據(jù):設(shè)計(jì)一個(gè)類(lèi)compute,包含私有數(shù)據(jù)成員,即person結(jié)構(gòu)體數(shù)組st,另有3個(gè)私有成員函數(shù)daynum()、count day()、leap()和3個(gè)公共成員函數(shù)getdam()、sort()、disp(),它們的功能分別用于數(shù)據(jù)獲取、按出生日期排序和輸出數(shù)據(jù),在實(shí)現(xiàn)過(guò)程中調(diào)用前面的3個(gè)私有成員函數(shù)。sort()成員函數(shù)按結(jié)構(gòu)體數(shù)組元素的d成員排序,d存放的是該學(xué)生從1900年1月l日到出生日期的天數(shù)。程序如下:#include <iostream.h>#inc

34、lude <iomanip.h>const int N=4;int count day(int,int,int,int);int leap(int);struct person char name10; struct dates int year; int month; int day; date; int d; /從190011到出生日的天數(shù);class compute struct person stN; int daynum(int,int,int,int,int,int); int count_day(int,int,int,int); bool leap(int);pub

35、lic: void getdata(); void sort(); void disp();int compute:daynum(int s_year,int s_month,int s_day,int e_year, int e_month,int e_day) int year,day,dayl,day2; if(s year<e year) day=count_day(s_year,s_month,s_day,0); for(year=s_year+l;year<e_year;year+) if(leap(year) day+=366L; else day+=365L; da

36、y+=count_day(e_year,e_month,e_day,1); else if(s_year=e_year) dayl=count_day(s_year,s_month,s_day,1); day2=count_day(e_year,e_month,e_day,1); day=day2-dayl; else day=-1; return day;int compute:count_day(int year,int month,int day,int flag) staric int day_tab212=(31,28,31,30,31,30,3l,31,30,31,30,3l, 3

37、1,29,31,30,31,30,31,31,30,31,30,31; /使用二維數(shù)組存放各月天數(shù),第一行對(duì)應(yīng)非閏年,第二行對(duì)應(yīng)閏年 int p,i,s; if(leap(year) p=1; else p=0; if(flag) s=day; for(i=1;i<month;i+) s+=day tabpi一1; else s=day_tabpmonth-day; for(i=month+1;i<=12;i+) s+=day_tabPi一1; return(s);bool compute:leap(int year) if(year%4=O&&year%100!=

38、O|year%400=O) /是閏年 return true; else /不是閏年 return false;void compute:sort() int i,j; struct person temp; for(i=O;i<N一1;i+) for(j=0;j<N-i-1;j+) if(stj.d>stj+1.d) temp=stj; stj=stj+1; stj+1=temp; void compute:getdata() int i; for(i=0;i<N;i+) cout<<"輸入第"<<i+l<<&q

39、uot;個(gè)學(xué)生姓名:" cin>>; cout<<" 出生日期:" cin>>sti.date.year>>sti.date.month>>sti.date.day; sti.d=daynum(1900,1,1,sti.date.year, sti.date.month,sti.date.day); void compute:disp() int i; cout<<"輸出結(jié)果:"<<endl; cout<<" 姓名 出生日期

40、"<<endl; for(i=0;i<N;i+) cout<<setw(10)<<<<" "<<sti.date.year<< "." <<sti.date.month<<"."<<sti.date.day<<endl;void main() compute obj; obj.getdata(); obj.sort(); obj.disp();10設(shè)計(jì)一個(gè)集合類(lèi)Set,包括將集合置空、

41、添加元素、判斷元素是否在集合中、輸出集合以及將集合中元素逆置,另外還有一個(gè)復(fù)制構(gòu)造函數(shù),并使用一些數(shù)據(jù)進(jìn)行測(cè)試。【知識(shí)點(diǎn)】:2.2 2.3 3.1【參考分】:40分【難易度】:B【答案】:Set類(lèi)包括私有數(shù)據(jù)data(存放集合元素)、Bum(集合中元素個(gè)數(shù)),一個(gè)缺省構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)Set(Set &s),另有成員函數(shù)setempty()(將集合置空)、issetempty()(判斷集合是否為空)、ismemberof()(判斷元素是否在集合中)、add()(添加元素)、disp()(輸出集合)、reverse()(將集合中元素逆置)。其中,issetempty、ismember

42、of和disp函數(shù)設(shè)計(jì)為常成員函數(shù)。程序如下:#include<iostream.h>const int Max=10;C1ass Set int dataMax; int num;public: Set()num=0; Set(Set &s); /對(duì)象引用作為參數(shù) void setempty()num=0; bool issetempty()const; bool ismemberof(int n) const; int add(int n); void disp() const; void reverse();Set:Set(Set &S) nUm=S.num;

43、 for(int i=0;i<num;i+) datai=s.datai;bool Set:issetempty() const return(num=0);bool Set:ismemberof(int n) const for(int i=0;i<num;i+) if(datai=n) return l; return 0;int Set:add(int n) if (ismemberof(n) return 1; else if(num>Max) return 0; else datanum+=n; return 1; void Set:disp() const cou

44、t<<"" for(int i=0;i<num-1; i+) cout<<datai<<"," if(num>0) cout<<datanum-1; cout<<"" <<endl;void Set:reverse() int n=num/2; for(int i=0;i<n;i+) int temp; temp=datai; datai=datanum-i-1; datanum-i-1=temp; void main() Set A; cout

45、<<"A是否為空:" cout<<(A.issetempty()?"空":"非空")<<endl; cout<<"A:"A.disp(); Set B; for(int i=1;i<=8;i+) B.add(i); cout<<"B:"B.disp(); cout<<"5是否在B中:"cout<<B.ismemberof(5)<<endl; B.setempty(); for

46、(int j=11; j<20;j+) B.add(j); Set C(B); cout<<"C:"C.disp(); C.reverse(); cout<<"C逆置"<<endl; cout<<"C:"C.disp();11設(shè)計(jì)一個(gè)職工類(lèi)Person,一個(gè)系有若干個(gè)職工,按職務(wù)分為系主任、室主任和職工,給出他們之間的領(lǐng)導(dǎo)關(guān)系。【知識(shí)點(diǎn)】:2.2【參考分】:25分【難易度】:C【答案】:類(lèi)Person有姓名、職務(wù)和指向領(lǐng)導(dǎo)的指針等私有數(shù)據(jù),以及兩個(gè)構(gòu)造函數(shù)和以下成員函數(shù):setle

47、ader()設(shè)置當(dāng)前職工的領(lǐng)導(dǎo);getname()獲取職工姓名;getleader()獲取領(lǐng)導(dǎo)者對(duì)象指針:disp()輸出姓名和職務(wù)。程序如下:#include<iostream.h>#include<iomanip.h>#include<string.h>class Person char name10; char prof10; Person *1eader;public: Person()strcpy(name,"0"); Person(char n,char P) strcpy(name,n); strcpy(prof,P); l

48、eader=new Person; void setleader(Person &p)leader=&p; /對(duì)象引用作為參數(shù) char *getname()return name;) Person *getleader() return leader; void disp() cout<<setw(10)<<name<<setw(10)<<prof; ;void main() Person P =Person("王華","室主任"),Person("李明","

49、職工"), Person("陳強(qiáng)","系主任"),Person("章城","職工"), Person("張偉","室主任"),Person("許源","職工");P0.setleader(P2);P1.setleader(P0);P3.setleader(P4);P4.setleader(P2);P5.setleader(P4);cout<<” 姓名 職務(wù) 領(lǐng)導(dǎo)姓名”<<endl;cout<<

50、;” - - -”<<endl; for(int i=0;i<6;i+) Pi.disp(); cout<<setw(10)<<(Pi.getleader()->getname()<<endl; 12需要求3個(gè)長(zhǎng)方柱的體積,請(qǐng)編一個(gè)基于對(duì)象的程序。數(shù)據(jù)成員包括length(長(zhǎng))、width(寬)、height(高)。要求用成員函數(shù)實(shí)現(xiàn)以下功能: (1)由鍵盤(pán)分別輸入3個(gè)長(zhǎng)方柱的長(zhǎng)、寬、高;(2)計(jì)算長(zhǎng)方柱的體積;(3)輸出3個(gè)長(zhǎng)方柱的體積。【知識(shí)點(diǎn)】:2.2 2.3【參考分】:20分【難易度】:C【答案】:#include <i

51、ostream>using namespace std;class Boxpublic: void get_value(); float volume(); void display(); public: float lengh; float width; float height; ;void Box:get_value() cout<<"please input lengh, width,height:" cin>>lengh; cin>>width; cin>>height;float Box:volume() r

52、eturn(lengh*width*height);void Box:display() cout<<volume()<<endl;int main()Box box1,box2,box3; box1.get_value(); cout<<"volmue of bax1 is " box1.display(); box2.get_value(); cout<<"volmue of bax2 is " box2.display(); box3.get_value(); cout<<"vo

53、lmue of bax3 is " box3.display(); return 0;13 建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成績(jī)),用指針指向數(shù)組首元素,輸出第1、3、5個(gè)學(xué)生的數(shù)據(jù)?!局R(shí)點(diǎn)】:3.2【參考分】:20分【難易度】:C【答案】:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void display(); private: int num; float score; ;void Student:

54、display() cout<<num<<" "<<score<<endl; int main()Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); Student *p=stud; for(int i=0;i<=2;p=p+2,i+) p->display(); return 0; 14 建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成績(jī)),設(shè)立一個(gè)函數(shù)

55、max,用指向?qū)ο蟮闹羔樧骱瘮?shù)參數(shù),在max函數(shù)中找出5個(gè)學(xué)生中成績(jī)最高者,并輸出其學(xué)號(hào)?!局R(shí)點(diǎn)】:3.2【參考分】:20分【難易度】:C【答案】:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); void

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論