版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、112-5#in clude#in clude#in cludeusing n amespace std;class Trian gle int a,b,c;public: double Area() double s=(a+b+c)/2;return sqrt(s*(s-a)*(s-b)*(s-c); ;int Perimeter() retur n a+b+c;void In put() cin abc; ;void mai n() Triangle t1;t1.I nput();coutt1.Area() t1.Perimeter(); /2-3#in cludein li ne int
2、 max(i nt a,i nt b)if(ab)return a;else return b;in li ne int max(i nt a,i nt b,i nt c)return max(a,max(b,c);void mai n()int x=21,y=15,z=22,d; d=max(x,max(y,z);coutd;/2-4#in cludeint overload(i nt n);char overload(char n);void mai n()int n;char s;cout输入 5;cinn;cout n= overload( n)en dl; cout输入 a s;co
3、uta=; coutoverload(s)e ndl;int overload(i nt n)return n;char overload(char n)coutbe ndl;return 0;112-2#i ncludeiostreamusing n amespace std;in li ne char tran s(char ch);int mai n()char ch;while(ch=getchar()!=n) couttra ns(ch);cout=a&ch=z)return ch-32;elsereturn ch+32;3-1#in cludeusing std:cout;usin
4、g std:e ndl;int Min(int a,int b)retur n (ab?a:b);int Mi n(i nt a,i nt b,i nt c)return (Mi n(a,b)c?Mi n(a,b):c);int Min (i nt a,i nt b,i nt c,i nt d)return (Mi n(a,b,c)d?Mi n(a,b,c):d);void mai n(void)coutMIN(1,2)=Mi n(1,2)e ndl; coutMIN(3,4,5)=Mi n(3,4,5)e ndl; coutMIN(4,5,6,7)=Mi n(4,5,6,7)e ndl;3-
5、2#in cludeusing n amespace std;void Area(float r)coutthe circles area is: 3.14*r*re ndl;void Area(float a,float b)coutthe recta ngles(squares) area is: a*be ndl;void Area(float a,float b,float h)coutthe trapzoids area is: (a+b)*h/2e ndl;void mai n()Area(5);Area(3,3);Area(3,5,4);3-3#in cludeusing n a
6、mespace std;void Sort(int a,int n)bool flag;for(i nt i=0;i n-1;i+)flag=false;for(i nt j=1;jaj)int temp=aj-1;aj-1=aj;aj=temp;flag=true;if(!flag)break;for(i nt m=0;m n ;m+)coutam;coute ndl;void Sort(float b,i nt n)bool flag;for(i nt i=0;i n-1;i+)flag=false;for(i nt j=1;jbj)float temp=bj-1;bj-1=bj;bj=t
7、emp;flag=true;if(!flag) break;for(i nt m=0;m n ;m+)coutbm;coute ndl;void mai n()int a10=3,5,1,2,9,0,8,6,4,7;float b10=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);113-4#in cludeusing n amespace std;class Recta nglefloat a,b;public:Recta ngle()couti nput the two side of the recta ngl
8、e: ab;Recta ngle(float x,float y):a(x),b(y)float Cir()return (2*(a+b);float Area()return (a*b);void mai n()Recta ngle Re;coutthe recta ngles circumfere nee is:Re.Cir()e ndl the recta ngles area is:Re.Area()e ndl;3-5#in cludeusing n amespace std;class Circlefloat r;public:Circle()couti nput the radiu
9、s of the circle:e ndl; cinr;Circle(float x):r(x)float Cir()return 2*3.14*r;float Area()return 3.14*r*r; ;void mai n()Circle C(5);cout the Circles circumfere nee is:C.Cir()e ndl the Circles Area is:C.Area()endl;4-1class stude ntint no;char n ame20;public:stude nt();int get no ()c on st;char*get name(
10、);#in cludestude nt:stude nt()n o=1;strcpy (n ame,wa ng);int stude nt:get no ()c onstreturn no;char*stude nt:get name()return n ame;int mai n()stude nt s1;sl.get no ();sl.get name();const stude nt s2;s2.get no();return 0;4-3#in cludeclass objectfloat a;float weight;public:object(float w);object()cou
11、tdestruct weighte ndl;class box:public objectfloat b;float height,widt;public:box(float w,float h,float wi):object(w),height(h),widt(wi)coutc on struct heighte ndl;coutc on struct widte ndl;box()coutdestruct heighte ndl;coutdestruct widte ndl;object:object(float w)weight=w;coutc on struct weighte nd
12、l;int mai n()box b(1.1,2.2,3.3);return 0;4-4#i ncludeiostreamusing n amespace std;class baseclasspublic:baseclass()couta.ce ndl;virtual baseclass()coutade ndl;class derivedclass:public baseclasspublic:derivedclass()coutb.ce ndl;derivedclass()coutb.de ndl;int mai n()derivedclass obj;baseclass *p;p=&o
13、bj;return 0;4-5#i ncludeiostream#in cludeusing n amespace std;class Doume ntchar *n ame;public:Doume nt(char *na)n ame=new char strle n (n a)+1;strcpy( name ,n a);n amee ndl;virtual void prin t() const cout姓名:;class Book:public Doume ntint pageco unt;char *n ame;public:Book(i nt p,char *n a):Doume n
14、t(na)p=pageco unt;n ame=new char strle n (n a)+1;strcpy (n ame ,n a);页码:pagecount姓名:nameendl;void prin t() const cout;void disp(Doume nt& i)i.pri nt();int mai n()Book b(23, 小明); disp(b);return 0;5-1#i ncludeiostreamusing n amespace std;class pointprivate:int兀int y;int乙static int num;public:poin t(i
15、nt a,i nt b,i nt c); int prin t();int poin t: num=0;int mai n()poi nt p1(1,2,3);pl.pri nt();poi nt p2(5,3,2);pl.pri nt();poi nt p3(3,6,4);pl.pri nt();return 0;poin t:po in t(i nt a,i nt b,i nt c) x=a;y=b;z=c;nu m+;int poin t:pri nt()cout第num个点x,y,zendl; return 0;5-2#in clude#in cludeusing n amespace
16、 std;class Stuchar n ame15;int age;float score;public:static int count;static float sum;Stu(char *p,i nt a,float s):age(a),score(s)strcpy( name,p);coun t+;sum+=score;Stu()couti nput the stude nts n ame,age,score:agescore;coun t+;sum+=score;int Stu:co un t=0;float Stu:sum=0;void mai n()Stu p(子机,19,98
17、),q;coutthe nu mber of stude nts is: Stu:co un te ndlthe total score is: Stu:sume ndl;5-3#in cludeusing n amespace std;class Pointfloat x,y,z;public:Poi nt(float a=0,float b=0,float c=0):x(a),y(b),z(c)Point operator +();Point operator -();Point operator +(i nt);Point operator -(i nt);void show()cout
18、( x , y , z )=( x , y , z )e ndl;Point Poin t:operator +()return Poin t(+x,+y,+z);Point Poin t:operator -()return Poi nt(-x,-y,-z);Point Poin t:operator +(i nt)Point temp;temp.x=x+;temp.y=y+;temp.z=z+;return temp;Point Poin t:operator -(i nt)Point temp;temp.x=x-;temp.y=y-;temp.z=z-;return temp;void
19、mai n()Poi nt a(1,1,1),b(2,2,2),c,d;couta :;a. show();coutb :;b. show();coute ndl;c=a+;coutc = a+n;coutc :;c. show();coute ndl;d=+b;coutd = +bn;coutd :;d. show();coutNew a and b :n;a. show();b. show();coute ndl;c=a-;coutc = a-n;coutc :;c. show();coute ndl;d=_b;coutd = -bn;coutd :;d. show();coutNew a
20、 and b :n;a. show();b. show();5-4#in cludeusing n amespace std;class Complexfloat real,image;public:Complex(float a,float b):real(a),image(b)Complex()friend bool operator =(Complex &x,Complex &y)retur n (x.real=y.rea I& x.image=y.image);void mai n()Complex Cp1(1,2),Cp2(3,4),Cp3(1,2);cout(Cp1=Cp2)e n
21、dl(Cp1=Cp3)e ndl;6-1#in cludeusing n amespace std;class Complexfloat real,image;public:Complex(float x,float y):real(x),image(y)Complex()Complex operator +(Complex &a)retur n Complex(real+a.real,image+a.image);Complex operator -(Complex &b)return Complex(real-b.real,image-b.image);friend ostream & o
22、perator (ostream &output,Complex &c) output(c.real,c.image);return output;void mai n()Complex Cp1(1,-2),Cp2(2,5),Cp3,Cp4;Cp3=Cp1+Cp2;Cp4=Cp1-Cp2; coutCp1+Cp2=Cp3e ndl Cp1-Cp2=Cp4e ndl;6-2#in cludeusing n amespace std;class computerfloat x,y;public:computer(float a=0,float b=0):x (a) ,y(b)computer op
23、erator +();computer operator -();computer operator +(i nt);computer operator -(i nt);void show()cout( x , y )=( x , y )e ndl;computer computer:operator +()retur n computer(+x,+y);computer computer:operator -()return computer(_x,_y);computer computer:operator +(i nt)computer temp;temp.x=x+;temp.y=y+;
24、return temp;computer computer:operator -(i nt)computer temp;temp.x=x-;temp.y=y_;return temp;void mai n()computer a(1,1),b(2,2),c,d;couta :;a. show();coutb :;b. show();coute ndl;c=a+;coutc = a+n;coutc :;c. show();coute ndl;d=+b;coutd = +bn;coutd :;d. show();coutNew a and b :n;a. show();b. show();cout
25、e ndl;c=a-;coutc = a-n;coutc :;c. show();coute ndl;d=_b;coutd = -bn;coutd :;d. show();coutNew a and b :n;a. show();b. show();6-3#in clude#in cludeclass stri ngchar *xstri ng;public:stri ng(char *str);stri ng();string &operator=(const string &); void prin t();stri ng:stri ng(char *str)xstri ng=new ch
26、ar strle n( str)+1; strcpy(xstri ng,str);stri ng:stri ng()delete xstri ng;void stri ng:pri nt()coutxstri nge ndl;int mai n()stri ng s1(hello);stri ng s2(ya ngche nhua n); s2.pri nt();s2=s1;cout修改后:;s2.pri nt();return 0;string & string :operator=(const string &s)if(this=&s)return *this;delete xstri n
27、g;xstring=new char strlen(s.xstring)+1;strcpy(xstri ng,s.xstri ng);return *this;6-4#in clude #in clude using n amespace std;class String public:String() p=NULL;Strin g(char *str);friend bool operator=(String &string1,String &string2); void display();private:char *p; ;Strin g:Stri ng(char *str) p=str
28、; void Strin g:display() coutp; II 输出P所指向的字符串bool operator=(String &string1,String &string2) if(strcmp(stri ng1.p,stri ng2.p)=0)return true;elsereturn false; void compare(String &string1,String &string2)if(operator=(stri ng1,stri ng2)=0) stri ng1.display();cout=;stri ng2.display();else couterror!e n
29、dl;int mai n() Stri ng stri ng1(Hello),stri ng2(chi na);compare(stri ng1,stri ng2);return 0;6-5#in clude#in cludeusing n amespace std;class Stringchar *p;public:Stri ng()p=NULL;Strin g(c onst char *s)p=new char(strle n( s)+1);strcpy(p,s);char * operator +(c onst char *s)char *temp=new char(strle n(
30、p)+strle n( s)+1); strcpy(temp,p);strcat(temp,s);return temp;friend bool operator =(c onst String &a,c onst String &b) return (!strcmp(a.p,b.p);friend ostream &operator (ostream &output,c onst String &d) outputd.p;retur n output;void mai n()Stri ng Str1(Malagebi!),Str2(Ready,Go!),Str3;Str3=Str1+ ni
31、shi;coutStr3e ndl(Str1=Str2)e ndl;7-1#in cludeusing n amespace std;class Carint wnum;float weight;public:Car(i nt a,float b):w nu m(a),weight(b)void Cshow()cout轮子个数:wnum个endl车重:weight Te ndl;class SmallCar:private Carint s_cnum;public:SmallCar(i nt a,float b,i nt c):Car(a,b),s_cnum(c)void Sshow()Csh
32、ow();cout载客量:s_cnum 人次endl;class BigCar:private Carint b_cnum;float b_cweight;public:BigCar(int a,float b,int c,float d):Car(a,b),b_cnum(c),b_cweight(d)void Bshow()Cshow();cout载客量: b_cnum人 次endl载重量 b_cweightTe ndl;int mai n()Car car(4,8);SmallCar smallcar(8,16,8);BigCar bigcar(16,32,16,32);cout汽车:en
33、dl;car.Cshow();coutn 小车:e ndl;smallcar.Sshow();coutn 卡车:e ndl;bigcar.Bshow();return 0;7-2#in clude#in cludeusing n amespace std;class Shapefloat x,y;public:Shape(float a,float b):x(a),y(b)void show()cout左上角坐标(x,y)= (x,y)n;virtual float GetArea()=0;class Rec:virtual public Shapefloat high,width;publi
34、c:Rec(float a,float b,float c,float d):Shape(a,b),high(c),width(d) void show()cout矩形:n;Shape:show();cout矩形宽、高:width highendl;float GetArea()show();return high*width;class Ell:virtual public Shapefloat xx,yy;public:Ell(float a,float b,float c,float d):Shape(a,b),xx(c),yy(d)void show()cout椭圆:n;Shape:s
35、how();cout椭圆 x 轴,y 轴:xx yyendl;float GetArea()show();return 4*ata n(1)*xx*yy;int mai n()Rec rec(5,5,5,5);Ell ell(8,8,12,10);Shape *s;s=&rec;cout矩形的面积:GetArea()endlendl; s=ℓcout椭圆的面积:GetArea()endlendl; return 0;7-3#in clude #in clude using n amespace std;class mammal int m;public:mammal(i nt i=0)
36、:m(i)coutmammal is con structe ndl;mammal()coutmammal is destructive ndl;class cat:public mammalint c;public:cat( int j=0):c(j)cout cat is con structe ndl;cat()coutcat is destructive ndl;;class dog:public mammalint d;public:dog(int k=O):d(k)cout dog is con structe ndl;dog()coutdog is destructe ndl;int mai n()cat c;dog d;return 0;7-4p39.78-1#in cludeusing n amespace std;template double sum(T1 a,T2 b)return (a+b);void mai n()coutsum(1,3)e ndl;coutsum(1.2,3)e ndl;coutsum(1,3.1)e ndl; coutsum(1.2,3.34)e ndl;8-2P 课本 179118-3#in cludeusing n amespace std;con st int M=100;template class
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年校园扩建工程沉降监测与基础施工质量验收合同3篇
- 2024年度饭店食材供应及厨房设备租赁合同
- 办公环境下的儿童户外活动管理
- 2024年带庭院别墅长期租赁合同范本3篇
- 利用网络平台推动学校图书馆的建设与发展
- 以心理健康教育为导向的小学数学教学设计
- 商业学校操场运动场地装饰设计与品牌形象塑造
- 医疗行业客户服务标准与挑战
- 2025中国铁塔江西省分公司社会招聘51人高频重点提升(共500题)附带答案详解
- 2025中国移动上海公司春季校园招聘高频重点提升(共500题)附带答案详解
- 对承租方有利的商铺租赁合同
- 投标突发事件应急预案
- EPC项目土建设计的重难点分析及解决措施
- 医院保安服务应急预案
- 2024年广东省揭阳市榕城区实验小学小升初衔接问卷数学试卷
- 江西警察学院治安学专业主干课程教学大纲 文档
- 浙江大学2011–2012学年冬季学期《高级数据结构与算法分析》课程期末考试试卷
- MOOC 信号与系统-西北工业大学 中国大学慕课答案
- 手术保护性约束
- 砌砖工程实训总结报告
- 低空经济研究报告-中国低空经济行业市场现状调查及未来发展趋势报告(2020-2023年)
评论
0/150
提交评论