南昌大学数据结构实验任燕.doc

实 验 报 告 实验课程: 数据结构实验 学生姓名: 王维 学 号: 6100212202 专业班级: 网络工程121班 2014年 06月 25日 目录 实验一:测试长方形 1 实验二:顺序表 10 实验三:非循环单链表 28 实验四:循环双链表 48 南昌大学实验报告 ---(1)测试长方形 学生姓名: 王维 学 号: 6100212202 专业班级: 网工121班 实验类型:□ 验证 □ 综合 ■ 设计 □ 创新 实验日期: 实验成绩: 【实验题目】 长方形C++类的定义与实现 【实验步骤】 需求分析 定义一个长方形类,能完成设置序号,设置长和宽,求长方形面积,以及重载拷贝初始化构造函数使之能直接通过=赋值。 概要设计 抽象长方形类: ADT Rectangle { 数据对象:D={序号,长,宽} 基本操作: void setNo(int i); void setLength(ElemType a); void setWidth(ElemType a); ElemType area(); Rectangle(const Rectangle otherD); } 详细设计 ①长方形类基类 ///////////////////////////////////////////////// //长方形数据结构C++声明(基类) ///////////////////////////////////////////////// template typename ElemType class Rectangle { public: //长方形的序号类 class RectangleNo { public: int no; }; void setNo(int i); void setLength(ElemType a); void setWidth(ElemType a); ElemType area(); // Rectangle(); Rectangle(const Rectangle otherD); virtual ~Rectangle(); protected: ElemType length; ElemType width; RectangleNo myNo; }; ///////////////////////////////////////////////// //长方形数据结构C++实现(基类) ///////////////////////////////////////////////// template typename ElemType RectangleElemType::Rectangle() { length = width = 0; cout 自动调用构造函数endl; } //拷贝构造函数 template typename ElemType RectangleElemType::Rectangle(const RectangleElemType otherD) { length = otherD.length; width = otherD.width; myNo = otherD.myNo; cout 自动调用拷贝初始化构造函数为(; coutlength,width)endl; } //析构函数 template typename ElemType RectangleElemType::~Rectangle() { cout\n 第myNo.no个长方形对象(length,width)生存期结束endl; } //功能:设置长方形的序号 template typename ElemType void RectangleElemType::setNo(int i) { myNo.no=i; } //功能:设置长方形长度 template typename ElemType void RectangleElemType::setLength(ElemType a) { length = a; } //功能:设置长方形宽度 template typename ElemType void RectangleElemType::setWidth(ElemType a) { width = a; } /

文档评论(0)

1亿VIP精品文档

相关文档