上机测试题目.docx
- 文档编号:24461047
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:21
- 大小:755.69KB
上机测试题目.docx
《上机测试题目.docx》由会员分享,可在线阅读,更多相关《上机测试题目.docx(21页珍藏版)》请在冰豆网上搜索。
上机测试题目
1.改错:
(虚函数)
#include
#include
classb
{
strings;
};
classd:
b
{
stringt;
};
intmain()
{b*bp=newd;
deletebp;
return0;
}
3.#include
classbox
{//Thecopyconstructordefinition.
Box(constBox&b)
{height=b.height;
width=b.width;
length=b.length;
}
voidshow(box&b)
{cout<<"height:
"< "< "< private: intheight; intwidth; intlength; }; intmain() { Boxbox1(12,15,16);//实参为整数,调用普通构造函数 Boxbox2(box1);//实参是对象名,调用复制构造函数 box2.show() return0; } // #include classbox {//Thecopyconstructordefinition. public: box(){height=0;width=0;length=0;} box(intx=0,inty=0,inth=0): height(h),width(y),length(x){} box(constbox&b) {height=b.height; width=b.width; length=b.length; } voidshow() {cout<<"height: "< "< "< private: intheight; intwidth; intlength; }; intmain() { boxbox1(12,15,16);//实参为整数,调用普通构造函数 boxbox2(box1);//实参是对象名,调用复制构造函数 box2.show(); return0; } 改错4 #include classball { private: colorshade; ball(){shade=green;} whatcolor(){cout< } enumcolor{red,blue,green}; intmain(); { ballmyball myball.whatcolor(); } 改错5 #include classbirds { private: charname[40]; floatsize; public: birds(); voidsingAsong(); } voidsingAsong() {cout<<"cheepcheep"; } intmain() {birdssparrows[100]; sparrows.singAsong(); } 改错6 #include classPrinter { private: intpages=0; intmode=2; public: printer(); }; intmain() { printer4HP2; } 改错7 #include enumLight{full,partial,shade,bright}; classPhoto { private: charsubject[50]; Lightamount; public: Photo(); voidsetlight(Lightx){amount=x;} }; intmain() { Photooneofme; strcpy(oneofme.subject,"Joshuadotcommer"); oneofme.setlight(bright); } 改错8 #include classDolphin { private: floatsize; charname[45]; public: Dolphin(){strcpy(Dolphin.name,"tosser: ");} intsetsize(ints){size=s;} booloperator>(Dolphin&x,Dolphin&y); }; boolDolphinoperator>(Dolphin&x,Dolphin&y) {ifx.setsize>y.setsize returntrue; else returnfalse; } intmain() {Dolphinbig,little; big.setsize(20); little.setsize(15); if(big>little) cout<<"bigger"; else cout<<"smaller"; } 改错9 #include classA { public: intn=2: A(intval){cout< ~A(){}; }; voidmain() { Aa(0); } 改错10 #include classMyClass {public: MyClass(inti){member=i;} voidSetMember(intm){member=m;} intGetMember()const{returnmenber;} voidprint()cont{cout<<"member="< } private: intmember; }; voedmain() { /**********found**********/ MyClassobj1; obj1.print(); MyClassobj2(3); /**********found**********/ obj1.member=5; /**********found**********/ MyClass.SetMember(10); obj1.print(); obj2.print(); } 改错11 #include classBase { public: intm1; protected: intm2; private: intm3; }; classPrivateClass: privateBase { public: voidTest() { m1=1; m2=2; m3=3; } }; classDrivedFromPri: publicPrivateClass { public: voidTest() { m1=1; m2=2; m3=3; } }; classProtectedClass: protectedBase { public: voidTest() { m1=1; m2=2; m3=3; } }; classDerivedFromPro: publicProtectedClass { public: voidTest() { m1=1; m2=2; m3=3; } }; classPublicClass: publicBase { public: voidTest() { m1=1; m2=2; m3=3; } }; classDerivedFromPub: publicPublicClass { public: voidTest() { m1=1; m2=2; m3=3; } }; voidmain() { PrivateClasspriobj; priobj.m1=1; priobj.m2=2; priobj.m3=3; ProtectedClassproobj; proobj.m1=1; proobj.m2=2; proobj.m3=3; PublicClasspubobj; pubobj.m1=1; pubobj.m2=2; pubobj.m3=3; } 改错11 #include classA { public: A(char*s){cout< ~A(){} }; classB: publicA { public: B(char*sl,char*s2): A(sl) { cout< } }; classC: publicA { public: C(char*sl,char*s2): A(sl) { cout< } }; classD: publicB,publicC { public: D(char*sl,char*s2,char*s3,char*s4): B(sl,s2),C(sl,s3) { cout< }; voidmain() { Dd("classA","classB","classC","classD"); cout<<"welcometoyou"< system("pause"); } 改错12 #include classvehicle { protected: intMaxSpeed; intWeight; public: vehicle(ints,intw){MaxSpeed=s;Weight=w;} voidSetMaxSpeed(ints){MaxSpeed=s;} voidSetWeight(intw){Weight=w;} voidRun(){cout<<"classvehicleisrunning! "< voidStop(){cout<<"classvehiclehasstopped! "< }; classbicycle: virtualpublicvehicle { protected: intHeight; public: bicycle(inth,ints,intw){Height=h;} }; classmotorcar: virtualpublicvehicle { protected: intSeatNum; public: motorcar(intS,ints,intw){SeatNum=S;} }; classmotorcycle: publicbicycle,publicmotorcar { public: motorcycle(ints,intw,inth,intS): vehicle(s,w),bicycle(h,s,w),motorcar(S,s,w){} voidShow() { cout<<"MaxSpeed: "< cout<<"Weight: "< cout<<"Height: "< cout<<"SeatNum: "< } }; voidmain() { motorcyclemc(150,200,15,3); mc.Show(); } 改错13 程序能运行,但结果不是所要求的 #include classCParent { public: CParent() { } virtual~CParent(){} virtualvoidPrint()const { std: : cout<<"1,"; } }; classCSon: publicCParent { public: CSon() { } virtual~CSon(){} virtualvoidPrint() { std: : cout<<"2,"; } }; intmain() { CSon*pSon=newCSon(); CParent*pParent=newCSon(); pSon->Print(); pParent->Print(); deletepParent; deletepSon; pParent=NULL; pSon=NULL; return0; } 结果为A)1,2 B)1,1 C)2,2 编程题1: 编程2 编程3 编程4 编程5 编程6 编程7 编程8
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 上机 测试 题目