您好,欢迎访问三七文档
1函数重载定义重载函数max3用于计算三个数的最大值(参数类型分别为int和double)。#includeiostreamusingnamespacestd;intmax3(intx,inty,intz){return((xy?x:y)(xz?x:z)?(xy?x:y):(xz?x:z));}doublemax3(doublex,doubley,doublez){return((xy?x:y)(xz?x:z)?(xy?x:y):(xz?x:z));}voidmain(){inta,b,c;coutPleaseinputthreeinteger:;cinabc;coutTheMaxofthreeismax3(a,b,c)endl;doublea1,b1,c1;coutPleaseinputthreerealnumber:;cina1b1c1;coutTheMaxofthreeismax3(a1,b1,c1)endl;}2类的组合定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。定义line类,端点由两个point类的对象组成,包括构造函数,析构函数以及计算线段长度的函数getlength。在main函数中,定义line的对象,并输出其长度。#includeiostream#includecmathusingnamespacestd;classpoint{private:doublex,y;public:point(){}point(doublex,doubley):x(x),y(y){}voidsetx(doublexx){x=xx;}doublegetx(){returnx;}voidsety(doubleyy){y=yy;}doublegety(){returny;}point(point&p){x=p.x;y=p.y;}~point(){}};classline{private:pointa,b;public:line(pointaa,pointbb):a(aa),b(bb){}doublegetlength(){doublelength;length=sqrt(pow((a.getx()-b.getx()),2)+pow((a.gety()-b.gety()),2));returnlength;}~line(){}};voidmain(){pointp1(2,3);pointp2(5,6);linel1(p1,p2);coutThelengthofthelineisl1.getlength()endl;}3对象数组和函数定义student类,数据成员包括姓名name和成绩score,成员函数包括构造函数,拷贝构造函数和析构函数。定义函数voidhighestscore(students[]),输出分数最高的学生姓名和分数。在main函数中定义students[N],调用highestscore函数,输出分数最高的学生姓名和分数。#includeiostream#includeiomanip#includestringconstintN=2;usingnamespacestd;classstudent{private:stringname;doublescore;public:student(){}student(stringn,doubles):name(n),score(s){}student(student&s){name=s.name;score=s.score;}/*voidsetname(stringnn){name=nn;}stringgetname(){returnname;}voidsetscore(doubless){score=ss;}doublegetscore(){returnscore;}*/voidhighestscore(students[]){intk=0;doublemax;max=s[0].score;for(inti=1;iN;i++){if(maxs[i].score){max=s[i].score;k=i;}}coutThenameiss[k].nameendl;coutHighestscoreiss[k].scoreendl;}~student(){}friendostream&operator(ostream&os,students){oss.names.scoreendl;returnos;}friendistream&operator(istream&is,student&s){iss.names.score;returnis;}};voidmain(){students[N];for(inti=0;iN;i++){cins[i];}s[N].highestscore(s);}4静态数据成员设计一个书类,能够保存书名、定价,所有书的本数和总价。(将书名和定价设计为普通数据成员;将书的本数和总价设计为静态数据成员)classbook{private:doublepriece;stringname;staticintnum;staticdoubletotal;};intbook::num;doublebook::total;5动态内存分配定义point类,数据成员包括x,y,成员函数包括构造函数,拷贝构造函数和析构函数,以及setx,getx,sety,gety四个属性函数。在main函数中,用new和delete分配和释放N个point的数组。(N是const常量,N=10)#includeiostreamconstintN=10;usingnamespacestd;classpoint{private:intx,y;public:point(){}point(intx,inty):x(x),y(y){}point(point&p){x=p.x;y=p.y;}voidsetx(intxx){x=xx;}intgetx(){returnx;}voidsety(intyy){y=yy;}intgety(){returny;}~point(){}};voidmain(){point*p=newpoint[N];delete[]p;}6类的继承定义一个point类,包含私有数据成员x,y,成员函数包括无参构造函数,带参构造函数,set和get属性函数。定义circle类,从point类公有派生,增加数据成员半径r,成员函数包括无参构造函数,带参构造函数,计算面积函数getarea。在main函数中定义一个circle的对象,并计算其面积。#includeiostreamusingnamespacestd;classpoint{private:doublex,y;public:point(){}point(doublex,doubley):x(x),y(y){}voidsetx(doublexx){x=xx;}doublegetx(){returnx;}voidsety(doubleyy){y=yy;}doublegety(){returny;}~point(){}};classcircle:publicpoint{private:doubler;public:circle(){}circle(doublex,doubley,doubler):point(x,y),r(r){}doublegetarea(){return3.14*r*r;}};voidmain(){circlec1(2,3,4.5);coutTheareaofcircleisc1.getarea()endl;}7虚基类定义vehicle类,数据成员包括私有的weight,公有的构造函数,析构函数和输出函数dispaly;从vehicle类公有派生car类,增加数据成员载人数personnum,公有的构造函数,析构函数和输出display;从vehicle类公有派生truck类,增加数据成员载货量laod,公有的构造函数,析构函数和输出函数display;从car类和truck类共同公有派生出pickup类,包括公有的构造函数和输出函数。在main函数中,定义pickup类对象,并输出其基本信息。#includeiostreamusingnamespacestd;classvehicle{private:doubleweight;public:vehicle(doublew):weight(w){}voiddisplay(){couttheweightofvehicleisweightendl;}~vehicle(){}};classcar:virtualpublicvehicle{private:intpersonnum;public:car(doubleww,intp):vehicle(ww),personnum(p){}voiddisplay(){//vehicle::display();coutThenumberofpersonispersonnumendl;}~car(){}};classtruck:virtualpublicvehicle{private:doubleload;public:truck(doubleww,doublel):vehicle(ww),load(l){}voiddisplay(){//vehicle::display();coutTheloadofthetruckisloadendl;}~truck(){}};classpickup:publiccar,publictruck{public:pickup(doublew1,intp,doublew2,doublel,doublew):car(w1,p),truck(w2,l),vehicle(w){}voiddisplay(){vehicle::display();car::display();truck::display();}~pickup(){}};voidmain(){pickupp1(211,2,200,170,100);p1.display();}8运算符重载,友元函数和this指针定义一个计数器类counter,具备自增,自减功能(前后缀);输入输出,功能。在main函数里测试该类。#includeiostreamusingnamespacestd;classcounter{private:doublenum;public:counter(){}counter(doublen):num(n){}counteroperator++(){++num;return*this;}counteroperator--(){--num;return*this;}counteroperator++(int){counternn;nn.num=this-num;num++;returnnn;}counteroperator--(int){counternn;nn.num=this-num;num--;returnnn;}friendostream&operator(ostream&os,counterc);friendistream&operator(istream&is,counter&c);};ostream&operator(ostream&os,counterc){osc.numendl;returnos;}istream&operator(istream&is,counter&c){isc.num;returnis;}voidmain(){c
本文标题:课程设计(小题)
链接地址:https://www.777doc.com/doc-4640021 .html