版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
小程序练习代码:定义重载函数max3用于计算三个数的最大值(参数类型分别为int和double)#include<iostream>usingnamespacestd;intMAX3(inta,intb,intc){ intmax; max=a>b?a:b; returnmax>c?max:c;}doubleMAX3(doublea,doubleb,doublec){ doublemax; max=a>b?a:b; returnmax>c?max:c;}intmain(){ into,p,q; cout<<"Enterthreenumber:"<<endl; cin>>o>>p>>q; cout<<"thebiggestnumberis:"<<MAX3(o,p,q)<<endl; doublex,y,z; cout<<"Enterthreenumber"<<endl; cin>>x>>y>>z; cout<<"thebiggestnumberis:"<<MAX3(x,y,z)<<endl; return0;}定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。定义line类,端点由两个point类的对象组成,包括构造函数,析构函数以及计算线段长度的函数getlength。在main函数中,定义line的对象,并输出其长度#include<iostream>#include<cmath>usingnamespacestd;classpoint{private: intx,y;public: point(intxx=0,intyy=0):x(xx),y(yy){} point(point&p); intsetx(){} intgetx() { returnx; } intsety(){} intgety() { returny; } ~point(){}};point::point(point&p){ x=p.x; y=p.y;}classLine{private: pointp1,p2; doublelen;public: Line(pointxp1,pointxp2); Line(Line&l); doublegetlength() { returnlen; } ~Line(){}};Line::Line(pointxp1,pointxp2):p1(xp1),p2(xp2){ cout<<"callingtheconstructor..."<<endl; doublex=static_cast<double>(p1.getx()-p2.getx()); doubley=static_cast<double>(p1.gety()-p2.gety()); len=sqrt(x*x+y*y);}Line::Line(Line&l):p1(l.p1),p2(l.p2){ len=l.len;}intmain(){ pointmyp1(1,1),myp2(4,5); Lineline(myp1,myp2); Lineline2(line); cout<<"thelengthofthelineis:"; cout<<line.getlength()<<endl; cout<<"thelengthoftheline2is:"; cout<<line2.getlength()<<endl; return0;}定义student类,数据成员包括姓名name和成绩score,成员函数包括构造函数,拷贝构造函数和析构函数。定义函数voidhighestscore(students[]),输出分数最高的学生姓名和分数。在main函数中定义students[N],调用highestscore函数,输出分数最高的学生姓名和分数#include<iostream>#include<string>usingnamespacestd;constintN=5;classstudent{private: stringname; doublescore;public: student(stringn="zhangsan",doubles=0):name(n),score(s){} student(student&s) { this->name=; this->score=s.score; } ~student(){} friendvoidhighestscore(students[]);};voidhighestscore(students[]){ students0; for(inti=0;i<N;i++) { if(s0.score<s[i].score) s0=s[i]; } cout<<"thestudentwhogetthehighestscoreis:"<<<<endl; cout<<"thehighestscoreis:"<<s0.score<<endl;}intmain(){ students[N]= { student("studentone",10), student("studenttwo",30), student("studnetthree",90), student("studentfour",50), student("studentfive",70) }; highestscore(s); return0;}设计一个书类,能够保存书名、定价,所有书的本数和总价。(将书名和定价设计为普通数据成员;将书的本数和总价设计为静态数据成员)#include<iostream>#include<string>usingnamespacestd;classbook{private: stringbookname; doubleprice; staticintbooknum; staticdoubleallprice;public: book(stringbna="C++",doublep=0):bookname(bna),price(p) { booknum++; allprice=allprice+price; } ~book(){} voidBookname() { cout<<"thebooknameis:"<<bookname<<endl; cout<<"thepriceis:"<<price<<endl; } voidshowbook() { cout<<"thebooknumberis:"<<booknum<<endl; cout<<"allthebookpriceis:"<<allprice<<endl; }};intbook::booknum=0;doublebook::allprice=0;voidmain(){ books1("ChineseBook",95);s1.Bookname(); books2("C++Book",59); s2.Bookname(); s2.showbook();}定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。在main函数中,用new和delete分配和释放N个point的数组。(N是const常量,N=10)#include<iostream>usingnamespacestd;constN=10;classpoint{private: intx,y;public: point(intxx=0,intyy=0):x(xx),y(yy){} point(constpoint&pt) { x=pt.x; y=pt.y; } voidsetx(intxx) { setx(xx); } intgetx() { returnx; } voidsety(intyy) { sety(yy); } intgety() { returny; } voidshowpoint() { cout<<"x="<<x<<",y="<<y<<endl; } ~point(){}};voidmain(){ point*pt=newpoint[N]; (*pt).setx(1); (*pt).sety(1); (*pt).showpoint(); delete[]pt;}定义一个point类,包含私有数据成员x,y,成员函数包括无参构造函数,带参构造函数,set和get属性函数。定义circle类,从point类公有派生,增加数据成员半径r,成员函数包括无参构造函数,带参构造函数,计算面积函数getarea。在main函数中定义一个circle的对象,并计算其面积#include<iostream>usingnamespacestd;#definepi3.14classpoint{private: doublex,y;public: point(){} point(doublexx,doubleyy):x(xx),y(yy){} voidsetx(doublexx) { setx(xx); } intgetx() { returnx; } voidsety(doubleyy) { sety(yy); } intgety() { returny; }};classcircle:publicpoint{private: doubler;public: circle(){} circle(doublexx,doubleyy,doublerr):point(xx,yy),r(rr){} voidgetarea() { doublearea; area=pi*r*r; cout<<"theareais:"<<area<<endl; }};voidmain(){ circlec(3,4,5); c.getarea();}定义vehicle类,数据成员包括私有的weight,公有的构造函数,析构函数和输出函数dispaly;从vehicle类公有派生car类,增加数据成员载人数personnum,公有的构造函数,析构函数和输出display;从vehicle类公有派生truck类,增加数据成员载货量laod,公有的构造函数,析构函数和输出函数display;从car类和truck类共同公有派生出pickup类,包括公有的构造函数和输出函数。在main函数中,定义pickup类对象,并输出其基本信息#include<iostream>usingnamespacestd;classvehicle{private: intweight;public: vehicle(intw):weight(w){} voiddisplay() { cout<<"theweightis:"<<weight<<endl; } ~vehicle(){}};classcar:virtualpublicvehicle{private: intpersonnum;public: car(intw,intp):vehicle(w),personnum(p){} voiddisplay() { cout<<"thrpersonnumis:"<<personnum<<endl; } ~car(){}};classtruck:virtualpublicvehicle{private: intlaod;public: truck(intw,intl):vehicle(w),laod(l){} voiddisplay() { cout<<"thelaodis:"<<laod<<endl; } ~truck(){}};classpickup:publiccar,publictruck{public: pickup(intw1,intw2,intv,intp,intl):vehicle(v),car(w1,p),truck(w2,l){} voiddisplay() { vehicle::display(); car::display(); truck::display(); }};voidmain(){ pickupP(10,20,30,40,50); P.display();}定义一个计数器类counter,具备自增,自减功能(前后缀);输入输出>>,<<功能。在main函数里测试该类#include<iostream.h>classcounter{private: intn;public: counter(){} counter(intnn):n(nn){} counter&operator++(); counter&operator--(); counteroperator++(int); counteroperator--(int); friendostream&operator<<(ostream&os,constcounter&c); friendistream&operator>>(istream&is,constcounter&c);};counter&counter::operator++(){ n++; return*this;}counter&counter::operator--(){ n--; return*this;}countercounter::operator++(int){ counterc=*this; ++(*this); returnc;}countercounter::operator--(int){ counterc=*this; --(*this); returnc;}ostream&operator<<(ostream&os,constcounter&c){ os<<c.n; returnos;}istream&operator>>(istream&is,constcounter&c){ is>>c.n; returnis;}voidmain(){ counterc(5); c++; cout<<c<<endl;}定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生point类,增加私有数据成员x,y坐标,以及构造函数,析构函数。从point公有派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义哪个虚函数)。在main函数中,定义shape类的指针,指向派生类的对象,输出三类对象的基本信息,面积,体积;(将shape指针改为引用再尝试)#include<iostream>usingnamespacestd;classshape{public: virtualdoublearea()=0; virtualdoublevolume()=0; virtualvoidprintinfo()=0;};classpoint:publicshape{private: intx; inty;public: point(intx,inty):x(x),y(y){} doublearea() { return0; } doublevolume() { return0; } voidprintinfo() { cout<<"point:("<<x<<","<<y<<")"<<endl; } ~point(){}};classcircle:publicpoint{private: intr;public: circle(intx,inty,intr):point(x,y),r(r){} doublearea() { return(3.1415*r*r); } voidprintinfo() { cout<<"r="<<r<<endl; cout<<"area="<<area()<<endl; } ~circle(){}};classcylinder:publiccircle{private: inth;public: cylinder(intx,inty,intr,inth):circle(x,y,r),h(h){} doublevolume() { returncircle::area()*h; } voidprintinfo() { cout<<"h="<<h<<endl; cout<<"volume="<<volume()<<endl; } ~cylinder(){}};voidfun(shape*p){ p->printinfo();}intmain(){ pointp(1,1); circlec(1,1,2); cylinderC(1,1,2,2); fun(&p); fun(&c); fun(&C); return0;}设计一个堆栈的类模板Stack,在模板中用类型参数T表示栈中存放的数据,用非类型参数MAXSIZE代表栈的大小#include<iostream>usingnamespacestd;constmaxsize=5;template<typenameT>classstack{private: Ts[maxsize]; inttop;public: stack() { top=0; } voidpush(Te) {if(maxsize==top) { cout<<"栈满"<<endl; return; } s[top]=e; top++; } Tpop() { if(top==0) { cout<<"栈空"<<endl; return-1; } top--; returns[top]; }};voidmain(){ stack<int>s; s.push(3); cout<<s.pop();}定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 老年旅行安全护理的专家建议与案例
- 甘肃省临洮县2025-2026学年初三下学期期中质量检测试题数学试题含解析
- 浙江省临海市第五教研区市级名校2026届初三第一次诊断化学试题含解析
- 重庆市铜梁区市级名校2026届中考化学试题考前模拟题含解析
- 江苏省无锡市江阴初级中学2026年初三中考模拟冲刺卷(提优卷)(一)物理试题含解析
- 浙江经济职业技术学院《环境监测综合实验》2024-2025学年第二学期期末试卷
- 湖北省孝感市八校联谊-2026年中考模拟考试物理试题试卷含解析
- 跨学科合作在康复护理中的重要性
- 2026年河北邯郸高三一模高考数学试卷答案详解(精校打印)
- 遗传性血液病护理
- 初中英语阅读-篇章结构强化练习(附答案)
- 律师事务所投标书(文档)
- 产钳助产护理查房范文
- 公司规章制度及公司规章制度汇编
- ISO22000-2018全套程序文件模板
- 芯片提取基础知识课件
- 《预防血管内导管相关血流感染过程质控工具包》解读
- JJF 1033-2023计量标准考核规范
- 《中国饮食文化》第1章 中国饮食文化的历史发展
- 回顺炮掘工程施工组织设计
- 输电线路消缺修理施工方案
评论
0/150
提交评论