C++面向?qū)ο蟪绦蛟O(shè)計(jì)課后題答案_第1頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)課后題答案_第2頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)課后題答案_第3頁
已閱讀5頁,還剩57頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 PAGE PAGE 10面向?qū)ο蟪绦蛟O(shè)計(jì)課后題答案第二章 C+概述【2.6】D【2.7】D【2.8】A【2.9】A【2.10】B【】A【2.12】C【2.13】B【2.14】D【2.15】C【2.16】D【2.17】C【2.18】程序的運(yùn)行結(jié)果:101【2.19】程序的運(yùn)行結(jié)果:10 10【2.20】程序的運(yùn)行結(jié)果:1020【2.22】編寫一個(gè) C+風(fēng)格的程序,用動(dòng)態(tài)分配空間的方法計(jì)算 Fibonacci 數(shù)列的前 20 態(tài)分配的空間中。#include int main()int *p,i; p=new p0=1;p1=1;for(i=2;i20;i+)pi=pi-1+pi-2;for(i

2、=0;i20;i+)coutpiendl;return 0;【2.23C+sroot()sroot()3 次,讓它返回整數(shù)、長(zhǎng)整數(shù)與雙精度數(shù)的二次方根。#include #include doublesroot(intm)return sqrt(m);double sroot(long m)return sqrt(m);double sroot(double m)return sqrt(m);int main()coutsroot(145)=sroot(145)endl; coutsroot(123456)=sroot(123456)endl; coutsroot(1.44)=sroot(1.

3、44)endl; return 0;【2.24C+1、2、5 種換法?#include int main()int k=0;for(int i=0;i=20;i+)for(int j=0;j=0)k+;cout將一元人民幣兌換成 1、2、5 分的硬幣,共有k種換法endl; return 0;【2.25】編寫一個(gè) C+風(fēng)格的程序,輸入兩個(gè)整數(shù),將它們按由小到大的順序輸出。要求使用變量的引用。void change(int &a,int&b)int t=a; a=b;b=t;int main()int m,n;coutmn;if(mn)change(m,n);cout這兩個(gè)數(shù)從小到大依次為:mr

4、eturn0;【2.26C+f(x)=0 假設(shè)方程為:2*x*x*x-4*x*x+3*x-6=0#include #include using namespace std; double f(double x)return 2*x*x*x-4*x*x+3*x-6;int main()double left,right,middle,ymid,yleft,yright; coutleftright;yleft=f(left); yright=f(right);if(yleft*yright0)cout您輸入的兩個(gè)數(shù),用二分法無法求得方程的根endl; exit(0);domiddle=(left

5、+right)/2; ymid=f(middle); if(yleft*ymid0)right=middle; yright=ymid;if(yright*ymid=1e-6);cout方程的根為:middleendl; return 0;第三章 類和對(duì)象【3.6pointp1 p4別?pointp2,p3;point p2=p1;point p2(p1);(4)p4=p1;【解答】語句(1)point p2,p3;語句(2)在建立新對(duì)象 p2 時(shí),用已經(jīng)存在的對(duì)象p1 去初始化新對(duì)象 p2,在這個(gè)過程中用“賦值法”調(diào)用了拷貝構(gòu)造函數(shù);語句(3)在建立新對(duì)象 p2 時(shí),用已經(jīng)存在的對(duì)象p1 去

6、初始化新對(duì)象 p2,在這個(gè)過程中用“帶入法”調(diào)用了拷貝構(gòu)造函數(shù);【3.7】B【3.8】C【3.9】C【3.10】B【】B【3.12】A【3.13】A【3.14】 B【3.15】A【3.16】B【3.17】B【3.18】寫出下面程序的運(yùn)行結(jié)果。10,2030,4850,6870,8090,1611,120【3.19】寫出下面程序的運(yùn)行結(jié)果。Constructing 10Destructing 100Destructing【3.20】寫出下面程序的運(yùn)行結(jié)果。objects inexistenceobjectsinexistenceafterallocation 3objectsinexistenc

