欢迎来到天天文库
浏览记录
ID:38746811
大小:75.00 KB
页数:6页
时间:2019-06-18
《自考C++改错及答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.#includeclassTest{private:intx,y=20;public:Test(inti,intj){x=i,y=j;}intgetx(){returnx;}intgety(){returny;}};voidmain(){Testmt(10,20);cout<classTest{in
2、tx,y;public:fun(inti,intj){x=i;y=j;}show(){cout<<"x="<3、am.h>classA{inti;public:virtualvoidfun()=0;A(inta){i=a;}};classB:publicA{intj;public:voidfun(){cout<<"B::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){}错误4、,删去A(m)。[修改]B(intm,intn=0):j(n){}4.#includeclassX{public:intx;public:X(intx){cout<x=x<x=x<x=x表达式的值要加括号。[修改]cout<<(t5、his->x=x)<#includeclassBas{public:Bas(char*s="\0"){strcpy(name,s);}voidshow();protected:charname[20];};Basb;voidshow(){cout<<"name:"<6、修改]friendvoidshow();1.#include#include#includeusingnamespacestd;classA{public:A(constchar*na){strcpy(name,na);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidshow();};voidB::show(){cout<<"name:"<7、in(){Bb1("B");b1.show();}答案:private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。[修改]public:或protected:2.#includevoidf(int*a,intn){inti=0,j=0;intk=0;for(;i8、(inti=0;i
3、am.h>classA{inti;public:virtualvoidfun()=0;A(inta){i=a;}};classB:publicA{intj;public:voidfun(){cout<<"B::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){}错误
4、,删去A(m)。[修改]B(intm,intn=0):j(n){}4.#includeclassX{public:intx;public:X(intx){cout<x=x<x=x<x=x表达式的值要加括号。[修改]cout<<(t
5、his->x=x)<#includeclassBas{public:Bas(char*s="\0"){strcpy(name,s);}voidshow();protected:charname[20];};Basb;voidshow(){cout<<"name:"<6、修改]friendvoidshow();1.#include#include#includeusingnamespacestd;classA{public:A(constchar*na){strcpy(name,na);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidshow();};voidB::show(){cout<<"name:"<7、in(){Bb1("B");b1.show();}答案:private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。[修改]public:或protected:2.#includevoidf(int*a,intn){inti=0,j=0;intk=0;for(;i8、(inti=0;i
6、修改]friendvoidshow();1.#include#include#includeusingnamespacestd;classA{public:A(constchar*na){strcpy(name,na);}private:charname[80];};classB:publicA{public:B(constchar*nm):A(nm){}voidshow();};voidB::show(){cout<<"name:"<7、in(){Bb1("B");b1.show();}答案:private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。[修改]public:或protected:2.#includevoidf(int*a,intn){inti=0,j=0;intk=0;for(;i8、(inti=0;i
7、in(){Bb1("B");b1.show();}答案:private:因为name如果是私有的,在派生类中无法访问,而基类没有提供成员函数来访问name,所以更改name访问权限为公有或保护,这样对于派生类来说是透明的。[修改]public:或protected:2.#includevoidf(int*a,intn){inti=0,j=0;intk=0;for(;i8、(inti=0;i
8、(inti=0;i
此文档下载收益归作者所有