《C++编程题库.pdf》由会员分享,可在线阅读,更多相关《C++编程题库.pdf(61页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、31.定义盒子Bo x类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。i n c l ud e Si n c l ud e us i n g n am e s p ac e s t d;c l as s Bo xp ub l i c:i n t w e i g h t;i n t l e n g t h;i n t h i g h t;v o i d b o x_ s h ap e(i n t w,i n t 1,i n t h);i n t b o x_ v o l um e(i n t w,i n t 1,i n t h);i n t
2、b o x_ ar e a(i n t w,i n t 1,i n t h);;i n t m ai n ()(Bo x m yb o x;c o ut ”Pl e as e En t e r w e i g h t an d l e n g t h an d h i g h t:;c i n m yb o x.w e i g h t m yb o x.l e n g t h m yb o x.h i g h t;i n t b o x_ v,b o x_ a;m yb o x.b o x_ s h ap e(m yb o x.w e i g h t,m yb o x.l e n g t h,
3、m yb o x.h i g h t);b o x v =m yb o x.b o x v o l um e(m yb o x.w e i g h t,m yb o x.l e n g t h,m yb o x.h i g h t);c o ut ”Th i s b o x s v o l um e =b o x_ v e n d l;b o x_ a=m yb o x.b o x_ ar e a(m yb o x.w e i g h t,m yb o x.l e n g t h,m yb o x.h i g h t);c o ut ”Th i s b o x,s ar e a=b o x_
4、a e n d l;)v o i d Bo x:b o x_ s h ap e(i n t w,i n t 1,i n t h)(i f(w =1&1=h)c o ut Th i s i s a Cub e!z/e n d l;e l s ec o ut Th i s i s a c ub o i d!”e n d l;)i n t Bo x:b o x_ v o l um e(i n t w,i n t 1,i n t h)(r e t ur n w *1*h;i n t Bo x:b o x_ ar e a(i n t w,i n t 1,i n t h)r e t ur n 2*w*l+
5、2*l*h+2*w*h;3 2.有两个长方柱,其长、宽、高分别为:(1)30,20,10;(2)12,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。f t i n c l ud e c l as s Bo xp r i v at e:i n t l e n g t h;i n t w e i g h t;i n t h i g h t;p ub l i c:Bo x(i n t,i n t,i n t);i n t v o l um e (););i n t m ai n ()(us i n g n am e s p ac e s t d;Bo x m yb o
6、 xl (30,20,10);c o ut Th e f i r s t Bo x,s v o l um e =m yb o xl.v o l um e()e n d l;Bo x m yb o x2(12,10,30);c o ut ”Th e s e c o n d Bo x,s v o l um e =m yb o x2.v o l um e()e n d l;r e t ur n 0;)Bo x:Bo x(i n t 1,i n t w,i n t h)(l e n g t h =1;w e i g h t =w;h i g h t =h;)i n t Bo x:v o l um e(
7、)r e t ur n (h i g h t *w e i g h t *l e n g t h);33.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。#i n c l ud e c l as s Bo xp r i v at e:i n t l e n g t h;i n t w i g h t;i n t h e i g h t;p ub l i c:Bo x();Bo x(i n t,i n t,i n t);i n t v o l um e (););i n t
8、m ai n()(us i n g n am e s p ac e s t d;Bo x m yb o x1;c o ut Th e f i r s t b o x,s v o l um e =m yb o xl.v o l um e()e n d l;Bo x m yb o x2(10,30,20);c o ut ”Th e s e c o n d b o x,s v o l um e =m yb o x2.v o l um e()e n d l;r e t ur n 0;)Bo x:Bo x()(l e n g t h =12;w i g h t =20;h e i g h t =25;)
9、Bo x:Bo x(i n t 1,i n t w,i n t h)(l e n g t h =1;w i g h t =w;h e i g h t =h;)i n t Bo x:v o l um e()(r e t ur n l e n g t h *w i g h t *h e i g h t;34.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。t t i n c l ud e us i n g n am e s p ac e s t d;t e m p l at e c l as s Co m p ar e p ub l i c:Co m p ar e(n
10、um t yp e a,n um t yp e b)x=a;y=b;n um t yp e m ax()r e t ur n (xy)?x:y;n um t yp e m i n()r e t ur n (xy)?x:y;p r i v at e:n um t yp e x,y;);i n t m ai n()Co m p ar e c m p 1(3,4);c o ut c m p l.m ax()z,i s t h e Maxi m um o f t w o i n t e d e r n um b e r s.z ze n d l;c o ut c m p l.m i n ()z,i s
11、 t h e Mi n i m um o fCo m p ar e c m p 2(45.78,93.6);c o ut c m p 2.m ax()z,i s t h e Maxi m um o fc o ut c m p 2.m i n(),z i s t h e Mi n i m um o fCo m p ar e c m p 3(a,A);c o ut c m p 3.m ax(),z i s t h e Maxi m um o fc o ut c m p 3.m i n ()z,i s t h e Mi n i m um o ft w o i n t e d e r n um b e
12、 r s.,e n d l e n d l;t w o f l o at n um b e r s.,e n d l;t w o f l o at n um b e r s.,e n d l e n d l;t w o c h ar ac t e r s.z ze n d l;t w o c h ar ac t e r s.z,e n d l;r e t ur n 0;3 5.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#i n c l ud e us i n g n am e s p ac e s t d;c l as s
13、 St ud e n t(p ub l i c:St ud e n t (i n t n,i n t s):n um(n),s c o r e (s)v o i d d i s p l ay()c o ut n um,/,zs c o r e e n d l;p r i v at e:i n t n um;i n t s c o r e;);i n t m ai n ()(St ud e n ts t ud 5 =St ud e n t(01,70),St ud e n t (02,71),St ud e n t(03,72),St ud e n t (04,73),St ud e n t(0
14、5,74);St ud e n t *p=s t ud;f o r (i n t i=0;i d i s p l ay();r e t ur n 0;3 6.建立一个对象数组,内放5个学生的数据(学号、成 绩),设立一个函数m a x,用指向对象的指针作函数参数,在m a x函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#i n c l ud e us i n g n am e s p ac e s t d;c l as s St ud e n tp ub l i c:St ud e n t (i n t n,i n t s):n um(n),s c o r e (s)i n t n
15、 um;i n t s c o r e;);i n t m ai n ()(St ud e n ts t ud 5 =St ud e n t (01,70),St ud e n t (02,71),St ud e n t (03,72),St ud e n t (04,73),St ud e n t (05,74);v o i d m ax(St ud e n t *);St ud e n t *p=&s t ud 0;m ax(p);r e t ur n 0;)v o i d m ax(St ud e n t *ar r)(f l o at m ax_ s c o r e=ar r 0.s
16、c o r e;f o r(i n t i=0;i 5;i+)i f(m ax_ s c o r e ar r i .s c o r e)m ax_ s c o r e=ar r i .s c o r e;c o ut m ax_ s c o r e z z /zar r i .n um e n d l;)38.定义一个复数类Co m p l e x,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include class Complexprivate:double real;double image;public
17、:Complex();Complex(double,double);double get_real();double get_image(););Complex operator+(Complex&,Complex&);int main()(Complex si(3,4);Complex s2(5,TO);Complex c3;c3=si+s2;std:cout sl+s2=std:cout (c3.get_real()X ,X c3.get_image()i)std:endl;/Complex si(3,4);/std:cout si.get_real()std:endl;return 0;
18、)Complex:Complex(double r,double i)(real=r;image=i;)Complex:Complex()(real=0;image=0;double Complex:get_real()r e t ur n r e al;d o ub l e Co m p l e x:g e t _ i m ag e()r e t ur n i m ag e;)Co m p l e x o p e r at o r +(Co m p l e x&s i,Co m p l e x&s 2)(Co m p l e x c (s i.g e t _ r e al ()+s 2.g
19、e t _ r e al (),s i.g e t _ i m ag e ()+s 2.g e t _ i m ag e ();r e t ur n c;)3 9.定义一个复数类Co m p l e x,重载运算符“+”,“一”,使之能用于复数的加,减运算,运算符重载函数作为Co m p l e x类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。#i n c l ud e c l as s Co m p l e xp r i v at e:d o ub l e r e al;d o ub l e i m ag e;p ub l i c:Co m p l e x();Co m p l
20、e x(d o ub l e,d o ub l e);Co m p l e x o p e r at o r +(Co m p l e x&);Co m p l e x o p e r at o r -(Co m p l e x&);v o i d d i s p l ay(););i n t m ai n()(Co m p l e x s i (3,4),s 2(5,6),c;c =s i +s 2;c.d i s p l ay();c =s i -s 2;c.d i s p l ay();r e t ur n 0;Co m p l e x:Co m p l e x()(r e al =0;
21、i m ag e =0;)Co m p l e x:Co m p l e x(d o ub l e r,d o ub l e i)r e al =r;i m ag e =i;)Co m p l e x Co m p l e x:o p e r at o r +(Co m p l e x&s i)(Co m p l e x c;c.r e al =s i.r e al +r e al;c.i m ag e =s i.i m ag e +i m ag e;r e t ur n c;)Co m p l e x Co m p l e x:o p e r at o r -(Co m p l e x&s
22、i)(Co m p l e x c;c.r e al =r e al -s i.r e al;c.i m ag e =i m ag e -s i.i m ag e;r e t ur n c;)v o i d Co m p l e x:d i s p l ay()(us i n g n am e s p ac e s t d;c o ut r e al i m ag e e n d l;)40.定义一个复数类Co m p l e x,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c l+c 2,i+c l,c l+i 均合
23、法(设i 为整数,c l,c 2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自拟。Si n c l ud e c l as s Co m p l e x(p r i v at e:d o ub l e r e al;d o ub l e i m ag;p ub l i c:Co m p l e x()r e al=0;i m ag=0;Co m p l e x(d o ub l e r,d o ub l e i)r e al=r;i m ag=i;Co m p l e x o p e r at o r+(Co m p l e x&c 2);Co m p l e x o p e r
24、 at o r-*-(i n t&i);f r i e n d Co m p l e x o p e r at o r+(i n t&,Co m p l e x&);v o i d d i s p l ay(););Co m p l e x Co m p l e x:o p e r at o r+(Co m p l e x&c)(r e t ur n Co m p l e x(r e al+c.r e al,i m ag+c.i m ag);)Co m p l e x Co m p l e x:o p e r at o r+(i n t&i)(r e t ur n Co m p l e x(r
25、 e al+i,i m ag);)v o i d Co m p l e x:d i s p l ay()us i n g n am e s p ac e s t d;c o ut,z(,zr e al z,,/zi m ag,zi)z,e n d l;)Co m p l e x o p e r at o r+(i n t&i,Co m p l e x&c)(r e t ur n Co m p l e x(i+c.r e al,c.i m ag);)i n t m ai n()(us i n g n am e s p ac e s t d;Co m p l e x c l (3,4),c 2(5
26、,-10),c 3;i n t i=5;c 3=c l+c 2;c o ut,c l+c 2=,z;c 3.d i s p l ay();c 3=i+c l;c o ut,i+c l=,/;c 3.d i s p l ay();c 3=c l+i;c o ut c l+i=;c 3.d i s p l ay();r e t ur n 0;41.有 两 个 矩 阵a和b,均 为2行3歹U。求 两 个 矩 阵 之 和。重 载 运 算 符“+”,使之能用于 矩 阵 相 加。如。=2+上 初 值 自 拟。#i n c l ud e us i n g n am e s p ac e s t d;c l
27、as s Mat r i x p ub l i c:Mat r i x();f r i e n d Mat r i x o p e r at o r+(Mat r i x&,Mat r i x&);v o i d i n p ut ();v o i d d i s p l ay();p r i v at e:i n t m at 2 3;);定 义Mat r i x类默认构造函数 重 载 运 算 符“+”输入数据函数输出数据函数Mat r i x:Mat r i x()f o r(i n t i=0;i 2;i+)f o r(i n t j=0;j 3;j+)m at i j =0;)Mat
28、r i x o p e r at o r+(Mat r i x&a,Mat r i x&b)Mat r i x c;f o r(i n t i=0;i 2;i+)f o r(i n t j=0;j 3;j+)c.m at i j =a.Ej +b.m at i j ;r e t ur n c;)v o i d Mat r i x:i n p ut()c o ut z i n p ut v al ue o f m at r i x:,ze n d l;f o r(i n t i=0;i 2;i+)f o r(i n t j=0;j 3;j+)c i n m at i j ;)v o i d Ma
29、t r i x:d i s p l ay()f o r (i n t i=0;i 2;i+)f o r(i n t j=0;j 3;j+)c o ut m at i j /,;c o ut e r)d l;定义构造函数 定 义 重 载 运 算 符“+”函数定义输入数据函数定义输出数据函数i n t m ai n()Mat r i x a,b,c;a.i n p ut ();b.i n p ut ();c o ut e n d l/,Mat r i x a:,e n d l;a.d i s p l ay();c o ut e n d l,Mat r i x b:e n d l;b.d i s p
30、 l ay();c=a+b;用重载运算符“+”实现两个矩阵相加c o ut e n d l,Mat r i x c =Mat r i x a+Mat r i x b :e n d l;c.d i s p l ay();r e t ur n 0;)42.将运算符“十 ”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Co m p l e x类的友元函数。初值自拟。Si n c l ud e c l as s Co m p ar ep r i v at e:i n t r e al;i n t i m ag;p ub l i c:Co m p ar e ();Co m p ar e(
31、i n t,i n t);f r i e n d Co m p ar e o p e r at o r+(Co m p ar e&c l,Co m p ar e&c 2);v o i d d i s p l ay(););i n t m ai n()(Co m p ar e c l (3,4),c 2(2,5),c 3;c 3=c l +c 2;c 3.d i s p l ay();r e t ur n 0;)Co m p ar e:Co m p ar e()r e al =0;i m ag =0;Co m p ar e:Co m p ar e(i n t r,i n t i)(r e al
32、=r;i m ag =i;)v o i d Co m p ar e:d i s p l ay()(us i n g n am e s p ac e s t d;c o ut r e al i m ag i e n d l;)Co m p ar e o p e r at o r+(Co m p ar e&c l,Co m p ar e&c 2)(Co m p ar e c (c l.r e al +c 2.r e al,c l.i m ag +c 2.i m ag);r e t ur n c;)43.定义一个字符串类St r i n g,用来存放不定长的字符串,重载运算符“=,用于两个字符串的等
33、于比较运算。初值自拟。44.定义一个字符串类St r i n g,用来存放不定长的字符串,重 载 运 算 符 用 于 两 个字符串的小于的比较运算。初值自拟。45.定义一个字符串类St r i n g,用来存放不定长的字符串,重 载 运 算 符 用 于 两 个字符串的大于的比较运算。初值自拟。Si n c l ud e#i n c l ud e us i n g n am e s p ac e s t d;c l as s St r i n g(p ub l i c:St r i n g ()p=NULL;St r i n g(c h ar *s t r);f r i e n d b o o
34、l o p e r at o r (St r i n g&s t r i n g l,St r i n g&s t r i n g 2);f r i e n d b o o l o p e r at o r(St r i n g&s t r i n g l,St r i n g&s t r i n g 2)i f (s t r c m p(s t r i n g l.p,s t r i n g 2.p)0)r e t ur n t r ue;e l s er e t ur n f al s e;)b o o l o p e r at o r(St r i n g&s t r i n g l,S
35、t r i n g&s t r i n g 2)(i f (s t r c m p(s t r i n g l.p,s t r i n g 2.p)(s t r i n g l,s t r i n g 2)=l)(s t r i n g l.d i s p l ay();c o ut/,;s t r i n g 2.d i s p l ay();e l s e i f(o p e r at o r(s t r i n g l,s t r i n g 2)=l)s t r i n g l.d i s p l ay();c o ut,/,;s t r i n g 2.d i s p l ay();
36、)e l s e i f(o p e r at o r=(s t r i n g l,s t r i n g 2)=l)(s t r i n g l.d i s p l ay();c o u t z z=z,;s t r i n g 2.d i s p l ay();c o ut e r)d l;i n t m ai n()(St r i n g s t r i n g l (He l l o ),s t r i n g 2(“Bo o k ),s t r i n g 3(Co m p ut e r ),s t r i n g 4(,He l l o,z);c o m p ar e(s t r
37、 i n g l,s t r i n g 2);c o m p ar e (s t r i n g 2,s t r i n g 3);c o m p ar e(s t r i n g l,s t r i n g 4);r e t ur n 0;4 6.定义一个描述学生基本情况的类,数据成员包括姓名、学号、C+成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。数据自拟。#i n c l ud e c l as s St ud e n t(p r i v at e:c h ar *n am e;i n t i d;i n t c p p _ s o ur c e;i n t e n
38、 g _ s o ur c e;i n t m at h _ s o ur c e;p ub l i c:St ud e n t (c h ar *,i n t,i n t,i n t,i n t);v o i d s um _ s o ur c e ();v o i d av g _ s o ur c e(););i n t m ai n()(St ud e n t s(Jo h n,1,90,80,97);s.s um _ s o ur c e ();s.av g _ s o ur c e ();r e t ur n 0;)St ud e n t:St ud e n t(c h ar *n
39、,i n t i,i n t c s,i n t e s,i n t m s)(n am e =n;i d =i;c p p _ s o ur c e =c s;e n g _ s o ur c e =e s;m at h _ s o ur c e =m s;)v o i d St ud e n t:s um _ s o ur c e()(s t d:c o ut n am e Th e s um o f s o ur c e:c p p _ s o ur c e+e n g _ s o ur c e+m at h _ s o ur c e s t d:e n d l;)v o i d St
40、ud e n t:av g _ s o ur c e()(d o ub l e av g;av g =(c p p _ s o ur c e +e n g _ s o ur c e +m at h _ s o ur c e)/3;s t d:c o ut Th e av g o f s o ur c e:av g ”,使之能够用于输出以上类对象。#i n cl u d e cl ass Poi n tpr ot e ct e d:i n t x,y;pu bl i c:Poi n t()x =0,y =0;Poi n t (i n t a,i n t b)(t hi s-x =a;t hi s
41、-y =b;v oi d se t X(i n t a)x =a;v oi d se t Y(i n t b)y =b;i n t g e t X()r e t u r n x;i n t g e t Y()r e t u r n y;);cl ass Ci r cl e:pu bl i c Poi n t(pr ot e ct e d:i n t r;pu bl i c:Ci r cl e(i n t x,i n t y,i n t r):Poi n t (x,y)t hi s-r =r;v oi d se t R(i n t a)r =a;i n t g e t RO r e t u r
42、n r;);cl ass Cy l i n d e r:pu bl i c Ci r cl e(pr ot e ct e d:i n t h;pu bl i c:Cy l i n d e r O:Ci r cl e(0,0,0),h(0)Cy l i n d e r(i n t x,i n t y,i n t r,i n t h):Ci r cl e(x,y,r)t hi s-h=h;v oi d se t H(i n t a)h=a;i n t g e t H()r e t u r n h;f r i e n d st d:i st r e am&ope r at or(st d:i st
43、r e am&,Cy l i n d e r&);f r i e n d st d:ost r e am&ope r at or (st d:i st r e am&i n pu t,Cy l i n d e r&cc)(i n t _ x,_ y,_ r,_ h;st d:cou t En t e r t he Cy l i n d e r:_ x _ y _ r _ h;cc.se t X(_ x);cc.se t Y(_ y);cc.se t R(_ r);cc.se t H(_ h);r e t u r n i n pu t;)st d:ost r e am&ope r at or (
44、st d:ost r e am&os,Cy l i n d e r&cc)(os cc.g e t X()cc.g e t Y()cc.g e t R()cc;cou t cc;r e t u r n 0;4 8.写一个程序,定义抽象类型Shape,由他派生三个类:Ci r cl e (圆形),Re ct an g l e(矩形),Tr ape zoi d (梯形),用一个函数pr i n t Ar e a分别输出三者的面积,3 个图形的数据在定义对象是给定。t t i n cl u d e cl ass Shape(pu bl i c:v i r t u al d ou bl e ar e
45、a()con st =0;);cl ass Ci r cl e:pu bl i c Shape(pr i v at e:d ou bl e r;pu bl i c:Ci r cl e (d ou bl e a):r (a)v i r t u al d ou bl e ar e a()con st(r e t u r n 3.1 4 *r *r;);cl ass Re ct an g l e:pu bl i c Shape(pr i v at e:d ou bl e h,w;pu bl i c:Re ct an g l e (d ou bl e a,d ou bl e b):h(a),w(b)v
46、 i r t u al d ou bl e ar e a()con st(r e t u r n h*w;);cl ass Tr ape zoi d:pu bl i c Shape(pr i v at e:d ou bl e h,w;pu bl i c:Tr ape zoi d (d ou bl e a,d ou bl e b):h(a),w(b)v i r t u al d ou bl e ar e a()con str e t u r n 0.5 *h*w;);v oi d pr i n t Ar e a(con st Shape&c)(st d:cou t c.ar e a()st d:
47、e n d l;)i n t m ai n()(Ci r cl e c(2);pr i n t Ar e a(c);Re ct an g l e r(2,4);pr i n t Ar e a(r);Tr ape zoi d t(3,5);pr i n t Ar e a(t);r e t u r n 0;)4 9.定义一个人员类Cpe r son,包括数据成员:姓名、编号、性别和用于输入输出的成员函数。在此基础上派生出学生类CSt u d e n t (增加成绩)和老师类Ct e ache r (增加教龄),并实现对学生和教师信息的输入输出。#i n cl u d e cl ass Cpe r
48、son(pr ot e ct e d:char n am e 1 0;i n t i d;i n t se x;pu bl i c:v i r t u al v oi d g e t l n f or()=0;v i r t u al v oi d pr i n t l n f or()=0;);cl ass CSt u d e n t:pu bl i c Cpe r sonpr ot e ct e d:i n t cou r se;pu bl i c:v i r t u al v oi d g e t l n f or()(u si n g n am e space st d;ci n co
49、u r se;cou t En t e rt hest u d e n t si n f or am t i onzz e n d lcou t En t e rt hen am e:;ci n n am e ;cou t En t e rt hei d:;ci n i d;cou t se x;cou t En t e rt hecou r se:;)v i r t u al v oi d pr i n t l n f or()(u si n g n am e space st d;cou t i d e n d l;cou t n am e e n d l;cou t se x e n d
50、 l;cou t cou r se e n d l;);cl ass Ct e ache r:pu bl i c Cpe r sonpr ot e ct e d:i n t ag e;pu bl i c:v i r t u al v oi d g e t l n f or()u si n g n am e spacecou t En t e rcou t En t e rci n n am e ;cou t En t e rci n i d;cou t En t e rci n se x;cou t En t e rst d;t he t e ache r st he n am e:;t he