7、eafterdelection【3.21】寫出下面程序的運(yùn)行結(jié)果。Countingat0Countingat1Countingat2Countingat3Countingat4Countingat5Countingat6Countingat7Countingat8Countingat9【3.22】寫出下面程序的運(yùn)行結(jié)果。Default constructor Default constructor Default constructor Constructor: a=1, b=2 Constructor: a=3, b=4 Constructor:a=5,b=6【3.23】寫出下面程序的運(yùn)行結(jié)

8、果。Con. Copydefault.【3.24】寫出下面程序的運(yùn)行結(jié)果。A=5 B=14 A=9B=14【3.25】寫出下面程序的運(yùn)行結(jié)果。5,722,25【3.26】寫出下面程序的運(yùn)行結(jié)果。Constructing Constructing A=5B=15Destructing A=10 B=15Destructing Destructing Destructing【3.27】指出下列程序中的錯(cuò)誤,并說明為什么。classStudentchar name10; intage;float aver;void printStu();void main()Student p1,p2,p3;p1.

9、age30;/Error:age為私有成員,不能在類外直接訪問【3.28】指出下列程序中的錯(cuò)誤,并說明為什么。class Studentint age; int sno;public:/*publicprivate,則在主函數(shù)中不能調(diào)用私有的成員函數(shù)*/void printStu(); void setSno(int d);void setAge(int a); /添加成員函數(shù)的聲明;void printStu()/類外定義成員函數(shù)需加類名:,改為:void Student:printStu()coutnStudentNo.iscoutageisage.endl;void setSno(int

10、 s)/類外定義成員函數(shù)需加類名:,改為:void Student: setSno(int s)sno=s;voidsetAge(inta)/類外定義成員函數(shù)需加類名:,改為:void Student: setAge(int a)age = a;void main()Student lin; lin.setSno(20021); lin.setAge(20); lin.printStu();【3.29】指出下列程序中的錯(cuò)誤,并說明為什么。class Pointpublic:int x,y; private:Point()/構(gòu)造函數(shù)不能說明為 private 的x=1;y=2;void main

11、()Point cpoint;cpoint.x = 2;Point “Point copit時(shí)出現(xiàn)錯(cuò)誤。錯(cuò)誤的原因是:類Point cpoint 時(shí)不能調(diào)用它?!?.30】下面是一個(gè)計(jì)算器類的定義,請(qǐng)完成該類成員函數(shù)的實(shí)現(xiàn)。#include using namespace std; class Counterpublic:Counter(int number);voidincrement();/111void decrement();/給原值減1 intgetValue();/取得計(jì)數(shù)器值intprint();/顯示計(jì)數(shù)private:int value;counter:counter(int

12、 number)value = number;void counter:increment()value+;void counter:decrement()value-;int counter:getvalue()return value;int counter:print()coutnvalueis:return0;void main()counter op(10); PAGE PAGE 21op.print(); op.decrement(); op.print(); op.increment(); op.print();【3.31】根據(jù)注釋語句的提示,實(shí)現(xiàn)類 Date 的成員函數(shù)。#in

13、clude using namespace std; class Datepublic:voidprintDate();/顯示日期voidsetDay(intd);/void setMonth(int m);/設(shè)置月的值void setYear(int y);/設(shè)置年的值private:int day,month,year;void Date:printDate()coutyear/month/dayendl;void Date:setDay(int d)day = d;void Date:setMonth(int m)month = m;void Date:setYear(int y)yea

14、r = y ;intmain()Date testDay; testDay.setDay(5); testDay.setMonth(10); testDay.setYear(2003); testDay.printDate(); return 0;【3.32cylinder,cylinderdoublecylinderdoublecylinder中包含一個(gè)成員函數(shù)vol()cylinder對(duì)象的體積。#include using namespace std;const double class Cylinderdouble radius,height,volume; public:Cylind

15、er(double r,double h)radius=r; height=h;volume=PI*radius*radius*height;void vol()coutthe volume isvolumeendl;int main()Cylinder c(2,5); c.vol();return 0;【3.33qu 5 個(gè)元素的數(shù)組對(duì)象, qu 15price qu 10 qu*price 值?!?.34】修改上題,通過對(duì)象指針訪問對(duì)象數(shù)組,使程序以相反的順序顯示對(duì)象數(shù)組的 qu*price 值。#include using namespace std; class Bookprivate

