您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > 自考C++改错及答案
1.#includeiostream.hclassTest{private:intx,y=20;public:Test(inti,intj){x=i,y=j;}intgetx(){returnx;}intgety(){returny;}};voidmain(){Testmt(10,20);coutmt.getx()endl;coutmt.gety()endl;}答案:intx,y=20;在类内部不能对数据成员直接赋值。[修改]intx,y;2.#includeiostream.hclassTest{intx,y;public:fun(inti,intj){x=i;y=j;}show(){coutx=x;if(y)cout,y=yendl;coutendl;}};voidmain(){Testa;a.fun(1);a.show();a.fun(2,4);a.show();}答案:inti,intj调用时,既有一个参数,也有两个参数,且没有重载,所以参数需要带默认值。所以inti,intj错误。[修改]inti,intj=0//注j只要有一个int类型的数据就行。3.#includeiostream.hclassA{inti;public:virtualvoidfun()=0;A(inta){i=a;}};classB:publicA{intj;public:voidfun(){coutB::fun()\n;}B(intm,intn=0):A(m),j(n){}};voidmain(){A*pa;Bb(7);pa=&b;}答案:B(intm,intn=0):A(m),j(n){}因为基类是抽象类,不能被实例化,所以在派生类中不能调用初始化基类对象。所以B(intm,intn=0):A(m),j(n){}错误,删去A(m)。[修改]B(intm,intn=0):j(n){}4.#includeiostream.hclassX{public:intx;public:X(intx){coutthis-x=xendl;}X(X&t){x=t.x;coutt.xendl;}voidfun(X);};voidfun(Xt){coutt.xendl;}voidmain(){fun(X(10));}答案:coutthis-x=xendl;要输出this-x=x表达式的值要加括号。[修改]cout(this-x=x)endl;5.#includeiostream.h#includestring.hclassBas{public:Bas(char*s=\0){strcpy(name,s);}voidshow();protected:charname[20];};Basb;voidshow(){coutname:b.nameendl;}voidmain(){Basd2(hello);show();}答案:voidshow();是普通函数不是成员函数,但是要访问类成员,需要定义为友元函数。[修改]friendvoidshow();1.#includeiostream#includefstream#includestringusingnamespacestd;classA{public:A(constchar*na){strcpy(name,na);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidshow();};voidB::show(){coutname:nameendl;}voidmain(){Bb1(B);b1.show();}答案:private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。[修改]public:或protected:2.#includeiostream.hvoidf(int*a,intn){inti=0,j=0;intk=0;for(;in/2;i++){k=a[i];a[i]=a[n-i-1];a[n-i-1]=k;}}voidshow(inta[],intn){for(inti=0;in;i++)couta[i];coutendl;}voidmain(){intp[5];inti=0,n=5;for(;i5;i++){p[i]=i;}f(*p,n);show(p,n);答案:[修改]f(p,n);[解析]f(*p,n);f函数第一个参数是指针而调用时使用*p,*p表示p所指向的变量或对象,不是地址即不是指针。3.#includeiostream.hvoidmain(){inti(3),j(8);int*constp=&i;cout*pendl;p=&j;cout*pendl;}答案:int*constp=&i;在指针变量前加const表示一个常指针即地址不能变化,它指向的变量不能改变且定义时必须设置指向变量或对象的地址。[修改]int*p=&i;4.#includeiostream.hvoidmain(){inti,*p;i=10;*p=i;cout*pendl;}答案:*p=i;指针即地址没有被赋值。[修改]p=&i;5.#includeiostream.hclassA{private:intx,y;public:voidfun(inti,intj){x=i;y=j;}voidshow(){coutxyendl;}};voidmain(){Aa1;a1.fun(2);a1.show();}答案:voidfun(inti,intj)调用时有一个参数,形参有两个,可以使第二个带默认值。[修改]voidfun(inti,intj=0)1.classABC{inta;public:ABC(intaa)a(aa){}};答案:ABC(intaa)a(aa){}初始化列表格式错误。[修改]ABC(intaa):a(aa){}2.#includeiostream.hclassAton{intX,Y;protected:intzx,zy;public:voidinit(inti,intj){zx=i;zy=j;}Aton(inti,intj,intn=0,intm=0){X=i,Y=j,zx=m,zy=n;}};voidmain(){AtonA(25,20,3,5);A.init(5,9);coutA.X()endl;答案:intX,Y;因为X,Y都是私有的,在类外无法直接访问。[修改]public:intX,Y;3.#includeiostream.hclassBas{public:~Bas(){coutBasconstructendl;}virtualvoidf()=0;};classDev:publicBas{public:~Dev(){coutBasconstructendl;}virtualvoidf(){coutDev::fendl;}};voidmain(){Bas*a=newBas();Devp;a=&p;a-f();}答案:[修改]Bas*a;[解析]Bas*a=newBas();抽象类不能被实例化,但可以声明指针或引用,所以不能用new,因为new产生临时对象。4.以下程序实现交换a,b变量的值,请用下横线标出错误所在行并给出修改意见。#includeiostream.hvoidswap(int&a,int&b){a=a+b;b=a-b;a=a-b;}voidmain(){inta=19,b=15;couta=a,b=bendl;swap(&a,&b);couta=a,b=bendl;}答案:swap(&a,&b);函数的形参是变量的引用,调用时的实参应该是地址。[修改]swap(a,b);5.#includeiostream.hvoidmain(){inti(3),j(8);int*constp=&i;cout*pendl;p=&j;cout*pendl;}答案:int*constp=&i;在指针变量前加const表示一个常指针即地址不能变化,它指向的变量不能改变且定义时必须设置指向变量或对象的地址。[修改]int*p=&i;2.#includeiostream.hclassT{protected:intp;public:T(intm){p=m;}};voidmain(){Ta(10);couta.pendl;}答案:[修改]public[解析]protected保护类型的成员,不能在类外访问。3.#includeiostreamusingnamespacestd;classDate;classTime{public:Time(inth,intm,ints){hour=h,minute=m,sec=s;}voidshow(Date&d);private:inthour,minute,sec;};classDate{public:Date(intm,intd,inty){month=m,day=d,year=y;}voidTime::show(Date&);private:intmonth,day,year;};voidTime::show(Date&d){coutd.month-d.day-d.yearendl;couthour:minute:secendl;}voidmain(){Timet1(9,23,50);Dated1(12,20,2008);t1.show(d1);答案:voidTime::show(Date&);成员函数作为友元函数,要加friend。[修改]friendvoidTime::show(Date&);4.输出最小值,有一处错误。#includeiostream.hclassTest{inta,b;intgetmin(){return(ab?a:b);}public:intc;voidsetValue(intx1,intx2,intx3){a=x1;b=x2;c=x3;}intGetMin();};intTest::GetMin(){intd=getmin();return(d=dc?d:c);}voidmain(){Testt1;t1.setValue(34,6,2);coutt1.getmin()endl;}答案:coutt1.getmin()endl;采用默认的访问权限即私有的,在外部无法访问。[修改]coutt1.GetMin()endl;5.实现数值、字符串的交换。#includeiostream#includestringusingnamespacestd;templateclassTvoidSwap(T&a,T&b){Ttemp;temp=a,a=b,b=temp;}voidmain(){inta=5,b=9;chars1[]=Hello,s2[]=hi;Swap(a,b);Swap(s1,s2);couta=a,b=bendl;couts1=s1,s2=s2endl;}答案:chars1[]=Hello,s2[]=hi;使用Swap(s1,s2)调用交换的是地址。字符指针作实参,形参值发生改变,实参也就发生变化。[修改]char*s1=Hello,*s2=hi;1.#includeiostream.hclassA{private:intx;public:A(inti){x=i;}A()
本文标题:自考C++改错及答案
链接地址:https://www.777doc.com/doc-7027404 .html