C++复习题.docx
- 文档编号:24296915
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:28
- 大小:17.36KB
C++复习题.docx
《C++复习题.docx》由会员分享,可在线阅读,更多相关《C++复习题.docx(28页珍藏版)》请在冰豆网上搜索。
C++复习题
1、
#include
classLocation
{
public:
Location(intxx=0,intyy=0)
{
X=xx;
Y=yy;
cout<<"ConstructorObject.\n";
}
Location(constLocation&p)//复制构造函数
{
X=p.X;
Y=p.Y;
cout<<"Copy_constructorcalled."< } ~Location() { cout< } intGetX() { returnX; } intGetY() { returnY; } private: intX,Y; }; voidf(Locationp) { cout<<"Funtion: "< } voidmain() { LocationA(1,2); f(A); } 结果: constructorobject. Copy_constructorcalled. Funtion: 1,2 1,2Objectdestroyed. 1,2Objectdestroyed. 2 #include classLocation { public: Location(intxx=0,intyy=0) { X=xx; Y=yy; cout<<"Objectconstructed."< } Location(constLocation&p) { X=p.X; Y=p.Y; cout<<"Copy_constructorcalled."< } ~Location() { cout< } intGetX(){returnX;} intGetY(){returnY;} private: intX,Y; }; voidmain() { Locationa; Locationb(a); Locationc; } 结果: Objectconstructed. Copy_constructorcalled. Objectconstructed. Objectdestroyed. Objectdestroyed. Objectdestroyed. 3 #include classcounter { staticintnum; public: voidsetnum(inti) { num=i; } voidshownum() { cout< } }; intcounter: : num=0; voidmain() { countera,b; a.shownum(); b.shownum(); a.setnum(10); a.shownum(); b.shownum(); } 结果: 001010 4 #include classA { friendclassB; public: voidDisplay() { cout< }; private: intx; }; classB { public: voidSet(inti) { Aobject.x=i; } voidDisplay() { Aobject.Display(); } private: AAobject; }; voidmain() { BBobject; Bobject.Set(100); Bobject.Display(); } 结果: 100 5 #include classx { public: voidf1(); voidf2(); voidprint() { cout<<"m="< } private: intm; }; voidx: : f1() { m=5; } voidx: : f2() { m=2; } voidmain() { xobj; obj.f1(); obj.print(); obj.f2(); obj.print(); } 结果: m=5 m=2 6 #include inti=10; voidmain() { inti=5; if(i<10) { inti; i=7; cout<<"i="< } cout<<"i="< cout<<": : i="<<: : i< } 结果: i=7 i=5 : : i=10 7 #include usingnamespacestd; voidfun() { staticinta=1; inti=5; a++; i++; cout<<"i="< } intmain() { fun(); fun(); } 结果: i=6,a=2 i=6,a=3 8 #include usingnamespacestd; classPoint { public: Point(); Point(intxx,intyy); ~Point(); voidMove(intx,inty); intGetX(){returnX;} intGetY(){returnY;} private: intX,Y; }; Point: : Point() { X=Y=0; cout<<"DefaultConstructorcalled."< } Point: : Point(intxx,intyy) { X=xx; Y=yy; cout<<"Constructorcalled."< } Point: : ~Point() { cout<<"Destructorcalled."< } voidPoint: : Move(intx,inty) { X=x; Y=y; } intmain(){ Point*Ptr=newPoint[2];//创建对象数组 Ptr[0].Move(5,10);//通过指针访问数组元素的成员 (Ptr+1)->Move(15,20);//通过指针访问数组元素的成员 cout<<"Deleting..."< delete[]Ptr;//删除整个对象数组 return0; } 结果: DefaultConstructorcalled. DefaultConstructorcalled. Deleting... Destructorcalled. Destructorcalled. 9 #include usingnamespacestd; classPoint { public: Point(); ~Point(); private: intX,Y; }; Point: : Point() { X=Y=0; cout<<"DefaultConstructorcalled."< } Point: : ~Point() { cout<<"Destructorcalled."< } intmain() { Point*Ptr=newPoint[2]; cout<<"Deleting..."< delete[]Ptr; } 结果: DefaultConstructorcalled. DefaultConstructorcalled. Deleting... Destructorcalled. Destructorcalled. 10 #include classA { public: inta1,a2; A(inti1=0,inti2=0) { a1=i1; a2=i2; } voidprint() { cout<<"a1="< } }; classB: publicA { public: intb1,b2; B(intj1=1,intj2=1) { b1=j1; b2=j2; } voidprint()//定义同名函数 { cout<<"b1="< } voidprintAB() { A: : print();//派生类对象调用基类版本同名成员函数 print();//派生类对象调用自身的成员函数 } }; voidmain() { Bb; b.A: : print(); b.printAB(); } 结果: a1=0a2=0 a1=0a2=0 a1=1a2=2 11 #include classB { public: staticvoidAdd() { i++; } staticinti; voidout() { cout<<"statici="< } }; intB: : i=0; classD: privateB { public: voidf() { i=5; Add(); B: : i++; B: : Add(); } }; voidmain() { Bx; Dy; x.Add(); x.out(); y.f(); cout<<"statici="< : i< cout<<"statici="< //cout<<"statici="< } 结果: statici=1 statici=1 statici=8 12 #include classBase { public: Base() { cout<<"Basecreated.\n"; } }; classD_class: publicBase { public: D_class() { cout<<"D_classcreated.\n"; } }; voidmain() { D_classd1; } 结果: Basecreated D_classcreated 13 #include classA { public: A() { cout<<"classA"< } }; classB: publicA { public: B() { cout<<"classB"< } }; classC: publicA { public: C() { cout<<"classC"< } }; classD: publicB,publicC { public: D() { cout<<"classD"< } }; voidmain() { Ddd; } 结果: classA classB classA classC classD 14虚基类 #include classA { public: A() { cout<<"classA"< } }; classB: virtualpublicA { public: B() { cout<<"classB"< } }; classC: virtualpublicA { public: C() { cout<<"classC"< } }; classD: publicB,publicC { public: D() { cout<<"classD"< } }; voidmain() { Ddd; } 结果 classA classB classC classD 15 #include classIncrease { public: Increase() { value=0; } voiddisplay()const { cout< }; Increaseoperator++();//前置 Increaseoperator++(int);//后置 private: unsignedvalue; }; IncreaseIncrease: : operator++() { value++; return*this; } IncreaseIncrease: : operator++(int) { Increasetemp; temp.value=value++; returntemp; } classIncrease1 { public: Increase1() { value=0; } voiddisplay()const { cout< }; friendIncrease1operator++(Increase1&);//前置 friendIncrease1operator++(Increase1&,int);//后置 private: unsignedvalue; }; Increase1operator++(Increase1&a) { a.value++; returna; } Increase1operator++(Increase1&a,int) { Increase1temp(a); a.value++; returntemp; } voidmain() { Increasea; (a++).display(); a.display(); (++a).display(); a.display(); Increase1a1; (a1++).display(); a1.display(); (++a1).display(); a1.display(); } 结果 0 1 2 2 16 #include classBase { public: Base(charxx) { x=xx; } virtualvoidwho() { cout<<"Baseclass: "< } protected: charx; }; classFirst_d: publicBase { public: First_d(charxx,charyy): Base(xx) { y=yy; } voidwho() { cout<<"Firstderivedclass: "< } protected: chary; }; classSecond_d: publicFirst_d { public: Second_d(charxx,charyy,charzz): First_d(xx,yy) { z=zz; } voidwho() { cout<<"Secondderivedclass: "< } protected: charz; }; voidmain() { BaseB_obj('A'); First_dF_obj('T','O'); Second_dS_obj('E','N','D'); Base*p; p=&B_obj; p->who(); p=&F_obj; p->who(); p=&S_obj; p->who(); } 结果 Baseclass: A Firstderivedclass: T,O Secondderivedclass: E,N,D 17 #include classA { public: ~A() { cout<<"A: : ~A()iscalled.\n"; } }; classB: publicA { public: ~B() { cout<<"B: : ~B()iscalled.\n"; } }; voidmain() { A*Ap=newB; B*Bp2=newB; cout<<"deletefirstobject: \n"; deleteAp; cout<<"deletesecondobject: \n"; deleteBp2; } 结果: deletefirstobject: A: : ~A()iscalled. deletesecondobject: B: : ~B()iscalled. A: : ~A()iscalled. 18 #include classfigure { protected: doublex,y; public: voidset_dim(doublei,doublej=0) { x=i; y=j; } virtualvoidshow_area()=0; }; classtriangle: publicfigure { public: voidshow_area() { cout<<"Trianglewithhigh"< } }; classsquare: publicfigure { public: voidshow_area() { cout<<"Squarewithdimension"< } }; classcircle: publicfigure { public: voidshow_area() {cout<<"Circlewithradius"< cout<<"hasanareaof"<<3.14*x*x<<"\n"; } }; voidmain() { trianglet;//派生类对象 squares; circlec; t.set_dim(10.0,5.0); t.show_area(); s.set_dim(10.0,5.0); s.show_area(); c.set_dim(9.0); c.show_area(); } 结果: Trianglewithhigh10andbase5hasanareaof25 Squarewithdimension10*5hasanareaof50 Circlewithradius9hasanareaof254.34
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 复习题
![提示](https://static.bdocx.com/images/bang_tan.gif)