16、:int qu,price; public: Book(int i); void print();void printqandp();Book:Book(int i)qu = i;price = qu * 10;void Book:print()coutthe Books qu is: quendl; coutthe Books price is: priceendl;void Book:printqandp()coutthe Books qu*price is: qu*priceendl;void main()Book a5 = 1,2,3,4,5;int i;for(i = 0; i 5;

17、 i+)ai.print();Book *b = &a4;/for(i = 0; i printqandp(); b-;Book *c = a;/for(i = 0; i printqandp(); c+;/或者class private:int qu,price; public:Book(int q,int p); void print();void printqandp();Book:Book(int q,int p)qu = q; price = p;void Book:print()coutthe Books qu is: quendl; coutthe Books price is:

18、 priceendl;void Book:printqandp()coutthe Books qu*price is: qu*priceendl;void main()Book a5 = Book(1,10),Book(2,20),Book(3,30),Book(4,40),Book(5,50);int i;for(i = 0; i printqandp(); b-;Book*ca;/for(i0;i5;i+)c-printqandp(); c+;【3.35stockcodequanprice3 個(gè)參數(shù):字符數(shù)組na。當(dāng)定義Stock 的類對(duì)象時(shí),將對(duì)象的第一個(gè)字符串參數(shù)賦給stockcode

19、2 3 quan price2 3 個(gè)參數(shù)時(shí),quan 8.98print()this Stock 1 個(gè)對(duì)象的三個(gè)參數(shù)為:“600001”、3000 5.672 1 個(gè)數(shù)據(jù)成員的值是“600001”2 3 個(gè)數(shù)據(jù)成員的值取默認(rèn)值。要求編寫程序分別顯示這兩個(gè)對(duì)象數(shù)據(jù)成員的值。#include #include using namespace std; class Stockpublic:Stock(char na, int q = 1000,double p =8.98);print(); private:char stockcode16; int quan;float price;Stock

20、:Stock(char na, int q,double p)strcpy(stockcode,quan =q;price=p;Stock:print()coutstocknode: stockcodequan: quan; coutprice:priceendl;void main()Stock a(600001,3000,5.67); Stock b(600001);a.print();b.print();【3.36計(jì)算出學(xué)生人數(shù)和平均成績(jī)(要求將學(xué)生人數(shù)和總成績(jī)用靜態(tài)數(shù)據(jù)成員表示。#include #include using namespace std; class Studentst

21、ring name; string sno; double score; static int num;staticdoublepublic:Student(string n,string no,double s); void print();static double getAverage(); static double gettotal(); static int getnum();int Student:num=0; double Student:sum=0;Student:Student(string n,string no,double s)name=n; sno=no; scor

22、e=s; num+; sum+=s;void Student:print()cout學(xué)生的學(xué)號(hào)為: sno, 姓名為:intStudent:getnum()name, 成績(jī)?yōu)椋簊coreendl;return num;double Student:getAverage()if (num=0)return 0;elsereturn sum/num;double Student:gettotal()return sum;int main()cout當(dāng)前學(xué)生人數(shù)為:Student:getnum()學(xué)生的總成績(jī)?yōu)椋篠tudent:gettotal()學(xué)生的平均成績(jī)?yōu)椋篠tudent:getAverag

23、e()endl;Student s1(12012,張明s1.print();cout當(dāng)前學(xué)生人數(shù)為:s1.getnum()學(xué)生的總成績(jī)?yōu)椋簊1.gettotal() s1.getAverage()endl;Student s2(09615,李濤s2.print();cout當(dāng)前學(xué)生人數(shù)為:s2.getnum()學(xué)生的總成績(jī)?yōu)椋簊2.gettotal() s2.getAverage()endl;Student s3(12016,王曉,90);s3.print();cout當(dāng)前學(xué)生人數(shù)為:s3.getnum()學(xué)生的總成績(jī)?yōu)椋簊3.gettotal() s3.getAverage()endl;re

24、turn 0;第四章 派生類與繼承【4.8】A【4.9】C【4.10】C【】B【4.12】程序運(yùn)行結(jié)果: Constructor B2 Constructor B1 Constructor A321【4.13】程序運(yùn)行結(jié)果: 主食=bread副食=steak【4.14】程序運(yùn)行結(jié)果:0,0,0220,1,03,6,9【4.15】程序運(yùn)行結(jié)果: Constructor B2.2 Constructor B1.1 Constructor B3.3 Constructor A.4124,3【4.16】程序運(yùn)行結(jié)果:4【4.17】程序運(yùn)行結(jié)果:(1,2)5,6(6,9)【4.18】程序運(yùn)行結(jié)果:cla

25、ss base1 class base2 class level2 class base2 class level1 class toplevel【4.19ASCII a 或任意兩個(gè)字符間)與所對(duì)應(yīng)的數(shù)字的對(duì)照表。#include using namespace std; #include class table PAGE PAGE 32public:table(int p,int q)i=p; j=q;void ascii(void);protected: int i,j;voidtable:ascii(void)int k=1; int m;for(m=i;m=j;m+)coutsetw(

