1、C+编程题库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 bo
2、x_v, 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; else cout This is a cuboid! endl; int Box:box_volume(int w, int l, int h) return w * l * h;int Box:
3、box_area(int w, int l, int 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,
4、10); cout The first Boxs 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。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数
5、,其中一个有参数,一个无参数。#include 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; wi
6、ght = 20; height = 25;Box:Box(int 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;numt
7、ype min()return (xy)?x:y;private:numtype 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; Compar
8、e cmp3(a,A); cout() is the Maximum of two 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
9、: int num; int score;int main() Student stud5=Student(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 Studentpub
10、lic: Student(int n,int s):num(n),score(s) int num; int score;int main() Student stud5=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_sco
11、rearri.score) max_score=arri.score; coutmax_score arri.numendl; 38.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include class Complex private: double real; double image; public: Complex(); Complex(double, double); double get_real(); double get_image();Complex operato
12、r + (Complex &, Complex &);int main() Complex s1(3, 4); Complex s2(5, -10); Complex c3; c3 = s1 + s2; std:cout s1 + s2 = ; std:cout ( () , () i) std:endl; .= sumendl; return 0;1. 编写一个程序,其中main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值),该程序按照下面的格式要求用户输入摄氏温度值,并显示结果:Please enter a Celsius value: 2020 degrees
13、Celsius is 68 degrees Fahrenheit.转换公式如下:华氏温度 = x 摄氏温度 + #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
14、N 5using namespace std;int main() int cM+N,i=0,j=0,k=0; int aM = 2,4,6,8; int bN = 1,3,5,7,9; while(iM&jN) if(aibj) ck+ = ai+; else ck+ = bj+; while(iM) ck+ = ai+; while(jN) ck+ = bj+; for(int z=0;zM+N;z+) coutcz ; return 0;3. 用指针方法完成:对一个45矩阵(int型)分别进行横向和纵向汇总(如图)。7 2 9 4 65 6 1 8 39 8 6 3 95 1 9 7 2
15、#includeusing namespace std;int main() int s45 = 7,2,9,4,6,5,6,1,8,3,9,8,6,3,9,5,1,9,7,2; int *p; p = &s00; for(int i=0;i4*5;i+) coutpi ; if(i+1)%5=0) coutendl; return 0;14声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include using namespace std;templateclass Compare public: Compare(numtype a,numtype b) x=
16、a;y=b; numtype max() return (xy)?x:y; numtype min() return (xy)?x:y; private: numtype x,y; ;int main()Compare cmp1(3,7); 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
17、 the Minimum of two float numbers.endlendl; Compare cmp3(a,A); cout() is the Maximum of two characters.endl; cout() is the Minimum of two characters.endl; return 0;15建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。#include using namespace std;class Student public: Student(int n,float s):num(n),s
18、core(s) void display(); private: int num; float score; ;void Student:display() coutnum scoreendl; int main()Student stud5= Student(101,Student(102,Student(103, Student(104,Student(105,; Student *p=stud; for(int i=0;idisplay(); return 0; 16建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个
19、学生中成绩最高者,并输出其学号。#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,Student(102,Student(103, Student(104,Student(105,; void max(Student* ); Student *p=&stud0; max(p); void max(Student *arr)float max_sc
20、ore=arr0.score; int k=0; for(int i=1;imax_score) max_score=arri.score;k=i; coutarrk.num max_scoreendl; 17将运算符“”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为类的友元函数。#include class Complex public: Complex()real=0;imag=0; Complex(double r)real=r;imag=0; Complex(double r,double i)real=r;imag=i; friend Complex operator
21、+ (Complex &c1,Complex &c2); void display(); private: double real; double imag; ;Complex operator+ (Complex &c1,Complex &c2) return Complex+, +; void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1=; (); coutc2=; (); coutc1+c2=; (); return 0;18定义一个复数类Co
22、mplex,重载运算符“”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。#include class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator
23、+(int&,Complex &); void display(); private: double real; double imag; ; Complex Complex:operator+(Complex &c)return Complex(real+,imag+; Complex Complex:operator+(int &i)return Complex(real+i,imag);void Complex:display()cout(real,imagi)endl;Complex operator+(int &i,Complex &c)return Complex(i+,;int
24、main()Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; coutc1+c2=; (); c3=i+c1; couti+c1=; (); c3=c1+i; coutc1+i=; (); return 0;19编写一个程序读入多个string对象,把它们连接起来存放到一个更大的string对象中。并输出连接后的string对象#include #include int main() using namespace std; string str1, str2, str3; cout str1 str2; str3 = str1 + str2;
25、cout str3 endl; return 0;20编写一个程序读入两个string对象,测试它们是否相等,若不相等,则指出两个中哪个较大。#include #include int main() using namespace std; string str1, str2; cout str1 str2; if(str1 str2) cout str1 is bigger! endl; else cout str2 is bigger! endl; return 0;21. 设计一个Time类,包括三个私有数据成员:hour,minute,sec,用构造函数初始化,内设公用函数displa
26、y(Date &d),设计一个Date类,包括三个私有数据成员:month,day,year,也用构适函数初始化;分别定义两个带参数的对象t1(12,30,55),d1(3,25,2010),通过友员成员函数的应用,输出d1和t1的值。#include using namespace std;class Date;class Time public: Time(int,int,int); void display(const Date&); private: int hour; int minute; int sec; ; class Date public: Date(int,int,int); friend void Time:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1