版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第二章2- 4#i nclude <iostream> using n amespace std;Add(i nt a,i nt b);int main()int x,y,sum;cout<<"please in put x and y:"cin> >x>>y;sum = add(x,y);cout vvxvv"+"vvyvv"="v<sumvve ndl;Add(i nt a,i nt b) retur n a+b;2- 5(1)this is a C+ program.x=50
2、.6 y=10 z=A x=216.34 y=10 z=A x=216.34 y=2 z=A x=216.34 y=2 z=Ex y z500 1000 0500 1500 1500500 200 15002- 6#i nclude <iostream> using n amespace std; int mai n()int *p,*i nit;int coun tp=0;int countn=0; p = new in t20;init = p;for(int i=0;i<20;i+) cin> >*p;p+;p = p-20;for( i=0;i<20
3、;i+)if(*p>0) cou ntp+;if(*p<0) cou ntn+;cout<<*p<<""p+;cout«"正數(shù)有:"<<countp«endl; cout«"負(fù)數(shù)有:"<<countn«endl; p = init;delete p;return 0;2- 7不做要求#i nclude <iostream>#in clude <stri ng>using n amespace std;void c
4、heckagescore(stri ng n ame,i nt age) if (n ame = "exit") throw n ame;if(age<0|age>50)throw age;int main()stri ng n ame; int age;for(int i=0 ;i<5 ;i+ )cin .ig nore (); getl in e(c in,n ame );cin> >age ;try checkagescore( name,age);catch( stri ng)cout«"exception :nam
5、e is exit"<<endl; con ti nue;catch(i nt)cout<<"excepti on :age is not proper"<<e ndl; con ti nue;cout< <"n ame:"< <n ame<<" age :"<<age<<e ndl; return 0;第三章3- 1(1) A(2) C (3) B (4)C(5)C(6)B (7) B (8) C(9)C3- 7(1)main(
6、)函數(shù)中pl.age = 30;語句是錯誤的。age是類的私有成員(2)構(gòu)造函數(shù)應(yīng)當(dāng)給常數(shù)據(jù)成員和引用成員初始化,將構(gòu)造函數(shù)改為:A(i nt a1,i nt b1):a(a1),b(b1)或A(i nt a1 ):a(a1),b(a) 再將 ma in 中的 A a(1,2); 改為 A a(1);(1)在Test類中添加語句:void prin t();void Prin t()coutvvxvv"-"vvyvv"="v<x-yvve ndl;改為void Test:Pri nt() coutvvxvv"-"vvyvv&qu
7、ot;="v<x-yvve ndl; ma in函數(shù)中Ini t(38,15); 改為:A.I nit(38,15);Prin t();改為: A.Pri nt();3- 8(1)Con struct ing ACon struct ing BDestructi ng BDestructi ng Adouble a,double bpoint & pp.x3- 9class boxin t len 1,le n2,le n3;public:box(int l1,int I2,int I3)len1 = l1;len2 = 12; len3 = 13; long volu
8、mn()return len 1*len2*len3;3- 10class Testint m1,m2;public:void In it(i nt a,i nt b)m1 = a;m2 = b;void Prin g()cout<<m1<<" "<<m2<<e ndl;3-11略3-12第四章4- 6(1) D (2) D (3) D (4) D (5) B(6) D4- 7(1)static in t cou nt = 0;這樣初始化靜態(tài)成員值是不對的將其改為static int count;在類外,main函數(shù)前加int
9、 Sample:co unt = 0;(2) #i nclude <iostream> #in clude <cstdlib> using n amespace std;class Ctest private:int x; const int y1; public:const int y2;Ctest(int i1,int i2):y1(i1),y2(i2)y1 =10;/y1 為常量不能賦值 x = y1;int readme() con st;int Ctest:readme ()c onstint i;i = x;x+; /常函數(shù)不能改變成員值 return x;
10、int main()Ctest c(2,8);int i = c.y2;c.y2 = i;/y2為常量,不能改值i = c.y1;/y1私有,類外不能訪問return 0;將出錯語句全部注釋4- 8(1)題中印刷錯誤,將class C構(gòu)造函數(shù)改為:C()cout«"co nstructor C:"運(yùn)行結(jié)果為:con structor Acon structor Bcon structor C403434- 9# in clude<iostream.h>#i nclude<stdio.h>class Dateint year;int mon
11、th;int day;public:Date(i nt y,i nt m,i nt d)year=y ;mon th=m;day=d;void disp()coutvvyearvv" "<< mon th<<" "<<day<<e ndl;frie nd int coun t_day(Date &d,i nt k);frie nd in t l(i nt year);friend int h(Date &d1,Date &d2);;int coun t_day(Date &d
12、,i nt k)static int day_tab212=31,28,31,30,31,30,31,31,30,31,30,31.31,29,31,30,31,30,31,31,30,31,30,31;/使用二維數(shù)組存放各月天數(shù),第一行對應(yīng)非閏年,第二行對應(yīng)閏年int j,i,s;if(l(d.year)j=1;閏年,取1else j=0; 非閏年,取0if(k)/K 非 0 時s=d.day;for(i=1;i<d.mo nth;i+)/d.mo nth為輸入的月份s+=day_tabji-1; _else/K為 0 時s=day_tabjd.m on th-d.day;for(i=
13、d.m on th+1; i<=12; i+)s+=day_tabji-1; _return s;/S 為相差的天數(shù)int l(i nt year)if(year%4=0&&year%100!=0|year%400=0) /是閏年return 1;else /不是閏年return 0;int h(Date & d1,Date & d2)int days,day1,day2,y;if(d1.year<d2.year)/第一個日期年份小于第二個日期年份days=co un t_day(d1,0);for(y=d1.year+1;y<d2.year;
14、y+)if(l(y)閏年days+=366L;else/非閏年days+=365L;days+=co un t_day(d2,1); _else if(d1.year=d2.year)day仁co un t_day(d1,1);day2=cou nt_day(d2,1);days=day2-day1;elsedays=-1;retur n days;void mai n() int year1,year2, mon th1,m on th2,day1,day2; cout«"輸入日期 1"<<e ndl;cin> >year1>>
15、; mon th1>>day1;cout«"輸入日期 2"<<endl;cin> >year2> >mon th2>>day2;Date d1( year1, month1, day1),d2( year2, month2, day2); int ds=h(d1,d2);cout«"輸出結(jié)果:"<<endl;if(ds>=0)d1.disp(); prin tf("與");d2.disp(); prin tf("之間有 %cR
16、nn",ds);else/第一個日期小于第二個日期cout«"時間錯誤!"<<endl;4- 10#i nclude<iostream.h>#i nclude<stri ng.h>class Stude ntint nu mber;char n ame20;public:Student(int i=O,char *s="0")/ 構(gòu)造學(xué)生對象 nu mber=i;strcpy( names);void Print()/ 輸出結(jié)果cout<v"Number:"< <
17、;nu mbervve ndl;cout«"Name:"v <n ame<<e ndl;friend bool greatertha n(Stude nt & st1,Stude nt &st2);bool greatertha n(Stude nt & st1,Stude nt &st2)return st1.number>st2.number;/ 返回成員 number的比較結(jié)果int main()Stude ntst 5=Stude nt(65,"Li"),Stude nt(78,&
18、quot;Zha ng"),Stude nt(80,"wa ng"),Stude n t(92,"zhao"),Stude nt(50,"zhe n");int max = 0;int min = 0;for(i nt i=1;i<5;i+)if(!greatertha n(stmax,sti)max = i;if(!greatertha n(sti,stmi n)min = i;cout«"最大成績:"<<endl;stmax.Pri nt ();cout«&quo
19、t;最小成績:"<<endl;stmi n .Pri nt ();return 0;4- 11#i nclude <iostream>#in elude <stri ng> using n amespace std;class Bookchar *n ame; char*author; int sale;public:Book() n ame = '0'author = '0' sale = -1;Book(char* a ,char* b,i nt c)n ame = new charstrle n( a)+1;
20、strcpy( name,a);author = new charstrle n( b)+1; strcpy(author,b);sale = c;void prin t()cout«"autor "vvauthorvve ndl; cout< <"n ame "<<n ame<<e ndl; cout<<"price "<<sale<<e ndl; Book()if(! name ) delete n ame; if(!author)delete au
21、thor;int main()Book b1("c+","li ai hua",12);Book b2;return 0;第五章5- 8改錯題答案不唯一(1) class DC int x; public:DC()x =100;(2) 編譯無錯,但邏輯錯誤,可改為:class BCprotected: int x;public:BC(i nt i=0)x = i;class DC:private BCpublic:DC(i nt i):BC(i) ;將DC構(gòu)造函數(shù)改為:DC(i nt i):BC(i)y = 0;5- 9 (1) base class(1
22、0,5) (3,9-18,33) (13,19) (13,19-18,33) (13,19)5-10#i nclude <iostream> using n amespace std;class Shape int x,y;public:Shape(i nt ix,i nt iy)x = ix; y = iy;virtual void show()cout«"pos: "vvxvv' '<<y<<e ndl; ; class Circle :public Shapeint radius;public:Circle
23、(int ix,int iy,int r):Shape(ix,iy),radius(r) void show() Shape:show ();cout<<"circle: "<<radius<<e ndl;class Rect :public Shapeint width,higth;iw,i ntpublic:Rect(i ntix,i ntiy,i ntih):Sh ape(ix,iy),width(iw),higth(ih)void show() Shape:show ();cout<<"width and h
24、igth: "<<width<<' '<<higth<<endl;int main()Shape s1(1,1);Rect r1(2,2,8,8);Circle c1(3,3,9);r1.show ();c1.show();return 0;5-11#i nclude<iostream.h>class vehicle /定義汽車類protected:int date; / 年份float price; / 價格public:vehicle(i nt date,float price);int get_date
25、();float get_price();float date_load();void show();宀class car:public vehicle /定義小車類int passe nge匚 .load; /載人數(shù)public:car(i nt date,float price,i nt passe ngers=4);int get_passe ngers();void show();class truck:public vehicle /定義卡車類float payload; / 載重量public:truck(i nt date,float price,float max_load=2
26、4000.00); float efficie ncy();void show();vehicle:vehicle(i nt date,float price)vehicle:date=date;vehicle:price=price;int vehicle:get_date() _return date;float vehicle:get_price() _return price;void vehicle:show()cout << " 年份:"<< date << "年"<< endl;cout &l
27、t;< "價格:"<< price << " 元"<< endl;car:car(i nt date, float price,int passe ngers) :vehicle (date, price)passe nger_load=passe ngers; _int car:get_passe ngers ()retur n passe nger_load; _void car:show()cout <<" 車型:小車"<< endl;vehicle:show()
28、;cout << " 載人:"<< passenger_load << " 人"<< endl;cout << en dl;truck: truck(int date, float price,float max_load):vehicle(date,price) _payload=max_load; _float truck:efficie ncy()retur n payload;void truck:show()cout <<"車型:卡車"<<
29、endl;vehicle: show ();cout << " 載重:"<< efficiency() << endl;cout << en dl;void mai n ()car car1(2001,2000,5);truck tru1(2002,8000,340000);cout << "輸出結(jié)果"<< endl;car1. show ();tru1. show ();第六章6- 4d=3D:fu n();6- 5C:pri nt(),ci nfo=2C:pri nt(),ci
30、nfo=2D:pri nt(),di nfo=4B類不能定義對象,否則編譯通不過,因?yàn)锽未定義基類A中的虛函數(shù)prin t(), 它也是個虛基類。6-6#i nclude <iostream>using n amespace std;class Mammalpublic:virtual void Speak()cout<<"in Mammal"«e ndl;class Dog:public Mammalpublic:void Speak()cout<<"dog bark"«e ndl;int mai
31、n()Dog dog;Mammal *pM;pM = &dog;pM->Speak ();return 0;運(yùn)行結(jié)果:dog bark6-7#i nclude <iostream>using n amespace std;class BaseClasspublic:virtual BaseClass() cout«"destruct Base"<<e ndl;;class Derived:public BaseClasspublic:Derived()cout<<"destruct derived&quo
32、t;<<e ndl;int main()BaseClass *pbase;pbase = new Derived; delete pbase;結(jié)果將不能正常執(zhí)行子類析構(gòu)6-8 #in clude <iostream> using n amespace std; class Shapepublic:virtual double Area() = 0;class Circle :public Shape double radius;public:Circle(double r):radius(r)double Area() return 3.14*radius*radius
33、; ;class Square :public Shapedouble radius;public:Square(double r):radius(r)double Area() return 6*radius*radius;;class Recta ngle :public Shapedouble width,radius;public:Recta ngle(double w,double r):radius(r)width=w;double Area() return width*radius;class Trapezoid :public Shapedouble height,radiu
34、s,le ngth;public:Trapezoid(double h,double r,double l):radius(r)height=h;length=l; double Area() return height*(radius+le ngth)/2;class Trian gle :public Shapedouble height,radius;public:Trian gle(double h,double r):radius(r)height=h;double Area() return height*radius/2;int main()double AreaSum= 0;S
35、hape * pS 6;pS1 = new Circle(1);pS2 = new Square(2);pS3 = new Rectangle(1,2);pS4 = new Trapezoid(4,2,5);pS5 = new Trian gle(4,2);AreaSum += pS1->Area();AreaSum += pS2->Area();AreaSum += pS3->Area();AreaSum += pS4->Area();AreaSum += pS5->Area();cout«"總面積是:"vvAreaSumvvend
36、l;coutvv"各三維圖形面積如下:"<<endl;coutvv"圓形:"vvpS1->Area()vvendl;cout«"正方形:"vvpS2->Area()v<endl;coutvv"長方形:"vvpS3->Area()v<endl;coutvv"梯形:"vvpS4->Area()v<e ndl;coutvv"三角形:"vvpS5->Area()<<endl; return 0;6-9#
37、i nclude viostream>using n amespace std;class Stude ntpublic:virtual void show() = 0;class Junior :public Stude ntpublic:void show()coutvv"this would be info for junior stude nts"vve ndl;class Senior :public Stude ntpublic:void show()coutvv"this would be info for Senior stude nts&q
38、uot;vve ndl;int main()Stude nt *pstu; pstu = new Junior; pstu->show (); pstu = new Senior; pstu->show (); return 0;第七章7-1#i nclude <iostream> #in elude <stri ng> using n amespace std;class vectorint x,y;public:vector(i nt ix=0,i nt iy=0)x =ix;y = iy; vector operator+(vector& v1
39、)retur n vector(x+v1.x,y+v1.y);vector& operator+=(vector& v1)x +=v1.x;y +=v1.y; return *this;void show()coutvv'('vvxvv','vvyvv')'v<e ndl; ;int main()vector v1(1,2),v2(3,4),v3; v3 = v1+v2;v1+=v2;v3.show();v1.show();return 0;7-2 #i nclude <iostream.h>class Comp
40、lexprivate:double real,image;public:Complex(double x=0.0,double y=0.0) real =x; image =y; bool operator !=(c onst Complex & c);Complex operator -(c onst Complex &c);bool operator =(c onst Complex & c);Complex operator -();Complex &operator +=(c onst Complex &c);void Prin t();void
41、 Complex:Pri nt()coutvvrealvv" + "<<image<<"i"<<e ndl;Complex Complex :operator -(c onst Complex &c)Complex temp(real-c.real,image-c.image);return temp;bool Complex :operator =(c onst Complex &c)retur n (real=c.real && image=c.image);bool Complex
42、:operator !=(c onst Complex &c)return (real!=c.real | image!=c.image);Complex Complex :operator -()retur n Complex(-real,-image);Complex &Complex :operator +=(c onst Complex &c)real+=c.real;image+=c.image;return *this;int main()Complex c1(2,7),c2(4,2),c3;c3=c1-c2;c3.Pri nt(); if(c3=c1)co
43、ut«"c3 equals to c1"<<endl;else cout«"c3 does n? t equale to c1"«e ndl;c3=-c2;c3.Pri nt();c3+=c2;c3.Pri nt();if(c3 != c1) cout<v"c3!=c1"v<e ndl;return 0;7-3#i nclude <iostream>using n amespace std;bool rn (i nt y)bool flag = 0;if(y%400=0
44、 | y%4 =0&&y%100!=0) flag = 1;return flag;class Date private:int mon th, day, year;public:Date(i nt m, int d, int y);Date& operator+(i nt days); void showDate();Date:Date(i nt y, int m, int d) if (m>0 && m<13)mon th=m;if (d>0 && d<32)day=d;if (y>0 &&
45、; y<3000)year=y;Date& Date:operator+(i nt days)int i;for(i = days;i>0;)int diff ;switch(m on th)case 1:case 3:case 5:case 7:case 8:case 10:case 12: diff = 31 -day;break;case 4:case 6:case 9:case 11: diff = 30 -day;break; case 2:if (rn( year)diff = 29 - day;else diff = 28 -day; break;if(i&g
46、t;diff)i-= diff+1;day = 1;mon th+;if(mo nth>12)year+; mon th = 1;elseday+= i; break;return *this;void Date:showDate()cout<vyearvv"."vv mon th<<"."<<day<<e ndl;int main()Date obj(1996,1,1); obj.showDate (); obj = obj+59;obj.showDate(); return 0;7-4以+,二為例#in
47、 clude<iostream>#i nclude<stri ng.h> using n amespace std;class Stringchar *sbuf;int len gth;public:Stri ng()len gth=0;sbuf= new char; sbuf0='0'String(char *s)/用字符串初始化len gth=strle n( s);sbuf= new charle ngth+1;strcpy(sbuf,s);Stri ng (Stri ng& str)len gth=str.le ngth ;sbuf= n
48、ew charlength+1; strcpy(sbuf,str.sbuf );Stri ng()delete sbuf;Stri ng & operator =(Stri ng& str)if(sbuf = str.sbuf ) return *this;elsesbuf = new charstr.length +1; strcpy(sbuf,str.sbuf ); return *this; Stri ng operator +(Stri ng& str)/此函數(shù)需配合拷貝構(gòu)造函數(shù)String st;st.le ngth = len gth + str.le ng
49、th ;st.sbuf = new charst.le ngth+1 ;st.sbuf 0 = '0'strcpy(st.sbuf,sbuf);strcat(st.sbuf,str.sbuf );return st;char & operator(i nt i)if(i<0|i>=le ngth) /對下標(biāo)進(jìn)行檢查,超出圍則報(bào)錯退出程序cout«"下標(biāo)越界錯誤!"<<endl;exit(0); retur n sbufi; void Show()coutvvsbufvve ndl;int main()Stri ng
50、s1("hello");Stri ng s2("world"),s3 s3 = s1+s2; / s3.Show ();return 0;7-6#in clude<iostream.h>/using n amespace std;class polynomialint n,i;float *a;public :polynomial(int x)n=x;a= new float n;polyno mial (const polynomial & p) /增加一個拷貝復(fù)制構(gòu)造函數(shù)n=p.n;a= new float p.n;for ( i
51、nt i= 0; i<p.n; i+)ai=p.ai;polyno mial()po lyno mial()deletea;void seta( int y, float x)ay=x;void print()cout«"y=" <<an-1<<*八(” <<n-1<v")"for (i=n- 2;i>= 0;i-)cout«"+" <<ai<< "xA(" vvivv")"cout«e
52、ndl;polyno mial operator +(po lyno mial &x)if (n>=x.n)polyno mial t(n);copy(t,*this );for (i= 0;i<x.n;i+)i<x.nt.ai=t.ai+x.ai;return t;elsepol yno mial t(x. n);copy(t,x);for (i= 0;i<n;i+)t.ai=t.ai+ai;return t;polynomialoperator -(polynomial &x)if (n>=x.n)polyno mial t(n);copy(t
53、,*this );for (i= 0;i<=x.n;i+)t.ai=t.ai-x.ai;return t;elsepol yno mial t(x. n);copy(t,x);for (i= 0;i<=n;i+)t.ai=t.ai-ai;return t;friend void copy(polynomial &x,polynomial &y);void copy(polynomial &x,polynomial &y)int i;cout<< "無法復(fù)制"<<endl;x. pri nt();y.pri
54、nt(); system("pause");for (i= 0;i<=y.n- 1;i+)x.ai=y.ai;void main()polynomial j(3),k( 4),h( 4);j.seta(0, 1);j.seta(1,2);j.seta(2,3);k.seta(0,5);k.seta(1,6);k.seta(2,7);k.seta(3,7);j.pri nt();k.pri nt();polynomial m=k+j; /在這里再定義一個copy(h,k+j);h.pri nt();system("pause");第八章8-1(1)
55、templatevtype name T>T fun(T a)(2) templatevtypename T>T test(T a).(3) templatevtypename T>class Arraypublic: void fun();template<type name T> void Array<T>:fu n() Arrayvint> a1,a2,a3;8-4#in clude<iostream> using n amespace std;template <type name T>T max(T a,T b,
56、T c)T temp; temp=a>b?a:b; temp=temp>c?temp:c; return temp;int main()8山<<口&乂(11,29,22)<<6 ndl;8山<<口&滅ndl; 8山<<口&乂(0力')<<6 ndl;return 0;8-5 及 8-6#in clude< iostream>#in cludevstri ng> using n amespace std;template <type name T > void sort_bubble(T arr,i nt n) _for(i nt i=0; i<n-1;i+)for(i nt j=O;j< n-1-i;j+)if( arrj<arrj+1)T temp; temp=arrj; arrj=arrj+1; arrj+1=temp; class stude ntint num;stri ng n ame;public:stude nt(i nt n=O,stri ng n s="")num = n;n ame = ns;bool operator<(stu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025河北建筑安全員-A證考試題庫附答案
- DB32T-食品安全督導(dǎo)工作規(guī)范編制說明
- 三個共點(diǎn)力的動態(tài)平衡
- 單位人力資源管理制度精彩大合集十篇
- 公用事業(yè)行業(yè)十二月行業(yè)動態(tài)報(bào)告:水電發(fā)電量降幅收窄風(fēng)光核裝機(jī)目標(biāo)明確
- 江蘇省連云港市海州區(qū)2024-2025學(xué)年八年級上學(xué)期期末考試生物學(xué)試卷(含答案)
- 單位管理制度展示合集【職員管理篇】十篇
- 年產(chǎn)5000臺液晶電視項(xiàng)目可行性研究報(bào)告建議書
- 單位管理制度展示選集人力資源管理篇
- 單位管理制度品讀選集人員管理篇十篇
- 機(jī)動車維修竣工出廠合格證
- 陜西延長石油精原煤化工有限公司 60 萬噸 - 年蘭炭綜合利用項(xiàng)目 ( 一期 30 萬噸 - 年蘭炭、1 萬噸 - 年金屬鎂生產(chǎn)線)竣工環(huán)境保護(hù)驗(yàn)收調(diào)查報(bào)告
- 大病救助申請書
- 法學(xué)概論-課件
- 廈門物業(yè)管理若干規(guī)定
- 外科護(hù)理學(xué)試題+答案
- 齊魯醫(yī)學(xué)屈光和屈光不正匯編
- 貨架的技術(shù)說明(一)
- 【高等數(shù)學(xué)練習(xí)題】皖西學(xué)院專升本自考真題匯總(附答案解析)
- 高處作業(yè)安全技術(shù)交底-
- 工抵房協(xié)議模板
評論
0/150
提交評論