Chapter 16- Classes and Data Abstraction.ppt
《Chapter 16- Classes and Data Abstraction.ppt》由会员分享,可在线阅读,更多相关《Chapter 16- Classes and Data Abstraction.ppt(68页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 16: Classes and Data Abstraction,Outline 16.1 Introduction 16.2 Implementing a Time Abstract Data Type with a Class 16.3 Class Scope and Accessing Class Members 16.4 Separating Interface from Implementation 16.5 Controlling Access to Members 16.6 Access Functions and Utility Functions 16.7 I
2、nitializing Class Objects: Constructors 16.8 Using Default Arguments with Constructors 16.9 Using Destructors 16.10 When Constructors and Destructors Are Called 16.11 Using Data Members and Member Functions 16.12 A Subtle Trap: Returning a Reference to a private Data Member 16.13 Assignment by Defau
3、lt Memberwise Copy 16.14 Software Reusability,Objectives,In this chapter, you will learn: To understand the software engineering concepts of encapsulation and data hiding. To understand the notions of data abstraction and abstract data types (ADTs). To be able to create C+ ADTs, namely classes. To u
4、nderstand how to create, use, and destroy class objects. To be able to control access to object data members and member functions. To begin to appreciate the value of object orientation.,16.1 Introduction,Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into
5、packages called classes Data and functions closely relatedInformation hiding Implementation details are hidden within the classes themselvesUnit of C+ programming: the class A class is like a blueprint reusable Objects are instantiated (created) from the class For example, a house is an instance of
6、a “blueprint class” C programmers concentrate on functions,16.2 Implementing a Time Abstract Data Type with a Class,Classes Model objects that have attributes (data members) and behaviors (member functions) Defined using keyword class,Public: and Private: are member-access specifiers.,setTime, print
7、Military, and printStandard are member functions. Time is the constructor.,hour, minute, and second are data members.,16.2 Implementing a Time Abstract Data Type with a Class (II),Format Body delineated with braces ( and ) Class definition terminates with a semicolonMember functions and data Public
8、- accessible wherever the program has access to an object of class TimePrivate - accessible only to member functions of the class Protected - discussed later in the course,16.2 Implementing a Time Abstract Data Type with a Class (III),Constructor Special member function that initializes data members
9、 of a class object Constructors cannot return values Same name as the class Definitions Once class defined, can be used as a data type,16.2 Implementing a Time Abstract Data Type with a Class (IV),Binary scope resolution operator (:) Specifies which class owns the member function Different classes c
10、an have the same name for member functionsFormat for definition class member functionsReturnType ClassName:MemberFunctionName( ),16.2 Implementing a Time Abstract Data Type with a Class (V),If member function is defined inside the class Scope resolution operator and class name are not needed Definin
11、g a function outside a class does not change it being public or privateClasses encourage software reuse Inheritance allows new classes to be derived from old onesIn following program Time constructor initializes the data members to 0 Ensures that the object is in a consistent state when it is create
12、d,fig16_02.cpp (Part 1 of 3),fig16_02.cpp (Part 2 of 3),fig16_02.cpp (Part 3 of 3),Program Output,The initial military time is 00:00 The initial standard time is 12:00:00 AMMilitary time after setTime is 13:27 Standard time after setTime is 1:27:06 PMAfter attempting invalid settings: Military time:
13、 00:00 Standard time: 12:00:00 AM,16.3 Class Scope and Accessing Class Members,Class scope Data members and member functions File scope Nonmember functions Function scope Variables defined in member functions, destroyed after function completes Inside a scope Members accessible by all member functio
14、ns Referenced by name,16.3 Class Scope and Accessing Class Members (II),Outside a scope Use handles An object name, a reference to an object or a pointer to an object Accessing class members Same as structs Dot (.) for objects and arrow (-) for pointers Example: t.hour is the hour element of t TimeP
15、tr-hour is the hour element,fig16_03.cpp (Part 1 of 2),fig16_03.cpp (Part 2 of 2) Program Output,Assign 7 to x and print using the objects name: 7 Assign 8 to x and print using a reference: 8 Assign 10 to x and print using a pointer: 10,16.4 Separating Interface from Implementation,Separating interf
16、ace from implementation Easier to modify programs C+ programs can be split intoHeader files contains class definitions and function prototypesSource-code files contains member function definitionsProgram Outline: Using the same Time class as before, create a header file Create a source code file Loa
17、d the header file to get the class definitions Define the member functions of the class,time1.h,time1.cpp (Part 1 of 2),time1.cpp (Part 2 of 2),fig16_04.cpp (Part 1 of 2),fig16_04.cpp (Part 2 of 2) Program Output,The initial military time is 00:00 The initial standard time is 12:00:00 AMMilitary tim
18、e after setTime is 13:27 Standard time after setTime is 1:27:06 PMAfter attempting invalid settings: Military time: 00:00 Standard time: 12:00:00 AM,16.5 Controlling Access to Members,Purpose of public Give clients a view of the services the class provides (interface) Purpose of private Default sett
19、ing Hide details of how the class accomplishes its tasks (implementation) Private members only accessible through the public interface using public member functions,fig16_05.cpp,Program Output,Borland C+ command-line compiler error messages,Time1.cpp: Fig16_05.cpp: Error E2247 Fig16_05.cpp 15: Time:
20、hour is not accessible in function main() Error E2247 Fig16_05.cpp 18: Time:minute is not accessible in function main()* 2 errors in Compile *,Microsoft Visual C+ compiler error messages,Compiling. Fig16_05.cpp D:Fig16_05.cpp(15) : error C2248: hour : cannot access private member declared in class T
21、ime D:Fig16_05time1.h(18) : see declaration of hour D:Fig16_05.cpp(18) : error C2248: minute : cannot access private member declared in class Time D:time1.h(19) : see declaration of minute Error executing cl.exe.test.exe - 2 error(s), 0 warning(s),16.6 Access Functions and Utility Functions,Utility
22、functions private functions that support the operation of public functions Not intended to be used directly by clients Access functions public functions that read/display data or check conditions For a container, it could call the isEmpty function Next Program to take in monthly sales and output the
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER16CLASSESANDDATAABSTRACTIONPPT
