C++_编程题库

上传人:m**** 文档编号:490549420 上传时间:2022-11-20 格式:DOCX 页数:62 大小:73.76KB
返回 下载 相关 举报
C++_编程题库_第1页
第1页 / 共62页
C++_编程题库_第2页
第2页 / 共62页
C++_编程题库_第3页
第3页 / 共62页
C++_编程题库_第4页
第4页 / 共62页
C++_编程题库_第5页
第5页 / 共62页
点击查看更多>>
资源描述

《C++_编程题库》由会员分享,可在线阅读,更多相关《C++_编程题库(62页珍藏版)》请在金锄头文库上搜索。

1、v1.0可编辑可修改31.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z ,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。#include #include using namespace std;class Boxpublic:int weight;int length;int hight;void box_shape(int w, int l, int h);int box_volume(int w, int l, int h);int box_area(int w, int l, int h);int main()Box mybox;cout ;int box_v,

2、 box_a;,;box_v =,;cout This boxs volume = box_v endl;box_a =,;cout This boxs area = box_a endl;void Box二box_shape(int w, int l, int h)if(w = l & l = h)cout This is a Cube! endl;elsecout This is a cuboid! endl;int Box:box_volume(int w, int l, int h)return w * l * h;int Box二box_area(int w, int l, int

3、h)return 2 * w * l + 2 * l * h + 2 * w * h;32.有两个长方柱,其长、宽、高分别为:(1) 30, 20, 10; (2) 12, 10, 20。分别 求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。#include class Boxprivate:int length;int weight;int hight;public:Box(int, int, int);int volume();; int main()using namespace std;Box mybox1(30, 20, 10);cout The first Boxs

4、volume = () endl;Box mybox2(12, 10, 30);cout The second Boxs volume = () endl;return 0;Box:Box(int l, int w, int h)length = l;weight = w;hight = h;int Box:volume()return (hight * weight * length);33.有两个长方柱,其长、宽、高分别为:(1) 12, 20, 25; (2) 10, 30, 20。分别 求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数, 一个无参数。#includ

5、e class Boxprivate:int length;int wight;int height;public:Box();Box(int, int, int);int volume();int main()using namespace std;Box mybox1;cout The first boxs volume = () endl;Box mybox2(10, 30, 20);cout The second boxs volume = () endl;return 0;Box二Box()length = 12;wight = 20;height = 25;Box:Box(int

6、l, int w, int h)length = l;wight = w;height = h;int Box:volume()return length * wight * height;34 .声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小 数。#include using namespace std;templateclass Comparepublic:Compare(numtype a,numtype b)x=a;y=b;numtype max()return (xy)x:y;numtype min()return (xy)x:y;private: numt

7、ype x,y;int main()Comparecmp1(3,4);cout() is the Maximum of two inteder numbers.endl;cout() is the Minimum of two inteder numbers.endlendl;Compare cmp2,;cout() is the Maximum of two float numbers.endl;cout() is the Minimum of two float numbers.endlendl;Compare cmp3(a,A);cout() is the Maximum of two

8、characters.endl;cout() is the Minimum of two characters.endl;return 0;35 .建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素, 输出第1, 3, 5个学生的数据。初值自拟。#includeusing namespace std;class Studentpublic:Student(int n,int s):num(n),score(s)void display。coutnum scoreendl;private:int num;int score;int main()Studentstud5=St

9、udent(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74);Student *p=stud;for(int i=0;idisplay();return 0;36 .建立一个对象数组,内放 5个学生的数据(学号、成绩),设立一个函数max用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#includeusing namespace std; class Studentpublic:Student(int n,int s):num(n),score(s)int num;

10、int score;int main()Studentstud5=Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74);void max(Student *);Student *p=&stud0;max(p);return 0;void max(Student *arr)float max_score=arr0.score;for(int i=0;i5;i+)if(max_scorearri.score)max_score=arri.score;coutmax_score arri.numendl;

11、38.定义一个复数类Complex,重载运算符“ +”,使之能用于复数的加法运算。将运算 符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include class Complexprivate:double real;double image;public:Complex。;Complex(double, double);double get_real();double get_image();;Complex operator + (Complex &, Complex &);int main()Complex s1(3, 4);Complex s2(5, -1

12、0);Complex c3;c3 = s1 + s2;std:cout s1 + s2 =;std:cout ( () , () i) std:endl;2 3 5 8 13 21.=sumendl;1 2 3 5 8 13 return 0;1.编写一个程序,其中 main()调用一个用户定义的函数(以摄氏温度值为参数,并返 回相应的华氏温度值),该程序按照下面的格式要求用户输入摄氏温度值,并显示结果:Please enter a Celsius value: 2020 degrees Celsius is 68 degrees Fahrenheit.转换公式如下:华氏温度=x 摄氏温度+#

13、includeusing namespace std;float fun(float);int main()float c;coutc;cout20 degrees Celsius is fun(c) degrees Fahrenheit.endl;return 0;float fun(float c)float f;f = *c + ;return f;2.把有序的两个数组a和b合并,要求合并后数组依然有序。#include#define M 4#define N 5using namespace std;int main()int cM+N,i=0,j=0,k=0;32【第#页共63页】v1.0可编辑可修改int aM = 2,4,6,8;int b

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号