




已阅读5页,还剩44页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验一实验一 C 类的定义及成员引用类的定义及成员引用 一 实验目的 1 掌握类和对象的概念及特性 2 掌握 C 类和对象的定义 3 掌握对象成员的基本引用方法 4 掌握简单的面向对象程序的编写 二 实验任务 题目题目 1 图形类设计图形类设计 设计三个图形类 Circle 圆 Rectangle 矩形 Triangle 三 角形 1 Cirlce 类基本信息 圆心坐标 半径 Rectangle 类基本信息 长 宽 Triangle 类基本信息 三个顶点坐标 其中 成员变量为 private 属性 成员函数为 public 属性 2 每个图形类有计算图形的面积 GetArea 显示图形的基本信 息函数 Show 修改基本信息的函数 Set 形参 以 Circle 类为例 通过 GetArea 计算圆的面积 Show 函数中显示圆心坐标 直径 周长 面积等基本信息 Set int x int y int r 函数可以修改圆心坐标 和半径 3 当输入数据为不合理数据时 例如 输入的三角形的顶点是否 能组成一个三角形 提示用户输入错误 测试数据 1 Circle 类测试数据 圆心 20 30 半径 5 20 40 30 40 10 5 2 Rectangle 类测试数据 顶点坐标 20 50 40 5 9 10 43 8 3 Triangle 类测试数据 顶点 20 30 40 50 40 20 10 10 50 10 35 10 源程序 include include include using namespace std 圆的面积 class Circle private int x int y int r public Circle int x 0 int y 0 int r 0 float Circle GetArea void Circle show void Circle set int a int b int c x a y b r c float Circle Circle GetArea float s s 3 14 r r return s void Circle Circle show float s if r 0 cout x y setw 6 r setw 6 2 r setw 6 2 3 14 r setw 6 s endl else cout 输入的数据错误 b a b if a c a c if b c b c if x1 x4 x1 x2 y1 y4 y1 y2 0 width b float Rectangle Rectangle GetArea float z z length width return z void Rectangle Rectangle show float z if z 0 cout 矩形的长 length setw 2 宽 width setw 2 面积 z endl else cout 输入数据错误 l3 s sqrt d d l1 d l2 d l3 else s 0 void Triangle show void Triangle set int x1 int y1 int x2 int y2 int x3 int y3 m1 x1 n1 y1 m2 x2 n2 y2 m3 x3 n3 y3 float a b c a sqrt x1 x2 x1 x2 y1 y2 y1 y2 b sqrt x1 x3 x1 x3 y1 y3 y1 y3 c sqrt x3 x2 x3 x2 y3 y2 y3 y2 l1 a l2 b l3 c void Triangle Triangle show float s int c c s switch c case 0 cout 三角形三点坐标 setw 2 m1 n1 setw 2 m2 n2 setw 2 m3 n3 endl cout 三角形边长 setw 2 l1 l2 l3 endl cout 三角形的面积 setw 2 s endl break case 1 cout 输入数据错误 x1 y1 r1 cin x2 y2 r2 cin x3 y3 r3 cin a1 b1 cin a2 b2 cin a3 b3 cin a4 b4 cin k1 l1 k2 l2 k3 l3 cin k4 l4 k5 l5 k6 l6 c1 Circle set x1 y1 r1 c2 Circle set x2 y2 r2 c3 Circle set x3 y3 r3 r Rectangle set a1 b1 a2 b2 a3 b3 a4 b4 t1 Triangle set k1 l1 k2 l2 k3 l3 t2 Triangle set k4 l4 k5 l5 k6 l6 cout 圆 endl cout 圆心坐标 setw 6 直径 setw 6 半径 setw 6 周长 setw 6 面积 endl c1 Circle GetArea c1 Circle show c2 Circle GetArea c2 Circle show c3 Circle GetArea c3 Circle show cout 矩形 endl r Rectangle GetArea r Rectangle show cout 三角形 endl t1 Triangle GetArea t1 Triangle show t2 Triangle GetArea t2 Triangle show return 0 运行结果 题目2 学生类设计 设计一个 Studnet 学生 类 1 基本信息 学号 姓名 性别 出生日期 年级 班级 院系 专业 其中 基本信息为 private 属性 成员函数为public 属性 2 Student 类有多个构造函数 缺省构造函数 带参数的构 造函数 带默认参数的构造 函数 3 基本功能 1 可以从键盘输入学生的基本信息 2 SetInfo 形参表 函数可以修改学生的基本信息函数 例如 班级 专业等 3 Show 函数显示学生信息 4 当输入数据为不合理数据时 提示用户输入错误 测试数据 1 学号 姓名 zhangesan 出生日期 85 年11 月年级 05 级 班级 jy01 院系 computer 专业 application 2 学号 姓名 lihuan 出生日期 84 年11 月 3 学号 姓名 wang 出生日期 85 年7 月年级 05 级 班级 jy01 院系 computer 专业 software 4 学号 姓名 zhan 出生日期 85 年11 月年级 05 级 班级 jy02 5 学号 姓名 song 出生日期 85 年11 月 思考与扩展 采用什么方式可以让学号自动按输入顺序生成 源程序代码 include include include using namespace std class Student private int number string name string year string grade string clas string department string profession public Student grade 05级 Student string cla Student Student int num number num void setInto int b string b1 string b2 string b3 string b4 string b5 string b6 number b name b1 year b2 grade b3 clas b4 department b5 profession b6 void show Student Student string cla clas cla void Student show cout 学号 number setw 6 姓名 name setw 10 出生日期 year setw 6 年级 grade setw 6 班级 clas setw 6 院系 department setw 6 专业 profession endl endl int main Student stud 5 int i j num for i 0 num i a1 a2 a3 a4 a5 a6 stud i setInto num a1 a2 a3 a4 a5 a6 stud i show return 0 实验二实验二 C 中的构造函数与析构函数中的构造函数与析构函数 一 实验目的 5 掌握 C 中使用构造函数创建对象 6 掌握 C 中使用析构函数释放对象 7 掌握构造函数的重载特性 8 掌握类相关的指针及数组的使用方法 9 掌握 const 成员及 static 成员的特性 10 掌握友元的特性 二 实验任务 题目题目 人员管理人员管理 设计某小型公司的 employee 人员 类 1 类 类 1 employee 类 基本信息 编号 姓名 性别 出生日期 职位等 出生日期使用自定义的 Date 日期 类 其中 基本信息为 private 属性 成员函数为public 属性 多个构造函数 缺省构造函数 带参数的构造函数 带默认 参数的构造函数 可以从外部访问类成员的友员函数 2 Date 类 成员变量 年 月 日 成员函数 SetYear int year SetMonth int month SetDay int day GetYear GetMonth GetDay 2 基本功能 基本功能 1 职工信息的录入 2 职工信息的显示 3 用对象数组保存已输入的职工对象 4 可以修改人员的基本信息 如 姓名 职位等 5 可以通过编号或姓名进行人员查询 3 实验提示 1 注意带参数的构造函数和带默认参数的构造函数的声明与 定义 2 定义employee 类的成员变量时 应注意变量类型的声明 3 在查询时 通过申明成employee 类的友元函数来访问类 的成员变量 实验提示实验提示 1 注意带参数的构造函数和带默认参数的构造函数的声明与 定义 2 定义employee 类的成员变量时 应注意变量类型的声明 3 在查询时 通过申明成employee 类的友元函数来访问类 的成员变量 测试数据测试数据 1 编号 10001 姓名 jack 出生日期 80 年11 月1 日职 位 普通 2 编号 10002 姓名 andy 出生日期 75 年1 月9 日职位 经理 3 编号 10003 姓名 alex 出生日期 81 年4 月3 日职位 秘书 4 编号 10005 姓名 lili 出生日期 82 年10 月1 日职位 技师 源代码 include include define N 100 using namespace std class Date private int year int month int day public void SetYear int x year x void SetMonth int x month x void SetDay int x day x int GetYear return year int GetMonth return month int GetDay return day Date year 2000 month 1 day 1 Date int x int y int z year x month y day z class employee private int no string name string sex Date birth string pos public employee no 0 birth 2000 1 1 name Andy sex male pos intern employee int n string str string y Date day string p no n name str sex y birth day pos p friend string Getname employee x string name1 name1 x name return name1 friend int Getno employee x return x no friend string Getsex employee x string sex1 sex1 x sex return sex1 friend Date Getbirth employee x return x birth friend string Getpos employee x string pos1 pos1 x pos return pos1 friend void display cout n t t endl cout t t1 录入 2 显示 3 修改 4 查找 0 退出 endl cout t t endl cout choose while choose 0 switch choose case 1 if num 100 cout full n continue cout n cout y m d cout str cout s cout p persons num new employee n str s Date y m d p break case 2 cout 编号 t 姓名 t 性别 t 生日 t t 职务 endl for i 0 i num i cout Getno persons i t Getname persons i t Get sex persons i t Getbirth persons i GetYear 年 Getbirth persons i GetMonth 月 Getbirth persons i GetDay 日 t Getpos persons i endl break case 3 cout n for i 0 i num 1 i if n Getno persons i cout n t t endl cout t t1 名字修改 2 职位修改 endl cout t t endl cout 选择您要进行的修改方式 choose1 switch choose1 case 1 cout str n Getno persons i s Getsex persons i y Getbirth persons i GetYear m Getbirth persons i GetMonth d Getbirth persons i GetDay p Getpos persons i delete persons i persons i new employee n str s Date y m d p break case 2 cout p n Getno persons i str Getname persons i s Getsex persons i y Getbirth persons i GetYear m Getbirth persons i GetMonth d Getbirth persons i GetDay delete persons i persons i new employee n str s Date y m d p break break case 4 cout n t t endl cout t t1 按编号 2 按姓名 endl cout t t endl cout n switch n case 1 cout n for i 0 i num i if n Getno persons i cout 编号 t 姓名 t 性别 t 生日 t t 职 务 endl cout Getno persons i t Getname persons i t Get sex persons i t Getbirth persons i GetYear 年 Getbirth persons i GetMonth 月 Getbirth persons i GetDay 日 t Getpos persons i endl break if i num cout 该员工不存在 endl break case 2 cout str for i 0 i num i if str Getname persons i cout 编号 t 姓名 t 性别 t 生日 t t 职 务 endl cout Getno persons i t Getname persons i t Get sex persons i t Getbirth persons i GetYear 年 Getbirth persons i GetMonth 月 Getbirth persons i GetDay 日 t Getpos persons i endl break if i num cout 该员工不存在 choose return 0 实验三实验三 C 中的运算符重载中的运算符重载 一 实验目的 1 掌握C 中运算符重载的机制和运算符重载的方式 2 熟悉几种常见的运算符的重载方式 3 掌握类型转换的方式 二 实验任务 题目 定义一个复数类 用real描述其实部 imag描述其虚部 使用友元函数 重载复数的 加 减 乘 除 四则运算 实现 复数的四则运算 提示 运算符重载为友元函数的声明如下 friend complex operator const complex friend complex operator const complex friend complex operator const complex friend complex operator const complex 源代码 include using namespace std class Complex private double real double imag public void set friend Complex operator const Complex friend Complex operator const Complex friend Complex operator const Complex friend Complex operator const Complex friend ostream friend void display cout endl cout 加法 减法 乘法 除法 0 退出 endl cout endl cout real imag ostream output c1 imag i choose while choose 0 if choose switch choose case cout 输入第一复数 c1 set cout 输入第二复数 c2 set c3 c1 c2 cout 输出结果 cout c1 c2 c3 endl break case cout 输入第一复数 c1 set cout 输入第二复数 c2 set c3 c1 c2 cout 输出结果 cout c1 c2 c3 endl break case cout 输入第一复数 c1 set cout 输入第二复数 c2 set cout 输出结果 c3 c1 c2 cout c1 c2 c3 endl break case cout 输入第一复数 c1 set cout 输入第二复数 c2 set c3 c1 c2 cout 输出结果 cout c1 c2 c3 choose cout 谢谢 endl return 0 实验四实验四 C 中的继承与派生中的继承与派生 一 实验目的 4 理解 C 中继承与派生的概念 5 掌握 C 中各种不同继承方式的访问属性差别 6 掌握单继承与多继承的实现方法 7 掌握派生类构造函数与析构函数的实现及调用顺序 8 掌握虚基类的使用方法 二 实验任务 题目 小型公司人员管理 某小型公司有四类人员 经理 技术人员 销售经理 销售员 设计一个基类 employee 派生出 manager 经理 technician 技术人员 salesmanager 销 售经理 saleman 销售员 销售经理既是经理又是销售员 兼具 两类人员的特点 因此同时继承 manager 和 salesman 两个类 1 类定义 类定义 1 employee 类 基本信息 编号 姓名 性别 出生日期 职位 薪水等 出生日期使用自定义的 Date 日期 类 2 Date 类 成员变量 年 月 日 3 派生类technician 新增属性 工作时间 派生类 saleman 新增属性 销售额 所属部门 2 实现人员信息的录入与显示 实现人员信息的录入与显示 3 计算并显示个人月薪 计算并显示个人月薪 月薪计算办法 总经理拿固定月薪8000 元 技术人员按每小时 25 元领取月薪 推销员 的月薪按当月销售额的4 提成 销售经理固定月薪5000 元加所 管辖部门当月销售总额 的5 提示 1 在基类中 除了定义构造函数和析构函数 还应统一 定义对各类人员信息应有的操作 规范类族中各派生类的基 本行为 但是各类人员的月薪计算方法不同 不能在基类 employee 中统一确定计算方法 各类人员信息的显示内容不 同 同样不能在基类 employee 中统一确定显示方法 在基类 中实现上述功能的函数体应为空 在派生类中根据同名覆盖 原则定义各自的同名函数实现具体功能 源代码 include include using namespace std class Date private int year int month int day public void SetYear int x year x void SetMonth int x month x void SetDay int x day x int GetYear return year int GetMonth return month int GetDay return day class employee protected int num string nam string sex Date birth string position float salary public void display cout 编号 num 姓名 nam 性别 sex 出生日期 birth GetYear birth GetMonth birth GetDay 职位 position class manager virtual public employee public void set manager int n string na string se int y int m int d string pos num n nam na sex se birth SetYear y birth SetMonth m birth SetDay d position pos void display employee display void total salary salary 8000 cout 经理月薪 cout salary 元 class technician virtual public employee protected float Time public float t void set technician int n string na string se int y int m int d string pos float t num n nam na sex se birth SetYear y birth SetMonth m birth SetDay d position pos Time t void display employee display cout 工作时间 Time endl void total salary salary 25 Time cout 技术人员月薪 cout salary 元 class saleman virtual public employee protected float sale string dep public void set saleman int n string na string se int y int m int d string pos float sale1 string de num n nam na sex se birth SetYear y birth SetMonth m birth SetDay d position pos sale sale1 dep de void display employee display cout position 销售额 sale 所属部门 dep endl void total salary salary sale 0 04 cout 销售员月薪 salary 元 class salesmanager public manager public saleman public void total salary salary sale 0 05 5000 cout 销售经理月薪 salary 元 int main manager m1 technician tec saleman s salesmanager sg int n y m d choose choose1 string str se p cout n t t endl cout t t1 输入信息与显示 2 个人月薪 0 退出 endl cout t t endl cout choose while choose 0 switch choose case 1 cout n cout y m d cout str cout se cout p if p manager m1 set manager n str se y m d p m1 display else if p technician float t cout t tec set technician n str se y m d p t tec display else if p saleman float sale2 string de cout sale2 cout de s set saleman n str se y m d p sale2 de s display else float sale1 string de cout sale1 cout de sg saleman set saleman n str se y m d p sale1 de sg saleman display break case 2 cout n t t endl cout t t11 经理 12 技术人员 13 销售员 14 销 售经理 endl cout t t endl cout choose1 while choose1 0 switch choose1 case 11 m1 total salary break case 12 tec total salary break case 13 s total salary break case 14 sg total salary break choose1 0 break cout n t t endl cout t t1 输入信息与显示 2 个人月薪 0 退出 endl cout t t endl cout choose return 0 实验五 C 中的多态性 一 实验目的 1 理解C 中的多态性 2 掌握C 中虚函数的定义
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 陀螺果育苗技术规程
- 2025年南昌县公安局招聘警务辅助人员考试笔试试题【答案】
- 2025年超铀元素及其提取设备合作协议书
- 河南大学科技成果转化基金项目申请书
- 项目策划编制指南
- 大学生寒假.实践报告(卖春联)
- 2025产科护士个人工作计划
- 2025年医用超声诊断设备项目发展计划
- 2025年太阳能电池背膜项目发展计划
- 工作坊聚焦教育技术与教学质量的双重提升
- 2024年安徽省合肥市北城片区七年级数学第一学期期末学业水平测试试题含解析
- 2025至2030中国铜冶炼行业发展现状及应用需求现状分析报告
- 农业保险培训课件
- 茶园租赁合同(含茶叶加工销售)
- 2025至2030全球及中国浮式液化天然气行业产业运行态势及投资规划深度研究报告
- 药品连锁总部管理制度
- 2025至2030中国家用清洁工具行业发展趋势分析与未来投资战略咨询研究报告
- 20250617国金证券机器人行业研究垂直领域具身智能机器人的野望416mb
- 数字时代亲属关系重构-洞察及研究
- 管理类本科论文
- 招商人员笔试题目及答案
评论
0/150
提交评论