26、4)m (char)m; if(k%12=0)coutn;k+;coutendl;class der_table:public tablepublic:der_table(int p,int q,char*m):table(p,q)c=m;void print(void); protected:char *c;void der_table:print(void)coutcendl; table:ascii();void main()der_table ob(a,z,ASCII value-char); ob.print();ob.print();【4.20】#include using nam

27、espace std; class area_clprotected:doubledouble width; public:area_cl(double r,double s)height=r; width=s;virtual double area()=0;class rectangle:public area_clpublic:rectangle(int h,int w):area_cl(h,w)double area()return height*width;class isosceles:public area_clpublic:isosceles(int h,int w):area_

28、cl(h,w)double area()return 0.5*height*width;int main()rectangle r(10,5); isosceles i(4,6); area_cl *p;p=&r;cout矩形的面積為:area()endl;p=&i;cout三角形的面積為:area()endl;return 0;【4.21Time DateBirthtimeTime DateChildname 用于表示小孩的名字,同事設(shè)計(jì)主程序顯示一個(gè)小孩的出生時(shí)間和名字。#include #include using namespace std;classTimepublic:Time(i

29、nt h,int m,ints)hours=h; minutes=m; seconds=s;void display()cout出生時(shí)間:hours時(shí)minutes分seconds秒endl;protected:int hours,minutes,seconds;class Datepublic:Date(int m,int d,inty)month=m; day=d; year=y;virtual void display()cout出生年月year年month月day日endl;protected:int month,day,year;class Birthtime:public Time

30、,public Dateprivate:string Childname; public:Birthtime(string name,int y,int mon,int d,int h,int m,int s):Date(mon,d,y),Time(h,m,s)Childname=name;void display()cout小孩的姓名為:Childname n ;Date:display();Time:display();int main()Birthtime mary(Mary,2013,1,1,13,25,40); mary.display();Birthtime jack(Jackso

31、n,2000,11,11,23,55,10); jack.display();return 0;【4.22】編寫一個(gè)學(xué)生和教師的數(shù)據(jù)輸入和顯示程序。學(xué)生數(shù)據(jù)有編號(hào)、姓名、系別和成績(jī),教師數(shù)Student Teacher 的基類。#include #include using namespace std; class Personstring sno,name; public:void Input()coutsno;coutname;void print()cout編號(hào):snoendl; cout姓名:nameendl;class Student:public Personstring depa

32、rtment; double score; public:void Input()Person:Input();coutdepartment;coutscore;void print()Person:print();cout班號(hào):departmentendl; cout成績(jī):scoreendl;class Teacher:public Personstring department; string prof;public:void Input()Person:Input();coutdepartment;coutprof;void print()Person:print();cout教師的部門

33、:departmentendl; cout教師的職稱:profendl;int main()Students;Teachercouts.Input();coutt.Input();couts.print();coutt.print();return 0;第五章 多態(tài)性【5.7】D【5.8】A【5.9】A【5.10】D【】C【5.12】C【5.13( 參數(shù)個(gè)數(shù)與參數(shù)類型的順序)都必須與基類中的原型完全相同。本程序段中,虛函數(shù)在派生類中重新定義 時(shí),參數(shù)個(gè)數(shù)與基類中的原型不同?!?.14】寫出程序運(yùn)行結(jié)果。76【5.15】寫出程序運(yùn)行結(jié)果。This is C+ book.第 1 個(gè)字符:T第 16

