c++试验编程.docx
- 文档编号:3518647
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:23
- 大小:151.21KB
c++试验编程.docx
《c++试验编程.docx》由会员分享,可在线阅读,更多相关《c++试验编程.docx(23页珍藏版)》请在冰点文库上搜索。
c++试验编程
武汉工业学院
数学与计算机学院
C++程序设计实验报告
专业:
计算机大类
班级:
1007班
学号:
*******
姓名:
****
指导教师:
****
2011年11月23日
实验五
实验名称:
继承与派生
实验内容:
1、定义一个基类Animal,有私有整形成员变量age,构造其派生类dog,在其成员函数SetAge(intn)中直接给age赋值,看看会有什么问题,把age该为共有成员变量,还会有问题吗?
。
2、定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
源代码:
#include
usingnamespacestd;
classAnimal
{
public:
intage;
};
classdog:
publicAnimal
{
public:
voidSetAge(intn)
{
//cout<<"改变前的Animal类的成员变量age="< age=n; cout<<"改变后的Animal类的成员变量age="< } }; classBaseClass { public: BaseClass() { cout<<"BaseClass类的默认构造函数被调用"< Number=0; } BaseClass(inti) { cout<<"BaseClass类的带参数的构造函数被调用"< Number=i; } BaseClass(BaseClass&b1) { cout<<"BaseClass类的拷贝构造函数被调用"< Number=b1.Number; } ~BaseClass() { cout<<"BaseClass类的析构函数被调用"< } intNumber; }; classDerivedClass: publicBaseClass { public: DerivedClass() { cout<<"DerivedClass类的默认构造函数被调用"< d=0; } DerivedClass(inti) { cout<<"DerivedClass类的带参数的构造函数被调用"< d=i; } DerivedClass(inti,intj): BaseClass(i) { cout<<"DerivedClass类的带参数的构造函数被调用"< d=j; } DerivedClass(DerivedClass&d1): BaseClass(d1) { cout<<"DerivedClass类的拷贝构造函数被调用"< d=d1.d; } ~DerivedClass() { cout<<"DerivedClass类的析构函数被调用"< } private: intd; }; voidmain(void) { Animala; a.age=2; cout<<"改变前的Animal类的成员变量age="< dogDOG; DOG.SetAge(6); DerivedClassder1; DerivedClassder2(3); DerivedClassder3(4,5); DerivedClassder4=der3; } 运行结果截图: 实验小结: 通过本次实验,我明白了如何继承派生一个类,及如何定义派生类的构造函数,构造函数是怎样执行的。 基类数据成员在派生后的访问权限发生改变。 实验六 实验名称: 虚基类 实验内容: 1、编写程序定义一个车(vehicl)基类,由此派生出自行车(bicycle)类、汽车(motorcar)类,注意把vehicl设置为虚基类。 再从bicycle和motorcar派生出摩托车(motorcycle)类,在main中测试这个类。 2、编译成功后,把vehicl类设置为非虚基类,再编译一次,此时系统报错,无法编译成功。 因为若不把vehicl类设置为虚基类,会出现二义性错误,程序不能成功编译。 源代码: #include usingnamespacestd; classvehicl { public: vehicl() { cout<<"vehicl类的默认构造函数被调用"< MaxSpeed=0; Weight=0; } vehicl(inti,intj) { cout<<"vehicl类的带参数构造函数被调用"< MaxSpeed=i; Weight=j; } vehicl(vehicl&v) { cout<<"vehicl类的拷贝构造函数被调用"< MaxSpeed=v.MaxSpeed; Weight=v.Weight; } ~vehicl() { cout<<"vehicl类的析构函数被调用"< } intMaxSpeed; intWeight; voidrun(); voidstop(); }; voidvehicl: : run(void) { cout<<"此辆车在工作"< } voidvehicl: : stop(void) { cout<<"此辆车已停止工作"< } classbicycle: publicvirtualvehicl { public: bicycle() { cout<<"bicycle类的默认构造函数被调用"< Height=0; } bicycle(intb1) { cout<<"bicycle类的带参数的构造函数被调用"< Height=b1; } bicycle(intb1,intb2,intb3): vehicl(b1,b2) { cout<<"bicycle类的带参数的构造函数被调用并向基类传递参数"< Height=b3; } bicycle(bicycle&b): vehicl(b) { cout<<"bicycle类的拷贝的构造函数被调用"< Height=b.Height; } ~bicycle() { cout<<"bicycle类的析构函数被调用"< } intHeight; }; classmotorcar: publicvirtualvehicl { public: motorcar() { cout<<"motorcar类的默认构造函数被调用"< SeatNum=0; } motorcar(intm1) { cout<<"motorcar类的带参数的构造函数被调用"< SeatNum=m1; } motorcar(intm1,intm2,intm3): vehicl(m1,m2) { cout<<"motorcar类的带参数的构造函数被调用并向基类传递参数"< SeatNum=m3; } motorcar(motorcar&m): vehicl(m) { cout<<"motorcar类的拷贝的构造函数被调用"< SeatNum=m.SeatNum; } ~motorcar() { cout<<"motorcar类的析构函数被调用"< } intSeatNum; }; classmotorcycle: publicbicycle,publicmotorcar { public: motorcycle() { cout<<"motorcycle类的默认构造函数被调用"< } motorcycle(intm1,intm2,intm3): motorcar(m1,m2,m3) { cout<<"motorcycle类的带参数的构造函数被调用并向基类传递参数"< } motorcycle(intm1,intm2,intm3,intm4): motorcar(m1,m2,m3),bicycle(m1,m2,m4) { cout<<"motorcycle类的带参数的构造函数被调用并向基类传递参数"< } motorcycle(motorcycle&m): motorcar(m),bicycle(m) { cout<<"motorcycle类的拷贝构造函数被调用"< } ~motorcycle() { cout<<"motorcycle类的析构函数被调用"< } }; voidmain(void) { vehicla1; vehicla2(2,3); a2.run(); a2.stop(); bicyclea3; bicyclea4(1,2,3); motorcara5; motorcara6(4,5,6); motorcyclea7; motorcyclea8(2,3,4); motorcyclea9(2,3,4,5); motorcyclea10=a9; } 运行结果截图: 实验小结: 通过本次实验,我体会到在多重继承的过程中会产生二义性,可通过定义虚基类的方式来消除二义性。 定义虚基类后,派生类中所有可能产生二义性的数据成员只会出现一次。 实验七 实验名称: 多态性 实验内容: 定义一个车(vehicl)基类,有run、stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类;从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有run、stop等成员函数。 观察虚函数的作用。 源代码: #include usingnamespacestd; classvehicl { public: vehicl() { cout<<"vehicl类的默认构造函数被调用"< MaxSpeed=0; Weight=0; } vehicl(inti,intj) { cout<<"vehicl类的带参数构造函数被调用"< MaxSpeed=i; Weight=j; } vehicl(vehicl&v) { cout<<"vehicl类的拷贝构造函数被调用"< MaxSpeed=v.MaxSpeed; Weight=v.Weight; } ~vehicl() { cout<<"vehicl类的析构函数被调用"< } intMaxSpeed; intWeight; voidrun(); voidstop(); }; voidvehicl: : run(void) { cout<<"此车在工作"< } voidvehicl: : stop(void) { cout<<"此车已停止工作"< } classbicycle: publicvirtualvehicl { public: bicycle() { cout<<"bicycle类的默认构造函数被调用"< Height=0; } bicycle(intb1) { cout<<"bicycle类的带参数的构造函数被调用"< Height=b1; } bicycle(intb1,intb2,intb3): vehicl(b1,b2) { cout<<"bicycle类的带参数的构造函数被调用并向基类传递参数"< Height=b3; } bicycle(bicycle&b): vehicl(b) { cout<<"bicycle类的拷贝的构造函数被调用"< Height=b.Height; } ~bicycle() { cout<<"bicycle类的析构函数被调用"< } virtualvoidrun(void) { cout<<"此自行车在工作"< } virtualvoidstop(void) { cout<<"此自行车已停止工作"< } intHeight; }; classmotorcar: publicvirtualvehicl { public: motorcar() { cout<<"motorcar类的默认构造函数被调用"< SeatNum=0; } motorcar(intm1) { cout<<"motorcar类的带参数的构造函数被调用"< SeatNum=m1; } motorcar(intm1,intm2,intm3): vehicl(m1,m2) { cout<<"motorcar类的带参数的构造函数被调用并向基类传递参数"< SeatNum=m3; } motorcar(motorcar&m): vehicl(m) { cout<<"motorcar类的拷贝的构造函数被调用"< SeatNum=m.SeatNum; } ~motorcar() { cout<<"motorcar类的析构函数被调用"< } voidrun(void) { cout<<"此汽车在工作"< } voidstop(void) { cout<<"此汽车已停止工作"< } intSeatNum; }; classmotorcycle: publicbicycle,publicmotorcar { public: motorcycle() { cout<<"motorcycle类的默认构造函数被调用"< } motorcycle(intm1,intm2,intm3): motorcar(m1,m2,m3) { cout<<"motorcycle类的带参数的构造函数被调用并向基类传递参数"< } motorcycle(intm1,intm2,intm3,intm4): motorcar(m1,m2,m3),bicycle(m1,m2,m4) { cout<<"motorcycle类的带参数的构造函数被调用并向基类传递参数"< } motorcycle(motorcycle&m): motorcar(m),bicycle(m) { cout<<"motorcycle类的拷贝构造函数被调用"< } ~motorcycle() { cout<<"motorcycle类的析构函数被调用"< } voidrun(void) { cout<<"此摩托车在工作"< } voidstop(void) { cout<<"此摩托车已停止工作"< } }; voidmain(void) { vehicl*b1; bicycle*b2; motorcar*b3; vehicla2(2,3); bicyclea4(1,2,3); motorcara6(4,5,6); motorcyclea9(2,3,4,5); motorcyclea10=a9; b1=&a9; b1->run(); b1->stop(); b2=&a9; b2->run(); b2->stop(); b3=&a9; b3->run(); b3->stop(); } 运行结果截图: 实验小结: 通过本次实验,我明白了如何实现多态,通过定义虚函数的方式,实现动态访问各不同类中具有相同功能的函数。 实验八 实验名称: 运算符重载 实验内容: 定义Point类,有坐标_x,_y两个成员变量;对Point类重载“++”“--”运算符,实现对坐标的改变。 源代码: #include usingnamespacestd; classPoint { public: Point() { cout<<"Point的默认构造函数被调用"< } Point(inti,intj) { cout<<"Point的带参数构造函数被调用"< x=i; y=j; } Point(Point&p) { cout<<"Point的默认构造函数被调用"< x=p.x; y=p.y; } ~Point() {cout<<"Point的析构函数被调用"< Point&operator++(); Point&operator--(); intx,y; }; Point&Point: : operator++() { Pointp; p.x++; p.y++; return*this; } Point&Point: : operator--() { Pointp; p.x--; p.y--; return*this; } voidmain(void) { Pointp1(2,3); cout<<"当前的坐标为: \n"< cout<<"类自加后的坐标为: \n"<<++p1.x<<++p1.y< cout<<"类自减后的坐标为: \n"<<--p1.x<<--p1.y< } 运行结果截图: 实验小结: 通过本次实验,我体会到如何实现运算符重载。 通过运算符重载可以给运算符赋予新的含义,从而实现相关功能。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 试验 编程