C++经典练习1.docx
- 文档编号:5750187
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:11
- 大小:17.48KB
C++经典练习1.docx
《C++经典练习1.docx》由会员分享,可在线阅读,更多相关《C++经典练习1.docx(11页珍藏版)》请在冰豆网上搜索。
C++经典练习1
●.声明Point类,有坐标_x,_y两个成员变量;对Point类重载“++”(自增)、“--”(自减)运算符,实现对坐标值的改变。
#include
usingnamespacestd;
classpoint
{
public:
point(intxx=0,intyy=0)
{x=xx;y=yy;}
voidset(intxx,intyy);
pointoperator++();pointoperator++(int);
pointoperator--();pointoperator--(int);
voiddisplay();
private:
intx,y;
};
voidpoint:
:
set(intxx,intyy){x=xx;y=yy;}
voidpoint:
:
display(){cout<<"("< pointpoint: : operator++(){returnpoint(++x,++y);} pointpoint: : operator++(int){returnpoint(x++,y++);} pointpoint: : operator--(){returnpoint(--x,--y);} pointpoint: : operator--(int){returnpoint(x--,y--);} intmain() { pointa;intm,n;cout<<"输入横纵坐标: ";cin>>m>>n;a.set(m,n); cout<<"a=";a.display();cout<<"(a++)=";(a++).display(); cout<<"(++a)=";(++a).display();cout<<"(a--)=";(a--).display(); cout<<"(--a)=";(--a).display();return0;} ●分别使用成员函数和友元函数完成运算符加减法的重载,并进行测试。 #include usingnamespacestd; classcomplex { public: complex(doubler=0.0,doublei=0.0): real(r),imag(i){} voidset(doubler,doublei);voiddisplay(); friendcomplexoperator+(complex&c1,complex&c2); friendcomplexoperator-(complex&c1,complex&c2); private: doublereal;doubleimag; }; complexoperator+(complex&c1,complex&c2) {returncomplex(c1.real+c2.real,c1.imag+c2.imag);} complexoperator-(complex&c1,complex&c2) {returncomplex(c1.real-c2.real,c1.imag-c2.imag);} voidcomplex: : set(doubler,doublei) {real=r;imag=i;} voidcomplex: : display(){cout<<"("< intmain() {doublea,b,c,d;complexc1,c2,c3;cout<<"输入c1实部虚部: "; cin>>a>>b;cout<<"输入c2实部虚部: ";cin>>c>>d;c1.set(a,b); c2.set(c,d);cout<<"c1=";c1.display();cout<<"c2="; c2.display();c3=c1+c2;cout<<"c3=c1+c2=";c3.display(); c3=c1-c2;cout<<"c3=c1-c2";c3.display();return0;} ●.编一带派生类的程序,用虚函数多态性计算三角形、正方形和圆形的面积,并编写主程序测试。 #include usingnamespacestd; classgraph { public: graph(doublei=0.0,doublej=0.0){x=i;y=j;} virtualdoublearea(); private: doublex,y; }; doublegraph: : area(){return0.0;} classtria: publicgraph { public: tria(doublei=0.0,doublej=0.0,doublem=0.0,doublen=0.0): graph(i,j){x=m;y=n;} voidset(doublem,doublen) {x=m;y=n;} doublearea(); private: doublex,y; }; doubletria: : area(){return(x*y)/2.0;} classsqu: publicgraph { public: squ(doublei=0.0,doublej=0.0,doublem=0.0): graph(i,j) { x=m; } voidset(doublem) { x=m; } doublearea(); private: doublex; }; doublesqu: : area() { returnx*x; } classcir: publicgraph { public: cir(doublei=0.0,doublej=0.0,doublem=0.0): graph(i,j) { x=m; } voidset(doublem) { x=m; } doublearea(); private: doublex; }; doublecir: : area() { return(x*x)*3.14; } intmain() { graph*p1; triap2; squp3; cirp4; doublea,b,c,d; cout<<"输入三角形底高: "; cin>>a>>b; p2.set(a,b); p1=&p2; cout<<"三角形面积为: "< cout<<"输入正方形边长: "; cin>>c; p3.set(c); p1=&p3; cout<<"正方形面积为: "< cout<<"输入圆形半径: "; cin>>d; p4.set(d); p1=&p4; cout<<"圆形面积为: "< return0; } –.有一个person的类,有公有的数据成员char*Name;具有没有参数的构造函数和有参数的构造函数,参数为char*name,并有一个公有的打印姓名的函数PrintName;有一个类Student以公有继承的方式继承了person类,该类中有一个新增函数PrintInfo();请写出这两种类,在主函数中说明一个Student的对象,并验证新增的函数。 #include usingnamespacestd; classperson { public: char*Name; person(); person(char*name); PrintName(); }; person: : person() { cout<<"(默认构造函数被调用)"; chara='p'; Name=&a; } person: : person(char*name) { cout<<"(带参构造函数被调用)"; Name=name; } person: : PrintName() { cout<<*Name< } classStudent: publicperson { public: Student() {} Student(char*p): person(p) {} PrintInfo(); }; Student: : PrintInfo() { charn; cout<<"pleasecinonedata(char): "; cin>>n; *Name=n; } intmain() { charn='w'; Studentstudent1; student1.PrintName(); Studentstudent2(&n); student2.PrintName(); student1.PrintInfo(); student1.PrintName(); return0; } –声明一个车(vehicle)基类,具有MaxSpeed、Weightwheel等成员变量,Set、Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。 自行车(bicyc座位le)类有高度(Height)等属性,汽车(motorcar)类有数(SeatNum)等属性,通过构造函数对成员变量初始化。 在主函数中说明一个自行车类和汽车类的对象,并验证继承和新增的函数。 (选做)从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。 如果不把vehicle设置为虚基类,会有什么问题? 编程试试看。 #include usingnamespacestd; classvehicle { public: vehicle(floata,floatb,intc); Set(floata,floatb,intc); Run(); Stop(); protected: floatMaxSpeed; floatWeight; intwheel; }; vehicle: : vehicle(floata,floatb,intc) { MaxSpeed=a; Weight=b; wheel=c; } vehicle: : Set(floata,floatb,intc) { MaxSpeed=a; Weight=b; wheel=c; } vehicle: : Run() { cout<<"thevehicleisrunning! "< } vehicle: : Stop() { cout<<"thevehicleisstop! "< } classbicycle: virtualpublicvehicle { public: bicycle(floata,floatb,intc,floatd): vehicle(a,b,c) { Height=d; }; Set1(floatd); show(); protected: floatHeight; }; bicycle: : Set1(floatd) { Height=d; } bicycle: : show() { cout<<"theMaxSpeed(km/h)ofbicycleis: "< cout<<"theWeight(T)ofbicycleis: "< cout<<"thewheelofbicycleis: "< cout<<"theHeightofbicycleis: "< } classmotorcar: virtualpublicvehicle { public: motorcar(floata,floatb,intc,intd): vehicle(a,b,c) { SeatNum=d; } Set2(intd); show(); protected: intSeatNum; }; motorcar: : Set2(intd) { SeatNum=d; } motorcar: : show() { cout<<"theMaxSpeed(km/h)ofmotorcaris: "< cout<<"theWeight(T)ofmotorcaris: "< cout<<"thewheelofmotorcaris: "< cout<<"theSeatNumofmotorcaris: "< } classmotorcycle: publicbicycle,publicmotorcar { public: motorcycle(floata,floatb,intc,floatd,inte): vehicle(a,b,c),bicycle(a,b,c,d),motorcar(a,b,c,e) {} show(); }; motorcycle: : show() { cout<<"theMaxSpeed(km/h)ofmotorcycleis: "< cout<<"theWeight(T)ofmotorcycleis: "< cout<<"thewheelofmotorcycleis: "< cout<<"theHeightofmotorcycleis: "< cout<<"theSeatNumofmotorcycleis: "< } intmain() { bicyclebicy(25.0,0.2,2,1.0); floata,b,c; intd; bicy.show(); bicy.Stop(); cout<<"pleasecinMaxSpeed(km/h)Weight(T)wheelHeightofbicycle: "; cin>>a>>b>>d>>c; bicy.Set(a,b,d); bicy.Set1(c); bicy.show(); bicy.Run(); cout< motorcarmotor(60.0,1.0,2,2); floatm,n; inti,j; motor.show(); motor.Stop(); cout<<"pleasecinMaxSpeed(km/h)Weight(T)wheelSeatNumofmotor: "; cin>>m>>n>>i>>j; motor.Set(m,n,i); motor.Set2(j); motor.show(); motor.Run(); cout< motorcyclemotorbicy(60.0,1.0,2,1.1,2); floatp,q,w; intf,g; motorbicy.show(); motorbicy.Stop(); cout<<"pleasecinMaxSpeed(km/h)Weight(T)wheelHeightSeatNumofmotor: "; cin>>p>>q>>f>>w>>g; motorbicy.Set(p,q,f); motorbicy.Set1(w); motorbicy.Set2(g); motorbicy.show(); motorbicy.Run(); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 经典 练习