收藏 分销(赏)

2023年C++实验报告2.doc

上传人:a199****6536 文档编号:3185121 上传时间:2024-06-24 格式:DOC 页数:9 大小:115.54KB 下载积分:6 金币
下载 相关 举报
2023年C++实验报告2.doc_第1页
第1页 / 共9页
2023年C++实验报告2.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
试验汇报 第8章 多态性 汇报书 名称 试验8-1 多态性1 姓名 陈高雪 指导教师 乐仁昌 学号 日期 任务清单 l 场景 申明Point类,有坐标_x,_y两个组员变量;对Point类重载“++”(自增)、“——”(自减)运算符,实现对坐标值旳变化。 l 试验措施 编写程序申明Point类,在类中申明整型旳私有组员变量_x、_y,申明组员函数Point &operator ++();Point operator ++(int);以实现对Point类重载“++”(自增)运算符,申明组员函数Point &operator --();Point operator --(int);以实现对Point类重载“——”(自减)运算符,实现对坐标值旳变化。程序名:lab8_1.cpp l 试验预估时间 40分钟 l 试验成果 l #include<iostream> using namespace std; class Point { public: Point(int x=0,int y=0); Point &operator ++(); Point operator ++(int); Point &operator --(); Point operator --(int); void disypaly(); private: int _x,_y; }; Point::Point(int x,int y) { _x=x; _y=y; } void Point::disypaly() { cout<<"("<<_x<<","<<_y<<")"<<endl; } Point &Point::operator ++() { _x++; _y++; return *this; } Point Point::operator ++(int) { Point old=*this; ++(*this); return old; } Point &Point::operator --() { _x--; _y--; return *this; } Point Point::operator --(int) { Point old=*this; --(*this); return old; } void main() { Point point(15,20); cout<<"初始坐标: "; point.disypaly(); cout<<"point++坐标: "; (point++).disypaly(); cout<<"++point坐标: "; (++point).disypaly(); cout<<"Point--坐标: "; (point--).disypaly(); cout<<"--Point坐标: "; (--point).disypaly(); } 汇报书 名称 试验8-2多态性2 姓名 指导教师 学号 日期 任务清单 l 场景 申明一种车(vehicle)基类,具有Run、Stop等组员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们均有Run、Stop等组员函数。观测虚函数旳作用。 l 试验措施 编写程序申明一种车(vehicle)基类,具有Run、Stop等组员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们均有Run、Stop等组员函数。在main()函数中申明vehicle、bicycle、motorcar、motorcycle旳对象,调用其Run()、Stop()函数,观测其执行状况。再分别用vehicle类型旳指针来调用这几种对象旳组员函数,看看能否成功;把Run()、Stop()申明为虚函数,再试试看。程序名:lab8_2.cpp l 试验预估时间 35分钟 l 试验成果 l #include<iostream> using namespace std; class vehicle { public: virtual void Run() { cout << "vehicle Run::display()" << endl; } virtual void Stop() { cout << "vehicle Stop::display()" << endl<<endl; } }; class bicycle: virtual public vehicle { public: void Run() { cout << "bicycle Run::display()" << endl; } void Stop() { cout << "bicycle Stop::display()" << endl<<endl; } }; class motorcar:virtual public vehicle { public: void Run() { cout << "motorcar Run::display()" << endl; } void Stop() { cout << "motorcar Stop::display()" << endl<<endl; } }; class motorcycle: public bicycle,public motorcar { public: void Run() { cout << "motorcycle Run::display()" << endl; } void Stop() { cout << "motorcycle Stop::display()" << endl<<endl; } }; void fun(vehicle *prt) { prt->Run(); prt->Stop(); } void main() { vehicle vehicle1,*p; bicycle bicycle1; motorcar motorcar1; motorcycle motorcycle1; p=&vehicle1; fun(p); p=&bicycle1; fun(p); p=&motorcar1; fun(p); p=&motorcycle1; fun(p); } l #include<iostream> using namespace std; class vehicle { public: virtual void Run() { cout << "vehicle Run::display()" << endl; } virtual void Stop() { cout << "vehicle Stop::display()" << endl<<endl; } }; class bicycle: virtual public vehicle { public: void Run() { cout << "bicycle Run::display()" << endl; } void Stop() { cout << "bicycle Stop::display()" << endl<<endl; } }; class motorcar:virtual public vehicle { public: void Run() { cout << "motorcar Run::display()" << endl; } void Stop() { cout << "motorcar Stop::display()" << endl<<endl; } }; class motorcycle: public bicycle,public motorcar { public: void Run() { cout << "motorcycle Run::display()" << endl; } void Stop() { cout << "motorcycle Stop::display()" << endl<<endl; } }; void main() { vehicle vehicle1; vehicle1.Run(); vehicle1.Stop(); bicycle bicycle1; bicycle1.Run(); bicycle1.Stop(); motorcar motorcar1; motorcar1.Run(); motorcar1.Stop(); motorcycle motorcycle1; motorcycle1.Run(); motorcycle1.Stop(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 实验设计

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服