您好,欢迎访问三七文档
【第1页共61页】31.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。#includeiostream#includeiostreamusingnamespacestd;classBox{public:intweight;intlength;inthight;voidbox_shape(intw,intl,inth);intbox_volume(intw,intl,inth);intbox_area(intw,intl,inth);};intmain(){Boxmybox;coutPleaseEnterweightandlengthandhight:;cinmybox.weightmybox.lengthmybox.hight;intbox_v,box_a;mybox.box_shape(mybox.weight,mybox.length,mybox.hight);box_v=mybox.box_volume(mybox.weight,mybox.length,mybox.hight);coutThisbox'svolume=box_vendl;box_a=mybox.box_area(mybox.weight,mybox.length,mybox.hight);coutThisbox'sarea=box_aendl;}voidBox::box_shape(intw,intl,inth){if(w==l&&l==h)coutThisisaCube!endl;elsecoutThisisacuboid!endl;}intBox::box_volume(intw,intl,inth){returnw*l*h;}intBox::box_area(intw,intl,inth){return2*w*l+2*l*h+2*w*h;}【第2页共61页】32.有两个长方柱,其长、宽、高分别为:(1)30,20,10;(2)12,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。#includeiostreamclassBox{private:intlength;intweight;inthight;public:Box(int,int,int);intvolume();};intmain(){usingnamespacestd;Boxmybox1(30,20,10);coutThefirstBox'svolume=mybox1.volume()endl;Boxmybox2(12,10,30);coutThesecondBox'svolume=mybox2.volume()endl;return0;}Box::Box(intl,intw,inth){length=l;weight=w;hight=h;}intBox::volume(){return(hight*weight*length);}【第3页共61页】33.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。#includeiostreamclassBox{private:intlength;intwight;intheight;public:Box();Box(int,int,int);intvolume();};intmain(){usingnamespacestd;Boxmybox1;coutThefirstbox'svolume=mybox1.volume()endl;Boxmybox2(10,30,20);coutThesecondbox'svolume=mybox2.volume()endl;return0;}Box::Box(){length=12;wight=20;height=25;}Box::Box(intl,intw,inth){length=l;wight=w;height=h;}intBox::volume(){returnlength*wight*height;}34.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。【第4页共61页】#includeiostreamusingnamespacestd;templateclassnumtypeclassCompare{public:Compare(numtypea,numtypeb){x=a;y=b;}numtypemax(){return(xy)?x:y;}numtypemin(){return(xy)?x:y;}private:numtypex,y;};intmain(){Compareintcmp1(3,4);coutcmp1.max()istheMaximumoftwointedernumbers.endl;coutcmp1.min()istheMinimumoftwointedernumbers.endlendl;Comparefloatcmp2(45.78,93.6);coutcmp2.max()istheMaximumoftwofloatnumbers.endl;coutcmp2.min()istheMinimumoftwofloatnumbers.endlendl;Comparecharcmp3('a','A');coutcmp3.max()istheMaximumoftwocharacters.endl;coutcmp3.min()istheMinimumoftwocharacters.endl;return0;}35.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#includeiostreamusingnamespacestd;classStudent{public:Student(intn,ints):num(n),score(s){}voiddisplay(){coutnumscoreendl;}private:intnum;intscore;};intmain(){Studentstud[5]={Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74)};Student*p=stud;for(inti=0;i=2;p=p+1,i++)【第5页共61页】p-display();return0;}36.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#includeiostreamusingnamespacestd;classStudent{public:Student(intn,ints):num(n),score(s){}intnum;intscore;};intmain(){Studentstud[5]={Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74)};voidmax(Student*);Student*p=&stud[0];max(p);return0;}voidmax(Student*arr){floatmax_score=arr[0].score;for(inti=0;i5;i++)if(max_scorearr[i].score){max_score=arr[i].score;}coutmax_scorearr[i].numendl;}38.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。【第6页共61页】#includeiostreamclassComplex{private:doublereal;doubleimage;public:Complex();Complex(double,double);doubleget_real();doubleget_image();};Complexoperator+(Complex&,Complex&);intmain(){Complexs1(3,4);Complexs2(5,-10);Complexc3;c3=s1+s2;std::couts1+s2=;std::cout(c3.get_real(),c3.get_image()i)std::endl;//Complexs1(3,4);//std::couts1.get_real()std::endl;return0;}Complex::Complex(doubler,doublei){real=r;image=i;}Complex::Complex(){real=0;image=0;}doubleComplex::get_real()【第7页共61页】{returnreal;}doubleComplex::get_image(){returnimage;}Complexoperator+(Complex&s1,Complex&s2){Complexc(s1.get_real()+s2.get_real(),s1.get_image()+s2.get_image());returnc;}39.定义一个复数类Complex,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。#includeiostreamclassComplex{private:doublereal;doubleimage;public:Complex();Complex(double,double);Complexoperator+(Complex&);Complexoperator-(Complex&);voiddisplay();};intmain(){Complexs1(3,4),s2(5,6),c;c=s1+s2;c.display();c=s1-s2;c.display();return0;}【第8页共61页】Complex::Complex(){real=0;image=0;}Complex::Complex(doubler,doublei){real=r;image=i;}ComplexComplex::operator+(Complex&s1){Complexc;c.real=s1.real+real;c.image=s1.image+image;returnc;}ComplexComplex::operator-(Complex&s1){Complexc;c.real=real-s1.real;c.image=image-s1.image;returnc;}voidComplex::display(){usingnamespacestd;coutrealimageendl;}40.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自
本文标题:C++_编程题库
链接地址:https://www.777doc.com/doc-7028867 .html