版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、112-5#include#include#includeusingnamespacestd;classTriangleinta,b,c;public:doubleArea()doubles=(a+b+c)/2;returnsqrt(s*(s-a)*(s-b)*(s-c);intPerimeter()returna+b+c;voidInput()cinabc;voidmain()Trianglet1;t1.Input();coutt1.Area()t1.Perimeter();/2-3#includeinlineintmax(inta,intb)if(ab)returna;elsereturn
2、b;inlineintmax(inta,intb,intc)returnmax(a,max(b,c);voidmain()intx=21,y=15,z=22,d;d=max(x,max(y,z);coutd;/2-4#includeintoverload(intn);charoverload(charn);voidmain()intn;chars;coutn;coutn=overload(n)endl;cout输入as;couta=;coutoverload(s)endl;intoverload(intn)(returnn;charoverload(charn)(coutbendl;retur
3、n0;/2-2#includeiostream”usingnamespacestd;inlinechartrans(charch);intmain()(charch;while(ch=getchar()!=n)couttrans(ch);cout=a&ch=z)returnch-32;elsereturnch+32;/3-1#includeusingstd:cout;usingstd:endl;intMin(inta,intb)(return(ab?a:b);intMin(inta,intb,intc)(return(Min(a,b)c?Min(a,b):c);intMin(inta,intb
4、,intc,intd)(return(Min(a,b,c)d?Min(a,b,c):d);voidmain(void)(coutMIN(1,2)=Min(1,2)endl;coutMIN(3,4,5)=Min(3,4,5)endl;coutMIN(4,5,6,7)=Min(4,5,6,7)endl;/3-2#includeusingnamespacestd;voidArea(floatr)(coutthecirclesareais:3.14*r*rendl;voidArea(floata,floatb)(couttherectangles(squares)areais:a*bendl;void
5、Area(floata,floatb,floath)(coutthetrapzoidsareais:(a+b)*h/2endl;voidmain()(Area(5);Area(3,3);Area(3,5,4);/3-3#includeusingnamespacestd;voidSort(inta,intn)(boolflag;for(inti=0;in-1;i+)(flag=false;for(intj=1;jaj)(inttemp=aj-1;aj-1=aj;aj=temp;flag=true;if(!flag)break;for(intm=0;mn;m+)coutam;coutendl;vo
6、idSort(floatb,intn)(boolflag;for(inti=0;in-1;i+)(flag=false;for(intj=1;jbj)(floattemp=bj-1;bj-1=bj;bj=temp;flag=true;if(!flag)break;for(intm=0;mn;m+)coutbm;coutendl;voidmain()(inta10=3,5,1,2,9,0,8,6,4,7;floatb10=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0;Sort(a,10);Sort(b,10);/3-4#includeusingnamespace
7、std;classRectanglefloata,b;public:Rectangle()coutinputthetwosideoftherectangle:ab;Rectangle(floatx,floaty):a(x),b(y)floatCir()return(2*(a+b);floatArea()return(a*b);voidmain()RectangleRe;couttherectanglescircumferenceis:Re.Cir()endltherectanglesareais:Re.Area()endl;/3-5#includeusingnamespacestd;class
8、Circlefloatr;public:Circle()coutinputtheradiusofthecircle:r;Circle(floatx):r(x)floatCir()return2*3.14*r;floatArea()(return3.14*r*r;;voidmain()(CircleC(5);couttheCirclescircumferenceis:C.Cir()endltheCirclesAreais:C.Area()endl;/4-1classstudent(intno;charname20;public:student();intgetno()const;char*get
9、name();#includestudent:student()(no=1;strcpy(name,wang);intstudent:getno()const(returnno;char*student:getname()(returnname;intmain()(students1;s1.getno();s1.getname();conststudents2;s2.getno();return0;/4-3#includeclassobjectfloata;floatweight;public:object(floatw);object()coutdestructweightendl;clas
10、sbox:publicobjectfloatb;floatheight,widt;public:box(floatw,floath,floatwi):object(w),height(h),widt(wi)coutconstructheightendl;coutconstructwidtendl;box()coutdestructheightendl;coutdestructwidtendl;object:object(floatw)weight=w;coutconstructweightendl;intmain()boxb(1.1,2.2,3.3);return0;/4-4#includei
11、ostreamusingnamespacestd;classbaseclasspublic:baseclass()couta.cendl;virtualbaseclass()coutadendl;classderivedclass:publicbaseclass(public:derivedclass()cout”b.cendl;derivedclass()cout”b.dendl;intmain()derivedclassobj;baseclass*p;p=&obj;return0;/4-5#includeiostream#includeusingnamespacestd;classDoum
12、entchar*name;public:Doument(char*na)name=newcharstrlen(na)+1;strcpy(name,na);virtualvoidprint()constcout姓名:nameendl;classBook:publicDoumentintpagecount;char*name;public:Book(intp,char*na):Doument(na)p=pagecount;name=newcharstrlen(na)+1;strcpy(name,na);voidprint()constcout页码:pagecount”姓名:nameendl;voi
13、ddisp(Doument&i)(i.print();intmain()(Bookb(23,小明);disp(b);return0;/5-1#includeiostream”usingnamespacestd;classpoint(private:intx;inty;intz;staticintnum;public:point(inta,intb,intc);intprint();intpoint:num=0;intmain()(pointp1(1,2,3);p1.print();pointp2(5,3,2);p1.print();pointp3(3,6,4);p1.print();retur
14、n0;point:point(inta,intb,intc)(x=a;y=b;z=c;num+;intpoint:print()cout第num个点x”,y”,zendl;return0;/5-2#include#includeusingnamespacestd;classStucharname15;intage;floatscore;public:staticintcount;staticfloatsum;Stu(char*p,inta,floats):age(a),score(s)strcpy(name,p);count+;sum+=score;Stu()coutinputthestude
15、ntsname,age,score:nameagescore;count+;sum+=score;intStu:count=0;floatStu:sum=0;voidmain()Stup(子机,19,98),q;coutthenumberofstudentsis:Stu:countendlthetotalscoreis:Stu:sumendl;/5-3#includeusingnamespacestd;classPointfloatx,y,z;public:Point(floata=0,floatb=0,floatc=0):x(a),y(b),z(c)Pointoperator+();Poin
16、toperator-();Pointoperator+(int);Pointoperator-(int);voidshow()cout”(x,y,z)=(x,y,z)endl;PointPoint:operator+()returnPoint(+x,+y,+z);PointPoint:operator-()returnPoint(-x,-y,-z);PointPoint:operator+(int)Pointtemp;temp.x=x+;temp.y=y+;temp.z=z+;returntemp;PointPoint:operator-(int)Pointtemp;temp.x=x-;tem
17、p.y=y-;temp.z=z-;returntemp;voidmain()Pointa(1,1,1),b(2,2,2),c,d;couta:;a. show();coutb:;b. show();coutendl;c=a+;coutc=a+n;coutc:;c. show();coutendl;d=+b;coutd=+bn;coutd:;d. show();coutNewaandb:n;a. show();b. show();coutendl;c=a-;coutc=a-n;coutc:;c. show();coutendl;d=-b;coutd=-bn;coutd:;d. show();co
18、utNewaandb:n;a. show();show();/5-4#includeusingnamespacestd;classComplexfloatreal,image;public:Complex(floata,floatb):real(a),image(b)Complex()friendbooloperator=(Complex&x,Complex&y)return(x.real=y.real&x.image=y.image);voidmain()ComplexCp1(1,2),Cp2(3,4),Cp3(1,2);cout(Cp1=Cp2)endl(Cp1=Cp3)endl;/6-1
19、#includeusingnamespacestd;classComplex(floatreal,image;public:Complex(floatx,floaty):real(x),image(y)Complex()Complexoperator+(Complex&a)returnComplex(real+a.real,image+a.image);Complexoperator-(Complex&b)returnComplex(real-b.real,image-b.image);friendostream&operator(ostream&output,Complex&c)output
20、(c.real,c.image);returnoutput;voidmain()ComplexCp1(1,-2),Cp2(2,5),Cp3,Cp4;Cp3=Cp1+Cp2;Cp4=Cp1-Cp2;coutCp1+Cp2=Cp3endlCp1-Cp2=Cp4endl;/6-2#includeusingnamespacestd;classcomputerfloatx,y;public:computer(floata=0,floatb=0):x(a),y(b)computeroperator+();computeroperator-();computeroperator+(int);computer
21、operator-(int);voidshow()cout(x,y)=(x,y)endl;computercomputer:operator+()(returncomputer(+x,+y);computercomputer:operator-()(returncomputer(-x,-y);computercomputer:operator+(int)(computertemp;temp.x=x+;temp.y=y+;returntemp;computercomputer:operator-(int)(computertemp;temp.x=x-;temp.y=y-;returntemp;v
22、oidmain()(computera(1,1),b(2,2),c,d;couta:;a. show();coutb:;b. show();coutendl;c=a+;coutc=a+n;coutc:;c. show();coutendl;d=+b;coutd=+bn;coutd:;d. show();coutNewaandb:n;a. show();b. show();coutendl;c=a-;coutc=a-n;coutc:;c. show();coutendl;d=-b;coutd=-bn;coutd:;d. show();coutNewaandb:n;a. show();show()
23、;/6-3#include#includeclassstringchar*xstring;public:string(char*str);string();string&operator=(conststring&);voidprint();string:string(char*str)xstring=newcharstrlen(str)+1;strcpy(xstring,str);string:string()deletexstring;voidstring:print()coutxstringendl;intmain()strings1(hello);strings2(yangchenhu
24、an);s2.print();s2=s1;cout修改后:;s2.print();return0;string&string:operator=(conststring&s)(if(this=&s)return*this;deletexstring;xstring=newcharstrlen(s.xstring)+1;strcpy(xstring,s.xstring);return*this;/6-4#include#includeusingnamespacestd;classString(public:String()(p=NULL;String(char*str);friendboolop
25、erator=(String&string1,String&string2);voiddisplay();private:char*p;String:String(char*str)(p=str;voidString:display()(coutp;输出P所指向的字符串booloperator=(String&string1,String&string2)(if(strcmp(string1.p,string2.p)=0)returntrue;elsereturnfalse;voidcompare(String&string1,String&string2)(if(operator=(stri
26、ng1,string2)=0)(string1.display();cout=;string2.display();elsecouterror!endl;intmain()(Stringstring1(Hello),string2(china);compare(string1,string2);return0;/6-5#include#includeusingnamespacestd;classString(char*p;public:String()p=NULL;String(constchar*s)p=newchar(strlen(s)+1);strcpy(p,s);char*operat
27、or+(constchar*s)char*temp=newchar(strlen(p)+strlen(s)+1);strcpy(temp,p);strcat(temp,s);returntemp;friendbooloperator=(constString&a,constString&b)return(!strcmp(a.p,b.p);friendostream&operator(ostream&output,constString&d)outputd.p;returnoutput;voidmain()StringStr1(Malagebi!”),Str2(Ready,Go!”),Str3;
28、Str3=Str1+nishi”;coutStr3endl(Str1=Str2)endl;/7-1#includeusingnamespacestd;classCar(intwnum;floatweight;public:Car(inta,floatb):wnum(a),weight(b)voidCshow()cout轮子个数:wnum个endl车重:weightTendl;classSmallCar:privateCarints_cnum;public:SmallCar(inta,floatb,intc):Car(a,b),s_cnum(c)voidSshow()Cshow();cout载客
29、量:s_cnum人次endl;classBigCar:privateCarintb_cnum;floatb_cweight;public:BigCar(inta,floatb,intc,floatd):Car(a,b),b_cnum(c),b_cweight(d)voidBshow()Cshow();cout载客量:b_cnum人次endl载重量b_cweightTendl;intmain()Carcar(4,8);SmallCarsmallcar(8,16,8);BigCarbigcar(16,32,16,32);cout汽车:endl;car.Cshow();coutn小车:endl;sm
30、allcar.Sshow();coutn卡车:endl;bigcar.Bshow();return0;/7-2#include#includeusingnamespacestd;classShapefloatx,y;public:Shape(floata,floatb):x(a),y(b)voidshow()cout左上角坐标(x,y)=(x,y”)n”;virtualfloatGetArea()=0;classRec:virtualpublicShapefloathigh,width;public:Rec(floatafloatb,floatc,floatd):Shape(a,b),high
31、(c),width(d)voidshow()cout矩形:n;Shape:show();cout矩形宽、高:widthhighendl;floatGetArea()show();returnhigh*width;classEll:virtualpublicShapefloatxx,yy;public:Ell(floata,floatb,floatc,floatd):Shape(a,b),xx(c),yy(d)voidshow()(cout椭圆:n;Shape:show();cout椭圆x轴,y轴:xxyyendl;floatGetArea()(show();return4*atan(1)*xx
32、*yy;intmain()(Recrec(5,5,5,5);Ellell(8,8,12,10);Shape*s;s=&rec;cout矩形的面积:GetArea()endlendl;s=ℓcout椭圆的面积:GetArea()endlendl;return0;/7-3#include/#includeusingnamespacestd;classmammal(intm;public:mammal(inti=0):m(i)(coutmammalisconstructendl;mammal()(coutmammalisdestructendl;classcat:publicmammal(i
33、ntc;public:cat(intj=0):c(j)coutcatisconstructendl;cat()(coutcatisdestructendl;classdog:publicmammal(intd;public:dog(intk=0):d(k)(coutdogisconstructendl;dog()(coutdogisdestructendl;intmain()(catc;dogd;return0;/7-4p39.7/8-1#includeusingnamespacestd;templatedoublesum(T1a,T2b)(return(a+b);voidmain()(coutsum(1,3)endl;coutsum(1.2,3)endl;coutsum(1,3.1)endl;coutsum(1.2,3.34)endl;/8-2P课本179/8-3#includeusingnamespacestd;constintM=100;templ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 五年级科学上册生物与环境第6课做一个生态瓶教学建议教科版
- 2024-2025学年新教材高中数学第9章平面向量9.3向量基本定理及坐标表示9.3.3向量平行的坐标表示课时素养评价含解析苏教版必修第二册
- 2024年商标许可使用合同标的商标使用范围及限制条件
- PROTAC-SMARCA2-4-degrader-29-生命科学试剂-MCE
- 放飞梦想让孩子自由成长计划
- 2024年云安全防护服务合同
- 2024年二手房交易过程中的信息保密合同
- 通风管理制度
- 煤矿紧急处置权、撤人避险制度
- 2024年地铁隧道土方工程承包合同
- 锂电池供应商的合作协议书范文
- 杭州市2025届高三教学质量检测(一模) 英语试题卷(含答案解析)
- 培训教学课件模板
- 系统架构师论文(经典范文6篇)
- 降低患者外出检查漏检率-品管圈课件
- 五年级上册生命安全教育全册教案
- 2024年中国烟花鞭炮市场调查研究报告
- 安全标准化安全培训试题附参考答案【考试直接用】
- 第二单元 成长的时空(知识清单)-【上好课】2024-2025学年六年级道德与法治全一册同步课堂(统编版五四制2024)
- -流体力学-流体力学基本方程名师公开课获奖课件百校联赛一等奖课件
- 2024年分项、分部、单位工程验收制度范文(二篇)
评论
0/150
提交评论