07 自定义数据类型_第1页
07 自定义数据类型_第2页
07 自定义数据类型_第3页
07 自定义数据类型_第4页
07 自定义数据类型_第5页
已阅读5页,还剩15页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 面向对象程序设计 第7章自定义数据类型 2 本章内容 结构体的概念 定义 初始化 引用和赋值结构体数组指向结构体变量的指针结构体作为函数参数new和delete运算符 3 1 结构体 structStudent intnum stringname charsex intage floatscore stringaddress Studentstudent1 10001 ZhangXin M 19 90 5 Shanghai 类型定义 变量定义和初始化 4 结构体变量的另两种定义形式 structStudent intnum stringname charsex intage floatscore stringaddress student1 student2 struct intnum stringname charsex intage floatscore stringaddress student1 student2 形式一 形式二 5 结构体变量的引用和赋值 Students1 s2 s3 s1 s2 s1 num 10010 s3 score s1 score s2 score cout s1 num Students1 s2 s3 s1 10001 ZhangXin M 19 90 5 Shanghai s1 s2 s3 cout s1 正确 错误 6 举例7 1 P206 include include includeusingnamespacestd structStudent intID stringname stringsex floatscore intmain Studentstudent1 student2 10002 张三 女 89 5 student1 student2 cout 学号姓名性别成绩 endl cout setw 6 student1 ID cout setw 8 student1 name cout setw 6 student1 sex cout setw 6 student1 score endl return0 7 2 结构体数组 Studentstu 10101 LiLin M 18 87 5 103BeijingRoad 10102 ZhangFun M 19 99 130ShanghaiRoad 10104 WangMin F 20 78 5 1010ZhongshanRoad 8 举例7 1 2 接续例7 1 include include includeusingnamespacestd constintNUM STU 3 structStudent intID stringname stringsex floatscore intmain Studentstu NUM STU 10002 张三 女 89 5 10102 李四 男 99 10104 王五 男 78 5 cout 学号姓名性别成绩 endl for inti 0 i NUM STU i cout setw 6 stu i ID cout setw 8 stu i name cout setw 6 stu i sex cout setw 6 stu i score endl return0 9 3 指向结构体变量的指针 structStudent intnum stringname charsex intage floatscore stringaddress Studentstudent1 Student p 10 举例7 1 3 接续例7 1 2 链表结构 structStudent intID stringname stringsex floatscore Student pNext intmain Students1 10002 张三 女 89 5 NULL Students2 10102 李四 男 99 NULL Students3 10104 王五 男 78 5 NULL Student pHead p pHead 11 4 结构体作为函数参数 用结构体变量名作参数 用指向结构体变量的指针作参数 用结构体变量的引用作参数 12 举例7 5 P215 include includeusingnamespacestd structStudent intnum stringname floatscore 3 voidprint Studentstu cout stu num stu name stu score 0 stu score 1 stu score 2 endl intmain Studentstu stu num 12345 stu name LiFung stu score 0 67 5 stu score 1 89 stu score 2 78 5 print stu return0 用结构体变量名作参数 13 举例7 5 P215 include includeusingnamespacestd structStudent intnum stringname floatscore 3 voidprint Student p coutnumnamescore 0 score 1 score 2 endl intmain Studentstu stu num 12345 stu name LiFung stu score 0 67 5 stu score 1 89 stu score 2 78 5 print 用指针作参数 14 举例7 5 P215 include includeusingnamespacestd structStudent intnum stringname floatscore 3 voidprint Student intmain Studentstu stu num 12345 stu name LiFung stu score 0 67 5 stu score 1 89 stu score 2 78 5 print stu return0 用引用作参数 15 5 new和delete运算符 动态地分配和撤销内存空间 16 回顾 变量的存储区 inti 1 voidf inta cout i endl cout a endl intmain intj 2 intk 3 cout k endl f j return0 静态存储区 动态存储区 i 1 j 2 k 3 a 2 17 修正 C 的4个内存区间 inti 1 intmain intj 2 int p NULL p newint 10 if NULL p cout p 正确 输出10deletep cout p 错误return0 静态存储区 动态存储区堆 heap i 1 j 2 p NULL 代码区 栈Stack 未命名变量 10 18 举例7 6 P218 include includeusingnamespacestd structStudent stringname intnum charsex intmain Student p NULL p newStudent if NULL p p name WangFun p num 10123 p sex m coutnamenumsex endl deletep p NULL return0 19 举例7 1 4 接续例7 1 3 动态链表结构 structStudent intID stringname stringsex floatscore Student pNext intmain Student pHead NULL p coutpNext pHead pHead p cin pHead ID pHead name pHe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论