您好,欢迎访问三七文档
1练习题:【请大家假期中练习阅读和编写如下练习题】1.#include“iostream.h”int&get(int&a,intb){a+=b;b++;returna;}int&gb(int&i){++i;returni;}intgc(inti){return++i;}voidmain(){inti=10,j=1;j=get(i,j)+10;cout“i=”i“j=”jendl;get(i,j)=100;cout“i=”i“j=”jendl;inta=1,b=2;a=a+gc(a);b=b+gb(b);couta”,”bendl;}2.#includeiostream.hclassinner{private:inta;public:inner(intx=10){a=x;cout“a=”a'\n';cout“constructurecalled\n”;}inner(inner&m){a=m.a;cout“copy-constructurecalled\n”;}~inner(){coutdestructurecalled\n;}voidshow(){coutaendl;}};voidfun(innert)2{t.show();}voidmain(){innert1;innert2=t1;fun(t1);}3.#include“iostream.h”classOBJ{intk;public:voidset_k(intx){k=x;}voidout_k(){coutkendl;}};voidf(OBJ*px){px-out_k();px-set_k(20);px-out_k();}voidmain(){OBJobj;obj.set_k(100);f(&obj);obj.out_k();}4.#include“iostream.h”classB{inta,b;public:B(intx=5,inty=6){cout“constructure_called”endl;a=x;b=y;}voiddisplay(){cout“a=”a“b=”bendl;}~B(){cout“destructure_called”endl;}};3voidmain(){Ba[2];a[0]=A(5,15);a[1]=A(22,30);A*p=&a[0];p-display();p++;p-display();}5.#include“iostream.h”classY{intx;public:Y(intp=2):x(p){}virtualvoidfb(){coutx“\n”;cout“Y::fb()called\n”;}};classZ:publicX{inty;public:Z(intm=10):y(m){}virtualvoidfb(){couty“\n”;cout“Z::fb()called\n”;}};voidfun(Y&a){a.fb();}voidmain(){Ya;Zb(5);fun(b);fun(a);}6.#include“iostream.h”classgoods{inteach_w;staticintall_w;public:4goods(intx){each_w=x;}goods(goods&t){each_w=t.each_w;all_w+=each_w;}voidadd_w(){all_w+=each_w;}voidshow(){couteach_wendl;coutall_wendl;}};intgoods::all_w=0;voidmain(){goodsg1(100);g1.add_w();g1.show();goodsg2=g1;g2.show();}7.#include“iostream.h”classB{inta,b;public:B(intx=5,inty=6){cout“constructure_called”endl;a=x;b=y;}voiddisplay(){cout“a=”a“b=”bendl;}~B(){cout“destructure_called”endl;}};voidmain(){B*p;p=newB[2];p-display();p++;5p-display();delete[]p;}8.#include“iostream.h”classBase{public:Base(){cout“Basecreated\n”;}~Base(){cout“Basedestructure\n”;}};classD_class:publicBase{public:D_class(){cout“D_classcreated\n”;}~D_class(){cout“D_classdestructure\n”;}};voidmain(){D_classd;}9.定义一个分数类类型,根据注释要求将类中函数补充完整。#include“iostream.h”classFenshu{inta,b;//a表示分子,b表示分母public:Fenshu(intx);//构造函数,用x初始化a,image为1Fenshu(intx,inty);//构造函数,用x,y分别初始化a,bvoidshow();//以“a/b”模式显示输出分数operatordouble();//类型转换函数,将分数转换为doublex型实数,如1/2转换的结果是0.5};10.定义一个复数类类型,根据注释的要求将类中的函数实现补充完整。#include“iostream.h”#include“math.h”classComplex{floatreal,image;//real表示实部,image表示虚部public:Complex();//缺省构造函数,私有量为0和0Complex(floatr);//构造函数,用r初始化real,image为0Complex(floatr,floatt);//构造函数,用r,t分别初始化real,imagefriendfloatmode(Complexc1,Complexc2);//用友元函数求两个复数的模返回voidshowcom();//以“a+bi”模式显示输出复数私有量6};11.定义一个三维点类类型,根据main中的调用,将点类补充完整。#include“iostream.h”classthree_node{intx,y,z;public:….};voidmain(){three_noded1(1,2,3);++d1;coutd1;d1++;coutd1;}12.定义一个时间类类型,根据main中的调用,将时间类补充完整。#include“iostream.h”classTime{inth,m,s;//分别表示小时、分、秒public:….};voidmain(){Timet(8,20,59);++t;//增加1秒,注意跨分钟和跨小时的设计coutt;cint;//键盘输入一个时间coutt;}13.定义一个数组类类型的模板结构,根据main函数中的调用,补充数组类。#include“iostream.h”templateclassTclassArray{T*pintsize;public:Array(intn);//动态申请数组空间~Array();//释放数组空间T&operator[](inti);//重载[]运算符返回p[i]Array&operator=(constArray&t);//重载赋值运算符Tfindmin();//返回最小值Tfindmax();//返回最大值7TAverage();//返回平均值};……//类体实现voidmain(){vectorintx(5);for(inti=0;i5;i++)x[i]=i+1;coutx.findmax()endl;coutx.findmin()endl;coutx.Average()endl;}14.定义一个向量类模板,根据main函数中的调用,补充数组类。#include“iostream.h”templateclassTclassVector{T*aintsize;public:Vector(intn);//动态申请向量空间~Vector();//释放向量空间T&operator[](inti);//重载[]运算符,返回a[i]变量Vector&operator=(constVector&t);//重载赋值运算符voidexpend(intoffset);//扩充offset个空间};……//类体实现voidmain(){Vectorintx(5);for(inti=0;i5;i++)x[i]=i+1;intn=10;x.expend(n);Vectorinty(5);y=x;coutendl;}
本文标题:C++复习
链接地址:https://www.777doc.com/doc-4947452 .html