版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、OOP-C+0defining and using new types. object-oriented programminggeneric programmingPart II. Abstraction MechanismsOOP-C+110 Classes 10.1 Introduction类概述类概述 10.2 Classes类中的主要元素:成员函数、访问控制(能见度)、实例生类中的主要元素:成员函数、访问控制(能见度)、实例生成函数、静态数据成员、常量成员函数、对象自身引用等成函数、静态数据成员、常量成员函数、对象自身引用等 10.3 Efficient User Defined T
2、ypes如何使得自定义类型被定义得更便于理解和使用如何使得自定义类型被定义得更便于理解和使用 10.4 Objects对象消除、生成、操作、属性定义、属性值初始化及其他对象消除、生成、操作、属性定义、属性值初始化及其他OOP-C+2 10.1 Introduction类概述类概述 10.2 Classes类中的主要元素:成员函数、访问控制(能见度)、实例生类中的主要元素:成员函数、访问控制(能见度)、实例生成函数、静态数据成员、常量成员函数、对象自身引用等成函数、静态数据成员、常量成员函数、对象自身引用等 10.3 Efficient User Defined Types如何使得自定义类型被定
3、义得更便于理解和使用如何使得自定义类型被定义得更便于理解和使用 10.4 Objects对象消除、生成、操作、属性定义、属性值初始化及其他对象消除、生成、操作、属性定义、属性值初始化及其他10 ClassesOOP-C+310.1 Introduction A type is a concrete representation of a concept.一个类型是(应用领域中)某个概念(在程序中)的具类型是(应用领域中)某个概念(在程序中)的具体表示。体表示。p例如,例如,C+的基本类型是关于逻辑运算、数值运算、字符处理的基本类型是关于逻辑运算、数值运算、字符处理等概念的具体表示等概念的具体表
4、示。pOO理念:理念:We design a new type to provide a definition of a concept that has no direct counterpart among the built-in types. 对于用基本类型无法直接表示的概念,我们可以定义一个新的对于用基本类型无法直接表示的概念,我们可以定义一个新的类型。类型。OOP-C+410.1 Introduction问题问题:为什么要:为什么要将概念定义为类型将概念定义为类型,而不是其他形式的,而不是其他形式的程序实体(如程序实体(如 数据结构或函数)?数据结构或函数)?p类型表示法更加易于理
5、解;类型表示法更加易于理解;p类型通过其接口隐藏了内部实现,使用起来更加简单;程序类型通过其接口隐藏了内部实现,使用起来更加简单;程序更加简明、清晰,更加易于维护;更加简明、清晰,更加易于维护;p根据类型定义,编译程序能够检查出程序中的非法使用,而根据类型定义,编译程序能够检查出程序中的非法使用,而不是直到测试时才能发现。不是直到测试时才能发现。OOP-C+510.1 Introduction The fundamental idea in defining a new type is to separate the incidental(伴随而来的伴随而来的)details of the i
6、mplementation ( e.g., the layout(布局布局)of the data used to store an object of the type ) from the properties(特性特性)essential to the correct use of it ( e.g., the complete list of functions that can access the data ). Such a separation is best expressed by channeling(导向导向)all uses of the data structure
7、 and internal housekeeping routines(内部杂务例程内部杂务例程)through a specific interface.如何定义类型?OOP-C+610.1 Introduction 在一般程序员看来,使用基本类型时看到的只有在一般程序员看来,使用基本类型时看到的只有类型名类型名和和 一组操一组操作作的声明(包括操作名、参数、涵义、使用规则)。的声明(包括操作名、参数、涵义、使用规则)。例如,对于基本类型例如,对于基本类型 double, 程序员看到并能够使用的操作:程序员看到并能够使用的操作:、*、/、=、=、+=、-=、*=、/= 等。等。看不到(也没有
8、必要看到)看不到(也没有必要看到)的内容:的内容:double 变量的数据结构,及以上操作的实现。变量的数据结构,及以上操作的实现。如何定义类型?OOP-C+710.1 Introduction 这样的自定义类型就是这样的自定义类型就是类(类(Class)。 基本类型的实例称为变量,类的实例基本类型的实例称为变量,类的实例称为称为对象对象(Object)。 OOPL 使得使得 class 的表现和基本类型的表现十分相似,两者只的表现和基本类型的表现十分相似,两者只是在实例生成上有所差异。是在实例生成上有所差异。 在一般程序员看来,使用自定义类型时应当与使用基本类型没有本在一般程序员看来,使用自
9、定义类型时应当与使用基本类型没有本质区别看到的只有质区别看到的只有类型名类型名和和 一组操作一组操作的声明(包括操作名、的声明(包括操作名、参数、涵义、使用规则)。参数、涵义、使用规则)。如何定义类型?OOP-C+8本节内容本节内容 10.1 Introduction类概述类概述 10.2 Classes类中的主要元素:成员函数、访问控制(能见度)、实例生类中的主要元素:成员函数、访问控制(能见度)、实例生成函数、静态数据成员、常量成员函数、对象自身引用等成函数、静态数据成员、常量成员函数、对象自身引用等 10.3 Efficient User Defined Types如何使得自定义类型被定
10、义得更便于理解和使用如何使得自定义类型被定义得更便于理解和使用 10.4 Objects对象消除、生成、操作、属性定义、属性值初始化及其他对象消除、生成、操作、属性定义、属性值初始化及其他OOP-C+9考虑:如何表示考虑:如何表示“日期日期”这一概念?这一概念?10.2 Classes Member Functionsstruct Date struct Date int d, m, y; int d, m, y;void init_date( Date& d, void init_date( Date& d, int dd, int mm, int yy); int dd, int mm,
11、int yy);void add_year( Date& d, int n);void add_year( Date& d, int n);void add_month( Date& d, int n);void add_month( Date& d, int n);void add_day( Date& d, int n);void add_day( Date& d, int n);?struct Date struct Date int d, m, y; int d, m, y; void init( int dd, int mm, void init( int dd, int mm, i
12、nt yy ); int yy ); void add_year( int n ); void add_year( int n ); void add_month( int n ); void add_month( int n ); void add_day( int n ); void add_day( int n );OOP-C+10struct Date struct Date int d, m, y; int d, m, y; void init( int dd, int mm, int yy ); void init( int dd, int mm, int yy ); void a
13、dd_year( int n ); void add_year( int n ); void add_month( int n ); void add_month( int n ); void add_day( int n ); void add_day( int n );10.2 Classes Member FunctionsMember functionData Member#include #include Date.hDate.hvoid Date:init(int dd, int mm, int yy)void Date:init(int dd, int mm, int yy) d
14、 = dd; d = dd; m = mm; m = mm; y = yy; y = yy; / /* * . . * */ /#include #include Date.hDate.hDate my_birthdayDate my_birthday; ;void f()void f() Date todayDate today; ; today.init(29,11,2006); today.init(29,11,2006); my_birthday.init(30,12,1950); my_birthday.init(30,12,1950); qualification每个对象均有一份每
15、个对象均有一份该类的所有对象共享该类的所有对象共享OOP-C+1110.2 Classes Member Functions 类的值集:通过数据成员的值集构造类的值集:通过数据成员的值集构造 VDate = Vd Vm Vy 操作集:全部成员函数构成了这个类的操作集。操作集:全部成员函数构成了这个类的操作集。struct Date struct Date int d, m, y; int d, m, y; void init( int dd, int mm, int yy ); void init( int dd, int mm, int yy ); void add_year( int n
16、); void add_year( int n ); void add_month( int n ); void add_month( int n ); void add_day( int n ); void add_day( int n );Data MemberMember function每个对象均有一份每个对象均有一份该类的所有对象共享该类的所有对象共享OOP-C+1210.2 Classes Access Controlvoid void jokingjoking(Date& d)(Date& d) d.m d.m * * = 20;= 20; / Date.h/ Date.hstr
17、uct Date struct Date int d, m, y; int d, m, y; void init(int dd, int mm, int yy); void init(int dd, int mm, int yy); void add_year(int n); void add_year(int n); void add_month(int n); void add_month(int n); void add_day(int n); void add_day(int n);private:private:#include #include Date.hDate.hvoid v
18、oid Date:initDate:init(int dd, int mm, int yy)(int dd, int mm, int yy) d = dd; d = dd; m = mm; m = mm; y = yy; y = yy; / /* * . . * */ /Access control: public interface to userAccess control: Can be used ONLY by member functionspublic:public:classclassOOP-C+1310.2 Classes Access Control/ Date.h/ Dat
19、e.hclassclass Date Date int d, m, y; int d, m, y;public:public: void init(int dd, int mm, int yy); void init(int dd, int mm, int yy); void add_year(int n); void add_year(int n); void add_month(int n); void add_month(int n); void add_day(int n); void add_day(int n);/ Date.h/ Date.hstructstruct Date D
20、ate int d, m, y; int d, m, y;public:public: void init(int dd, int mm, int yy); void init(int dd, int mm, int yy); void add_year(int n); void add_year(int n); void add_month(int n); void add_month(int n); void add_day(int n); void add_day(int n);缺省为缺省为 private缺省为缺省为publicOOP-C+1410.2 Classes Access C
21、ontrol 访问控制程度也称为访问控制程度也称为“能见度能见度”(Visibility)(Visibility)。 施加访问控制的目的施加访问控制的目的: :p全部全部 public public 成员函数的声明组成类的接口,它们确实构成成员函数的声明组成类的接口,它们确实构成了其操作集了其操作集; ;p使得类的用户只需了解类的对外接口就可以使用;使得类的用户只需了解类的对外接口就可以使用;p上述接口将其数据成员和成员函数的实现封装起来,以减少上述接口将其数据成员和成员函数的实现封装起来,以减少其实现的修改对外部的影响其实现的修改对外部的影响; ;p易于进行出错定位。易于进行出错定位。OOP
22、-C+1510.2 Classes Constructors 为避免自定义类型实例未初始化就使用,较好的办法是定义为避免自定义类型实例未初始化就使用,较好的办法是定义一个一个成员函数成员函数,它,它: (1)明确地对实例进行明确地对实例进行初始化初始化,(2) 并在生并在生成实例时被成实例时被自动自动调用。调用。 这个函数就是这个函数就是构造函数构造函数(constructor)。#include #include Date.hDate.hvoid f()void f() Date today; Date today; Date tomorrow = today; Date tomorrow
23、= today; tomorrow.add_day(1); tomorrow.add_day(1); today.init(29,11,2006);today.init(29,11,2006);?/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: void init(int dd, int mm, int yy); void init(int dd, int mm, int yy); void add_year(int n); void add_year(int n); void add_m
24、onth(int n); void add_month(int n); void add_day(int n); void add_day(int n);OOP-C+1610.2 Classes Constructors/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: void init(int dd, int mm, int yy); void init(int dd, int mm, int yy); void add_year(int n); void add_year(int n)
25、; void add_month(int n); void add_month(int n); void add_day(int n); void add_day(int n); Date (int dd, int mm, int yy);(int dd, int mm, int yy);构造函数构造函数与类同名与类同名构造函数不许指定返回值及类型/ Date.cpp/ Date.cpp#include “Date.h”#include “Date.h”Date:Date(int dd, int mm, int yy)Date:Date(int dd, int mm, int yy) d =
26、dd; d = dd; m = mm; m = mm; y = yy; y = yy; / /* * . . . . * */ /#include Date.h#include Date.hvoid f()void f() Date today = Date( 29,11,2006 ); Date today = Date( 29,11,2006 ); Date this_day( 12,12,2001 ); Date this_day( 12,12,2001 ); Date now; Date now; Date current_year( 12,12 ); Date current_yea
27、r( 12,12 ); / /* * . .* */ / OOP-C+1710.2 Classes Constructors 一个类中可以根据需要定义多个一个类中可以根据需要定义多个 constructors(函数名过载),编(函数名过载),编译程序根据调用时实参的数目、类型和顺序自动找到与之匹配者。译程序根据调用时实参的数目、类型和顺序自动找到与之匹配者。/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: Date(int dd, int mm, int yy); Date(int dd,
28、 int mm, int yy);Date(int dd, int mm); / Date(int dd, int mm); / todays yearDate(int dd); / Date(int dd); / todays month and yearDate();Date(); / / default Date: todayDate(const charDate(const char* * p); / p); / date in string #include #include Date.hDate.hvoid f()void f() Date today( 29 ); Date to
29、day( 29 ); Date july4(“July 4,1983“); Date july4(“July 4,1983“); Date guy(“5 Nov“); Date guy(“5 Nov“); Date now; Date now; Date tYear(25,12); Date tYear(25,12); OOP-C+18/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: Date(int dd, int mm, int yy); Date(int dd, int mm, in
30、t yy); Date(int dd, int mm); Date(int dd, int mm); / / todays year Date(int dd); Date(int dd); / / todays month and year Date(); Date();/ / default Date: today Date(const char Date(const char* * p); p);/ / date in string ;10.2 Classes Constructors 多个构造函数多个构造函数default argumentsDate(int dd, int mm, in
31、t yy =0);Date(int dd, int mm, int yy =0);Date(int dd, int mm =0, int yy =0);Date(int dd, int mm =0, int yy =0);Date(int dd =0, int mm =0, int yy =0);Date(int dd =0, int mm =0, int yy =0);最终的构造函数/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: Date(int dd=0, int mm=0, int
32、 yy=0); Date(int dd=0, int mm=0, int yy=0); Date(const char Date(const char* * p); p);/ / date in string ;OOP-C+19/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: Date(int dd, int mm, int yy); Date(int dd, int mm, int yy); Date(int dd, int mm); Date(int dd, int mm); / /
33、todays year Date(int dd); Date(int dd); / / todays month and year Date(); Date();/ / default Date: today Date(const char Date(const char* * p); p);/ / date in string ;10.2 Classes Constructors 多个构造函数多个构造函数default arguments/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public:
34、Date(int dd=0, int mm=0, int yy=0); Date(int dd=0, int mm=0, int yy=0); Date(const char Date(const char* * p); p);/ / date in string ;#include Date.h#include Date.hextern int getMachineDay();extern int getMachineDay();extern int getMachineMonth();extern int getMachineMonth();extern int getMachineYea
35、r();extern int getMachineYear();Date:Date(int dd, int mm, int yy)Date:Date(int dd, int mm, int yy) d = dd; m = mm; y = yy; d = dd; m = mm; y = yy; if( dd = 0 ) d = getMachineDay(); if( dd = 0 ) d = getMachineDay(); if( mm = 0 ) m = getMachineMonth(); if( mm = 0 ) m = getMachineMonth(); if( yy = 0 )
36、y = getMachineYear(); if( yy = 0 ) y = getMachineYear(); / check that d,m,y are valid / check that d,m,y are valid OOP-C+2010.2 Classes Static Members 问题:对于类问题:对于类Date,如何统计(到目前为止)曾经生成的实例,如何统计(到目前为止)曾经生成的实例(对对象象) 数目?数目?/ Date.h/ Date.hclass Date class Date int d, m, y; int d, m, y;public:public: Date
37、(int dd=0, int mm=0, int yy=0); Date(int dd=0, int mm=0, int yy=0);/ user.cpp/ user.cppDate today;Date today;Date Date * *p;p;void f ( )void f ( ) Date temp; Date temp; p = new Date; p = new Date; 分析:需要设置一个计数器,需要设置一个计数器, 每生成一个每生成一个 Date 的实例时,该计数器增的实例时,该计数器增 1。如何设置?如何设置?OOP-C+2110.2 Classes Static Me
38、mbers思路1:设立一个全局变量,设立一个全局变量, 每当产生一个对象时自动加每当产生一个对象时自动加1 1。缺点: 非类型化:非类型化:n_Daten_Date不属于类不属于类 DateDate; 可被其他模块任意篡改!可被其他模块任意篡改!int n_Date = 0; / # of Datesint n_Date = 0; / # of Dates/ /* * * */ /Date:Date(int dd, int mm, int yy)Date:Date(int dd, int mm, int yy) / /* * * */ / n_Date +; n_Date +; 问题:对于类问
39、题:对于类Date,如何统计(到目前为止)曾经生成的实例,如何统计(到目前为止)曾经生成的实例(对对象象) 数目?数目?OOP-C+2210.2 Classes Static Members思路2:类中设立一个数据成员做类中设立一个数据成员做为计数器,为计数器, 每当产生一个对象每当产生一个对象时自动加时自动加1 1。缺点:无法设置该计数器的初始值;无法设置该计数器的初始值;多份计数器每个实例拥有自己的数据成员;多份计数器每个实例拥有自己的数据成员;计数器随着实例的消亡而消亡。计数器随着实例的消亡而消亡。 int n_Date; / # of Dates int n_Date; / # of
40、Dates / /* * * */ / Date(int dd, int mm, int yy) Date(int dd, int mm, int yy) / /* * * */ / n_Date +; n_Date +; class Date class Date / end of class / end of class 问题:对于类问题:对于类Date,如何统计(到目前为止)曾经生成的实例,如何统计(到目前为止)曾经生成的实例(对对象象) 数目?数目?OOP-C+2310.2 Classes Static Members该问题提出了一种对语言机制的支持要求:该问题提出了一种对语言机制的支
41、持要求:需要有允许一个类的需要有允许一个类的所有对象共享所有对象共享的数据,而且这样的数据以的数据,而且这样的数据以数据成员数据成员的方式存在的方式存在。C+中允许类有中允许类有静态数据成员:如果一个数据是一个类的所有如果一个数据是一个类的所有实例实例都需要都需要的,而且这个数据值的变化对于这个类的所有实例的,而且这个数据值的变化对于这个类的所有实例又始终应当是又始终应当是统一统一的,就应当把这个数据定义成静态数据成员。的,就应当把这个数据定义成静态数据成员。 问题:对于类问题:对于类Date,如何统计(到目前为止)曾经生成的实例,如何统计(到目前为止)曾经生成的实例(对对象象) 数目?数目?
42、OOP-C+2410.2 Classes Static Members 静态数据成员静态数据成员 的特点的特点: 类的所有实例类的所有实例共享共享该成员;该成员; 与类的实例个数无关;与类的实例个数无关; 可以不通过类的任何实例访问。可以不通过类的任何实例访问。class Date class Date staticstatic int n_Date ; / # of Dates int n_Date ; / # of Dates / /* * * */ / Date:Date(int dd, int mm, int yy) Date:Date(int dd, int mm, int yy)
43、/ /* * * */ / n_Date +; n_Date +; / end of class / end of class 访问静态数据成员的方式:访问静态数据成员的方式:/ qualified by its class name/ qualified by its class nameint Date:n_Date = 0;int Date:n_Date = 0;void f( ) void f( ) Date d( 29,11,2006 ); Date d( 29,11,2006 ); d.d.n_Daten_Date = 10; / if its = 10; / if its pub
44、licpublic printf(“%d”, printf(“%d”, Date:Date:n_Date );n_Date ); 非定义声明非定义声明这是定义,这是定义,必须存在必须存在132(1) 通过对象访问;通过对象访问;(2) 通过类型名访问;通过类型名访问;(3) 类的方法中,类的方法中,还可以还可以直接访问。直接访问。OOP-C+2510.2 Classes Static Members 静态成员函数静态成员函数 只允许访问类中的静态数据成员、静态函数成员。只允许访问类中的静态数据成员、静态函数成员。class Date class Date staticstatic int n_
45、Date ; / # of Dates int n_Date ; / # of Dates / /* * * */ / Date:Date(int dd, int mm, int yy) Date:Date(int dd, int mm, int yy) / /* * * */ / n_Date +; n_Date +; staticstatic void InitCounter(int N ) void InitCounter(int N ) n_Date = N; n_Date = N; another_Static_method();another_Static_method(); / end of
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 哈姆雷特:人性与命运的永恒叩问
- 大模型推理性能优化工程师考试试卷及答案
- 2026年绿色工厂创建考试真题及答案
- 2026 高血压病人饮食的冬瓜粥课件
- 2026年宁夏回族自治区初二地生会考真题试卷+解析及答案
- 2025年安徽六安市初二地生会考考试真题及答案
- 江苏宿迁市地理生物会考真题试卷(+答案)
- 2026年湖北省咸宁市初二地理生物会考试卷题库及答案
- 2026九年级道德与法治上册 网络强国建设
- 2026一年级下新课标口算笔算结合训练
- 高二物理(人教版)试题 选择性必修一 模块综合检测(一)
- 电商客服话术技巧及常见问题
- 2026年体育与健康核心知识点梳理100道(含答案)
- 2024全国能源行业火力发电集控值班员理论知识技能竞赛题库附答案
- 学校占用居民协议书
- 通信登高作业安全培训
- 水厂防投毒应急预案(3篇)
- 银行招聘-中国农业银行计算机专业考试试题及答案
- 本质安全体系建设实施方案
- 2024年七年级生物下册 4.1.2人的生殖说课稿 (新版)新人教版
- 医院感染自查报告模板及整改记录
评论
0/150
提交评论