C课件110章节.5类与对象

上传人:E**** 文档编号:91276298 上传时间:2019-06-27 格式:PPT 页数:528 大小:4.47MB
返回 下载 相关 举报
C课件110章节.5类与对象_第1页
第1页 / 共528页
C课件110章节.5类与对象_第2页
第2页 / 共528页
C课件110章节.5类与对象_第3页
第3页 / 共528页
C课件110章节.5类与对象_第4页
第4页 / 共528页
C课件110章节.5类与对象_第5页
第5页 / 共528页
点击查看更多>>
资源描述

《C课件110章节.5类与对象》由会员分享,可在线阅读,更多相关《C课件110章节.5类与对象(528页珍藏版)》请在金锄头文库上搜索。

1、第5章 类与对象,5.1 结构,5.2 类与对象,5.3 构造函数和析构函数,5.4 静态成员,5.5 友员,小结,类(Class)是面向对象程序设计(OOP)实现信息封装的基础。 类是用户定义类型,也称为类类型 每个类包含数据说明和一组操作数据或传递消息的函数。类的 实例称为对象 C+的类由C语言的结构类型演变而来。本章首先介绍结构类型, 然后讨论C+的类和对象,第5章 类与对象,结构由数目固定的成员构成 各成员可以具有不同的数据类型 一个结构变量在内存占有一片连续的存储空间,5.1 结构,结构类型定义形式为: struct 标识符 类型 成员1 ; 类型 成员2 ; 类型 成员n ; ;,

2、5.1.1 定义结构和访问结构,1定义结构,例: struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;,5.1.1 定义结构和访问结构,1定义结构,例: struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;,可以用不同方法定义一个结构变量,(1) 声明类型之后声明变量,employee worker1, worker2, *Emp

3、 ;,5.1.1 定义结构和访问结构,1定义结构,例: struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;,可以用不同方法定义一个结构变量,(1) 声明类型之后声明变量,worker1, worker2, *Emp ;,(2) 声明类型的同时声明变量,5.1.1 定义结构和访问结构,1定义结构,例: struct employee char name 10 ; long code ; float salary ; char address 50 ; char

4、phone 20 ; ;,可以用不同方法定义一个结构变量,(1) 声明类型之后声明变量,worker1, worker2, *Emp ;,(2) 声明类型的同时声明变量,(3) 直接声明结构类型变量,注意 此时没有了结构类型标识符,5.1.1 定义结构和访问结构,1定义结构,例: struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 20 ; ;,employee worker1, worker2, *Emp = ,说明,(1) 结构变量占有一片连续内存 空间,具有结构类型的

5、特征,5.1.1 定义结构和访问结构,1定义结构,说明,(2) 一个结构类型的成员 可以是另一个已定义的结构类型,struct date int month ; int day ; int year ; ; struct employee char name 10 ; date birthday ; long code ; float salary ; char address 50 ; char phone 11 ; worker1, worker2 ;,例如: 为职工结构添加出生日期信息 类型和变量声明为:,son person ;,5.1.1 定义结构和访问结构,1定义结构,说明,(2)

6、 一个结构类型的成员 可以是另一个已定义的结构类型,struct person char name 10 ; long code ; float salary ; char address 50 ; char phone 11 ; worker1, worker2 ;,错误 不能实现的无穷递归结构,5.1.1 定义结构和访问结构,1定义结构,说明,(3) 声明结构类型变量可以同时初始化,struct employee char name 10 ; long code ; float salary ; char address 50 ; char phone 11 ; worker = “Wan

7、g Li “ , 991083456, 1200.5, “guang zhou “ , “ 87111111 “ ;,2访问结构,(1)访问结构变量的成员 结构变量 . 成员,点运算符,# include struct weather / 声明结构类型 double temp; double wind; ; void main ( ) weather today ; / 声明结构类型变量 today . temp = 10.5 ; / 对结构变量成员赋值 today . wind = 3.1 ; cout “Temp = ” today . temp endl ; / 按成员输出 cout “

8、Wind = ” today . wind endl ; ,访问结构变量成员,5.1.1 定义结构和访问结构,2访问结构,(2)用指针访问结构变量的成员 结构指针 - 成员 (*结构指针 ) . 成员,# include # include struct person char name20 ; unsigned long id; float salary; ; void main ( ) person pr1 ; person * pp ; / 定义结构指针 pp = ,person * pp ;,pp = ,pp - name,pp - id,pp - salary,5.1.1 定义结构和

9、访问结构,2访问结构,(2)用指针访问结构变量的成员 结构指针 - 成员 (*结构指针 ) . 成员,# include # include struct person char name20 ; unsigned long id; float salary; ; void main ( ) person pr1 ; person * pp ; / 定义结构指针 pp = ,(*pp).name,(*pp). id,(*pp). salary,5.1.1 定义结构和访问结构,2访问结构,# include struct weather double temp; double wind; yes

10、terday ; void main ( ) weather today ; yesterday . temp = 10.5 ; yesterday . wind = 3.1 ; today = yesterday ; / 结构变量整体赋值 cout “Temp = ” today . temp endl ; cout “Wind = ” today . wind endl ; ,(3)类型相同的结构变量可以整体赋值,5.1.1 定义结构和访问结构,2访问结构,(3)类型相同的结构变量可以整体赋值,例如: struct weather1 double temp; double wind; ye

11、sterday ; struct weather2 double temp; double wind; today ;,“类型相同的变量” 是指用同一类型标识符说明的变量,yesterday 和 today 尽管成员相同,但不是同类型变量 不可以整体赋值,5.1.1 定义结构和访问结构,# include # include struct person / 结构定义 char name10 ; unsigned int id; float salary ; ; person allone6 ; / 结构数组声明 void main ( ) int i ; person temp ; / 结构变

12、量声明 for ( i = 0 ; i allonei.id ; cout allonei.salary ; cout allonej+1.salary ) / 结构变量的整体交换 temp = allonej ; allonej = allonej+1 ; allonej+1 = temp ; for ( i = 0 ; i 6 ; i + ) / 输出排序后数据 cout allonei.name t allonei.id t allonei.salary endl ; ,例5-1 对结构数组以某一成员作关键字排序,5.1.1 定义结构和访问结构,# include # include s

13、truct person / 结构定义 char name10 ; unsigned int id; float salary ; ; person allone6 ; / 结构数组声明 void main ( ) int i ; person temp ; / 结构变量声明 for ( i = 0 ; i allonei.id ; cout allonei.salary ; cout allonej+1.salary ) / 结构变量的整体交换 temp = allonej ; allonej = allonej+1 ; allonej+1 = temp ; for ( i = 0 ; i

14、6 ; i + ) / 输出排序后数据 cout allonei.name t allonei.id t allonei.salary endl ; ,例5-1 对结构数组以某一成员作关键字排序,struct person / 结构定义 char name10 ; unsigned int id; float salary ; ; person allone6 ; / 结构数组声明,5.1.1 定义结构和访问结构,# include # include struct person / 结构定义 char name10 ; unsigned int id; float salary ; ; pe

15、rson allone6 ; / 结构数组声明 void main ( ) int i ; person temp ; / 结构变量声明 for ( i = 0 ; i allonei.id ; cout allonei.salary ; cout allonej+1.salary ) / 结构变量的整体交换 temp = allonej ; allonej = allonej+1 ; allonej+1 = temp ; for ( i = 0 ; i 6 ; i + ) / 输出排序后数据 cout allonei.name t allonei.id t allonei.salary endl ; ,例5-1 对结构数组以某一成员作关键字排序,void main ( ) int i ; person temp ; / 结构变量声明 for ( i =

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号