Chapter 10- Data Abstraction and Object Orientation.ppt
《Chapter 10- Data Abstraction and Object Orientation.ppt》由会员分享,可在线阅读,更多相关《Chapter 10- Data Abstraction and Object Orientation.ppt(43页珍藏版)》请在麦多课文档分享上搜索。
1、1,Chapter 10: Data Abstraction and Object Orientation,Aaron Bloomfield CS 415 Fall 2005,2,Fundamental OO Concepts,Encapsulation Inheritance Dynamic Method Binding,3,Encapsulation,Encapsulation Encapsulation allows the programmer to group data and the subroutines that operate on them together in one
2、place, and to hide irrelevant details from the user.Information Hiding Making objects and algorithms invisible to portions of the system that do not need them.,4,Modules,If a module M exports a type T, the rest of the program can only pass T to subroutines exported from M. T is said to be an opaque
3、type.var Database : moduleexports (tuple with (:=, name)type tuple = recordvar name : packed array 180 of charend tuple What can the code outside the Database module do?,5,Module Changing,Body is ChangedPrivate Part of Header is ChangedPublic Part of Header is Changed,It is a change to the interface
4、 which will often require a user code change,May require user recompile, but never requires a change in user code.,Will never require a change or recompile of the user code,6,Classes can limit visibility,PrivateProtectedPublicPackage (in some languages, e.g. Java),Visible inside the classs methods,V
5、isible inside the methods of the class and its descendants.,Visible anywhere the class declaration is in scope,7,Derived class can restrict visibility,Private Protected and public members of base class are private in derived class. Protected Protected and public members of base class are protected i
6、n derived class. Public Protected and public members of base class are protected and public in derived class. Private members of base class arent visible in derived class.,8,Initialization and Finalization,9,Four Important Issues,Choosing a Constructor References and Values Execution Order Garbage C
7、ollection Weve seen that already,10,Choosing a Constructor,Object-Oriented Languages allow classes to have zero, one or more different constructors. Two ways to distinguish between constructors Different Names Different Number and Types of Arguements,11,Constructors,Eiffel code:class COMPLEXcreation
8、new_cartesian, new_polarnew_cartesian(x_val, y_va; : REAL) isnew_polar(rho, theta : REAL) is class mydata public:mydata(string data);mydata(int data);mydata();,12,References and Values,C+ vs. Java Java uses reference, C+ you can specify Reference Every object is created explicitly so it is easy to m
9、ake sure the correct constructor is called. More elegant, but requires allocation from heap and extra indirections on every access of the object. Value More efficient but harder to control initialization,13,Execution Order,If class B is derived from class A, A constructor is called before B construc
10、tor To get arguments to the A constructor, you must use an intializer listclass foo : bar .foo:foo (foo_params) : bar(bar_params) The part after the colon is a call to bars constructor,14,Destructors and Garbage Collection,When an object is destroyed, the destructor is called for the derived class f
11、irst, then the destructors of the base classes are called. Reverse order of derivation Destructors purpose is to return allocated space back to the heap Many languages provide automatic garbage collection Java, Smalltalk, Eiffel, etc.,15,Javas finalize() method,In Java, you can override the finalize
12、() method This allows code to be executed when the object is about to be deleted But you shouldnt extend the objects lifetime by doing this As the finalize() method is only called once per object,16,Dynamic Method Binding,17,Polymorphism,A derived class (D) has all the members of its base class (C)
13、Class D can be used anytime class C is expected. If class D does not hide any publicly visible members of C then D is a subtype of C. If class D is used in place of class C, this is a form of polymorphism.,18,Polymorphism Example,class person class student : public person class professor : public pe
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER10DATAABSTRACTIONANDOBJECTORIENTATIONPPT

链接地址:http://www.mydoc123.com/p-379514.html