




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验报实验报告告 课程名称面向对象程序设计面向对象程序设计 系 别机械与电子工程系 班 级N 电信 091F 学生姓名邓建平 07 学 号24092200003 任课教师齐 琦 南湖学院教务办 实验六实验六 继承与派生 下继承与派生 下 一 实验目的一 实验目的 掌握类模板和模板类的概念 了解它们的定义和使用 掌握多重继承的使用 二 实验内容二 实验内容 题目一 定义一个单向链表的模板类 实现添加 删除 查找操作 要求 使用模板类 注意 可参考课堂代码实现 代码 include include using namespace std template class List 对 List 类的申明 template class Node 定义一个描述节点的类 T data Node next public Node Node T d void InsertAfter Node p Node RemoveAfter friend class List templateNode Node data 0 next NULL templateNode Node T d data d next NULL templatevoid Node InsertAfter Node p p next next next p templateNode Node RemoveAfter Node temp next if next NULL next temp next return temp templateclass List 定义一个描述链表的类 Node head rear public List 空链表的构造 链表中只含表头结 点 void MakeEmpty 清空链表 删除链表中除表头结点 以外的所有结点 List 析构函数 Node Find T d 查找数据域与 d 相同的结点 返回所找到结点的指针 int Length 计算链表长度 返回链表中除表 头结点以外的结点个数 void PrintList 输出链表中各结点的数据域 void InsertFront Node p 向前插入结点 链表首部插入一个 结点 void InsertFront T d void InsertRear T d void InsertRear Node p 向后插入结点 链表尾部插入一个 结点 void InsertOrder Node p 有序插入结点 插入结点后链表仍 有序 Node CreateNode T d 创建数据域为 d 的新结点 返回 新结点的指针 void Save void Read void CreateList Node DeleteNode Node p 从链表中删除指针为 p 的结点 templateList List head rear new Node templatevoid List MakeEmpty Node temp while head next NULL temp head next head next temp next delete temp rear head templateList List MakeEmpty delete head templateNode List Find T d Node temp head next while temp NULL return temp templateint List Length Node temp head next int count 0 while temp NULL temp temp next count return count templatevoid List PrintList Node temp head next while temp NULL cout data next templatevoid List InsertFront T d Node temp new Node temp data d temp next head next head next temp if rear head rear temp templatevoid List InsertFront Node p p next head next head next p if rear head rear p templatevoid List InsertRear T d rear next new Node rear next data d rear rear next rear next NULL templatevoid List InsertRear Node p rear next new Node rear next data p data rear rear next rear next NULL templateNode List CreateNode T d Node temp new Node d return temp templatevoid List CreateList T d cout 现在开始创建链表 请依次输入数据 以 Ctrl Z 结束 endl while cout d rear next new Node rear next data d rear rear next rear next NULL templatevoid List Save ofstream outfile data txt ios app Node temp head next while temp NULL outfile data next templatevoid List Read ifstream infile data txt if infile cout d rear next new Node rear next data d rear rear next rear next NULL templatevoid List InsertOrder Node p Node temp1 head Node temp2 head next temp1 指向 temp2 的前面的一个结点 while temp2 NULL if p data temp2 data break 找到第一个比要插入结点小的结点 则可以确定 新结点须插入在 temp2 前 temp1 temp2 temp2 temp2 next temp1 InsertAfter p if rear temp1 rear temp1 next templateNode List DeleteNode Node p Node temp head while temp next NULL if temp next rear rear temp return temp RemoveAfter void main List l l CreateList l PrintList cout Hello 题目二 通过多重继承定义研究生类 研究生既有学生的属性 又有老师 的属性 注意 思考构造函数的调用顺序 代码 include using namespace std class person char name int age public person cout the constructor of class person n person cout the dstructor of class person n class student virtual public person char department int level public student cout the constructor of class student n student cout the constructor of class student n class teacher virtual public person char major int salary public teacher cout the constructor of class teacher n teacher cout the constructor of class teacher n class Postgraduate public student public teacher public Postgraduate cout the constructor of class Postgraduate n Postgraduate cout the constructor of class
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 药剂类考试真实模拟试题及答案
- 在线中级考试试题及答案
- 自然药物的开发与应用试题及答案
- 激光材料研究促进行业发展的重要性试题及答案
- 电脑操作能力试题及答案
- 死亡监测试题及答案
- 大学刀术考试题及答案
- 文化产业管理证书考试题型解析
- 聚焦未来公共卫生执业医师试题及答案
- 2025三年级语文下册期中假期练习考试人教版
- 绍兴银行社会招聘真题
- 太空态势感知算法-洞察分析
- 路灯安装工程项目实施重点、难点和解决方案
- 裁纸机项目质量管理方案
- 建筑工程材料测试题含答案
- 《虹桥机场旅客服务质量提升对策探究》6100字(论文)
- 丰子恺课件教学课件
- 让每一个闪光在每一天成长-四年级期中家长会 课件
- 2024下半年软考信息安全工程师考试真题-及答案-打印
- 中华人民共和国能源法
- 《义务教育数学课程标准(2022年版)》初中内容解读
评论
0/150
提交评论