34、 個(gè)字符:k第 26 個(gè)字符:數(shù)組下標(biāo)越界!【5.16】寫出程序運(yùn)行結(jié)果。m=1.5 千米【5.17】編寫一個(gè)程序,用成員函數(shù)重載運(yùn)算符“+”和“-個(gè)二維數(shù)組的值由構(gòu)造函數(shù)設(shè)置,另一個(gè)二維數(shù)組的值由鍵盤輸入。#include using namespace std;class Arrayint M,N; doublepublic:Array(int m,int n,int *p);Array(int m,int n);void print(); void Input();Array operator+(Array Array operator-(Array b);Array:Array(int

35、 m,int n,int*a)M=m;N=n;p=new doublem*n; for(int i=0;im*n;i+)pi=ai;Array:Array(int m,int n)M=m;N=n;p=new doublem*n; for(int i=0;im*n;i+)pi=0;33void Array:print()for(int i=1;i=M;i+)for(int j=1;j=N;j+)coutp(i-1)*N+j-1t;coutendl;voidArray:Input()for(int i=0;ipi;Array Array:operator+(Array b)if(M !=b.M |

36、 N !=b.N)cout兩個(gè)數(shù)組的維數(shù)不同,不能進(jìn)行加運(yùn)算endl; exit(0);else PAGE PAGE 43Array s(M,N);for(int i=0;iM*N;i+)s.pi=pi+b.pi;return s;Array Array:operator-(Array b)if(M !=b.M | N !=b.N)cout兩個(gè)數(shù)組的維數(shù)不同,不能進(jìn)行加運(yùn)算endl; exit(0);elseArray s(M,N);for(int i=0;iM*N;i+)s.pi=pi-b.pi;return s;int main()int a6=1,2,3,4,5,6;Array ar1(

37、2,3,a);Array ar2(2,3);cout2*3 ar2 ar2.Input();coutar1為:endl; ar1.print();coutar2為:endl; ar2.print();Array ar3(2,3),ar4(2,3); ar3=ar1+ar2;ar4=ar1-ar2;coutar1+ar2=endl; ar3.print();coutar1-ar2=endl; ar4.print();return 0;【5.18】修改習(xí)題 5.17,用友元函數(shù)重載運(yùn)算符+和-將兩個(gè)二維數(shù)組相加或相減。#include class Arrayint M,N; doublepubli

38、c:Array(int m,int n,int*p);Array(int m,int n); void print();void Input();friendArrayoperator+(Arraya,ArrayfriendArrayoperator-(Arraya,Arrayb);Array:Array(int m,int n,int*a)M=m;N=n;p=new doublem*n; for(int i=0;im*n;i+)pi=ai;Array:Array(int m,int n)M=m;N=n;p=new doublem*n; for(int i=0;im*n;i+)pi=0;voi

39、d Array:print()for(int i=1;i=M;i+)for(int j=1;j=N;j+)coutp(i-1)*N+j-1t;coutendl;voidArray:Input()for(int i=0;ipi;Array operator+(Array a,Arrayb)if(a.M !=b.M | a.N != b.N)cout兩個(gè)數(shù)組的維數(shù)不同,不能進(jìn)行加運(yùn)算endl; return Array(1,1);elseArray s(a.M,a.N);for(int i=0;is.M*s.N;i+)s.pi=a.pi+b.pi;return s;Array operator-(

40、Array a,Arrayb)if(a.M !=b.M | a.N != b.N)cout兩個(gè)數(shù)組的維數(shù)不同,不能進(jìn)行加運(yùn)算endl; return Array(1,1);elseArray s(a.M,a.N);for(int i=0;is.M*s.N;i+)s.pi=a.pi-b.pi;return s;int main()int a6=1,2,3,4,5,6;Array ar1(2,3,a);Array ar2(2,3);cout2*3 ar2 ar2.Input();cout數(shù)組 ar1 為:endl;ar1.print();coutar2為:endl; ar2.print();Arr

41、ay ar3(2,3),ar4(2,3); ar3=ar1+ar2;ar4=ar1-ar2;coutar1+ar2=endl; ar3.print();coutar1-ar2=endl; ar4.print();return 0;【5.19】編寫一個(gè)程序,要求:聲明一個(gè)類Comple(復(fù)數(shù)類,定義類Complex的兩個(gè)對(duì)象c1和c,對(duì)象c1通過構(gòu)造函數(shù)直接指定復(fù)數(shù)的實(shí)部和虛部(doubleimag)2.53.7,對(duì)象c2通過構(gòu)4.26.5;c1、c2作;定義成員函數(shù)prin,調(diào)用該函數(shù)時(shí),以格式“real+imagi4.26.5print函數(shù)輸出格式為:4.2+6.5i;c1c2相加結(jié)果,并將

42、其結(jié)果輸出。#includeclass Complexdouble real,imag; public:Complex(double r=0,double i=0); void print();friend Complex operator+(Complex a,Complex b);Complex:Complex(double r,double i)real=r; imag=i;void Complex:print()cout(real,imag)endl;Complex operator+(Complex a,Complexb)Complex c; c.real=a.real+b.real

43、; c.imag=a.imag+b.imag; return c;int main()Complex c1(2.5,3.7),c2(4.2,6.5);coutc1=; c1.print(); coutc2=; c2.print();Complex c3=c1+c2;coutc1+c2=; c3.print();return 0;【5.20】寫一個(gè)程序,定義抽象類Container,由它派生出 3 球體柱體、Cube(正方體。用虛函數(shù)分別計(jì)算幾種圖形的表面積和體積。#include #include using namespace std;const double class Container

44、protected:double radius; public:Container(double r)radius=r;double getr()return radius;virtual double surface_area()=0;/virtualdoublevolume()=0;/volume;class Sphere: public Container/(球體)public:Sphere(double r):Container(r)double surface_area()return 4*PI*radius*radius;double volume()return PI*pow(r

45、adius,3)*4/3;class Cylinder: public Container/(圓柱體)double height; public:Cylinder(double r,double h):Container(r)height=h;double geth()return height;double surface_area()return 2*PI*radius*(height+radius);double volume()return PI*radius*radius*height;class Cube: public Container/(正方體)public:Cube(dou

46、ble r):Container(r)double surface_area()return 6*radius*radius;double volume()return pow(radius,3);int main()Container *ptr;Sphere s(2);Cylinder cy(1,5);Cube cb(5);44ptr=&s;cout球體的半徑為:getr()endl;cout球體的表面積為:surface_area()endl; cout球體的體積為:volume()endl;cout*endl; ptr=&cy;cout圓柱體的半徑為:getr()endl; cout圓柱

47、體的高為:cy.geth()endl;cout圓柱體的表面積為:surface_area()endl; cout圓柱體的體積為:volume()endl; cout*endl;ptr=&cb;cout正方體的邊長(zhǎng)為:getr()endl;cout正方體的表面積為:surface_area()endl; cout正方體的體積為:volume()endl;return 0;第六章 模板與異常處理【6.5】D【6.6】A【6.7】B【6.8】程序運(yùn)行結(jié)果: PAGE PAGE 54100.23XThis is atest.【6.9】程序運(yùn)行結(jié)果:4!=24n=-2 n!程序執(zhí)行結(jié)束?!?.10】指出

48、下列程序中的錯(cuò)誤,并說明原因。#include using namespace std;template class Comparepublic:Compare(T a, T b)x=a; y=b;T min(); private:T x,y;templateTCompare:min()/應(yīng)改為:TCompare:min()return (xy)?x:y;int main()Comparecom1(3,7);/應(yīng)改為:Comparecom1(3,7);cout其中的最小值是:com1.min()endl; return 0;【6.11】已知下列主函數(shù):int main()coutmin(10,

49、5,3)endl; coutmin(10.2,5.6,3.8)endl; coutmin(a,b,c)endl; return 0;聲明求 3 個(gè)數(shù)中最小者的函數(shù)模板,并寫出調(diào)用此函數(shù)模板的完整程序。#include using namespace std;template T min(T a, T b,T c)T t; t=(ac)t=c; return t;int main()coutmin(10,5,3)=min(10,5,3)endl; coutmin(10.2,5.6,3.8)=min(10.2,5.6,3.8)endl;coutmin(a,b,c)=min(a,b,c)endl;r

50、eturn 0;【6.12數(shù)組的類型可以是整型也可以是雙精度類型。#include using namespace std;template typename TMax(Ta,intsize=0)T max=a0;for(int i=1;isize;i+)if(maxai)max=ai;return max;int main()int a=23,45,1,37,2323,256,6;double b=1.2,4.56,34,6.785,23.13;int alength=sizeof(a)/sizeof(int);int blength=sizeof(b)/sizeof(double);cou

51、t數(shù)組 a 的元素為:;int i; for(i=0;ialength;i+)coutai;coutn 數(shù)組 a 的最大元素為:Max(a,alength)endl;cout數(shù)組 b 的元素為:; for(i=0;iblength;i+)coutbi;coutn 數(shù)組 b 的最大元素為:Max(b,blength)endl;return 0;【6.13】 編寫一個(gè)函數(shù)模板,使用冒泡排序?qū)?shù)組內(nèi)容由小到大排列并打印出來,并寫出調(diào)用此函數(shù)模板的完整程序,使得函數(shù)調(diào)用時(shí),數(shù)組的類型可以是整型也可以是雙精度型。#include using namespace std;template void Sor

52、t(T a,int T t;for(int i=1;i=size-1;i+)for(int j=0;jaj+1)t=aj; aj=aj+1; aj+1=t;int main()int a=23,45,1,37,2323,256,6;double b=1.2,4.56,34,6.785,23.13;int alength=sizeof(a)/sizeof(int); /存儲(chǔ)數(shù)組 a 的長(zhǎng)度intblength=sizeof(b)/sizeof(double);/b的長(zhǎng)度inti;/i為循環(huán)變量cout排序前數(shù)組 a 的元素為:; for(i=0;ialength;i+)coutai;Sort(a

53、,alength);coutn a for(i=0;ialength;i+)coutai;coutn b for(i=0;iblength;i+)coutbi;Sort(b,blength);coutn b for(i=0;iblength;i+)coutbi;coutendl;return 0;【6.1】 建立一個(gè)用來實(shí)現(xiàn)求3 個(gè)數(shù)和的類模板(將成員函數(shù)定義在類模板的內(nèi)部板的完整程序。#include using namespace std;template class Sumpublic:Sum(T a, T b, T c)x=a;y=b; z=c;T add()return x+y+z;

54、private:T x,y,z;int main()Sum s1(3,7,9); Sum s2(2.3,4.5,7.8);cout三個(gè)整數(shù)的和為:s1.add()endl; cout三個(gè) double 型數(shù)的和為:s2.add()endl;return 0;【6.156.14 改寫為在類模板外定義各成員函數(shù)。#include using namespace std;template class Sumpublic:Sum(T a, T b, T c);Tprivate:T x,y,z;template Sum:Sum(Ta,Tb,Tx=a; y=b; z=c;template T Sum:ad

55、d()return x+y+z;int main()Sum s1(3,7,9); Sum s2(2.3,4.5,7.8);cout三個(gè)整數(shù)的和為:s1.add()endl; cout三個(gè) double 型數(shù)的和為:s2.add()endl;return 0;第七章 C+的流類庫與輸入輸出【7.6】B【7.7】C【7.8】A【7.9】編一程序,分別計(jì)算 1!到 9!的值,使用 setw()控制“=”左邊的數(shù)值寬度。#include #include using namespace std;int main()int n=1;for(int i=1;i=9;i+)n=n*i; coutsetw(2

56、)i!=nendl;return 0;【7.10】編一程序,在屏幕上顯示一個(gè)由字母 A 組成的三角形。#include using namespace std;int main()int n=1;for(int i=1;i=7;i+)for(int j=1;j=7-i;j+)cout ;for(j=1;j2*i;j+)coutA;coutendl;return 0;【a b2 3 列,編一程序,求兩個(gè)矩陣之和。重載插入運(yùn)算符“運(yùn)算符“+c=a+。 #includeclass Matrixdouble p23; int row,col;public:Matrix();friend Matrix operator+(Matrix a,Matrix b);friend istream &operator(istream &in,Matrix &a); friend ostream &operator(ostream &out,Matrix ;Matrix:Matrix()for(int i=0;i2;i+)55for(i

溫馨提示

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

評(píng)論

0/150

提交評(píng)論