面向对象的程序设计(1).ppt_第1页
面向对象的程序设计(1).ppt_第2页
面向对象的程序设计(1).ppt_第3页
面向对象的程序设计(1).ppt_第4页
面向对象的程序设计(1).ppt_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

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

文档简介

C+面向对象程序设计 Object-Oriented Programming in C+,山东工商学院.计算机科学与技术学院 刘培强 ,2010-2011-01 电信08 计科09,1/44,Chap.1 Object-Oriented Programming,2/44,Content,1.1 Object-Oriented and Procedural Programming 1.2 Classes and Abstract Data Types 1.3 The Client/Server Model and Message Passing 1.4 Inheritance and Polymorphism 1.5 Interfaces and Components 1.6 Template,3/44,1.1 Object-Oriented and Procedural Programming,4/44,程序设计范型 ( Programming Paradigms/style ) 程序设计范型程序设计时所采用的基本方式模型。它决定了在程序设计时的思维方式及所采用的工具,它受应用领域的约束,以某一类程序设计语言为基础。 程序设计风格(Programming Styles)通常由思维方式和语言支持所决定,而不同的领域要求的思维方式不同,因而程序设计风格不同,对语言的支持要求也不同。,5/44,Procedural Programming(面向过程编程),Decide which procedures you want; (程序设计的核心) use the best algorithms you can find.(过程的内涵),以功能为中心,基于功能分解的程序设计范型,程序可以被看作为函数调用序列(图中没有画出三级函数),main program data,Procedure 1,Procedure 2,Procedure 3,6/44,Procedural Programming,Procedural programming is associated with a design technique knows as top-down design(自顶向下).,Build an automobile,Problem decomposition 问题分解,7/44,Procedural Programming,In top-down design, a problem is associated with a procedure.,void buildC() void inspect() int main() buildC(); buildE(); buildD(); assemble(); inspect(); ,8/44,Object-Oriented Programming (OOP),动物界,软体动物门,脊索动物门,爬行纲,哺乳纲,食肉目,灵长目,人科,人属,人种,界,门,纲,目,科,属,种,角龙科,林奈物种继承层次图,9/44,Object-Oriented Programming,Decide which classes you want; provide a full set of operations for each class; make commonality(共性) explicit by using inheritance(继承).,10/44,Object-Oriented Programming,Class(类) 是对一组类似事物的共同属性和行为特征的抽象描述. Objects(对象) in a class share properties, features, or attributes. In C+, these attributes are called data members(数据成员). A class has actions or processes associated with it. Objects in a class share these actions. In C+, these actions are called function members(成员函数) or methods(方法).,11/44,Object-Oriented Programming,Relationships(关系),Human,Animal,is a,Mary Leakey,instance of,12/44,1.2 Classes and Abstract Data Types,13/44,Information Hiding(信息隐蔽),The implementation is hidden from the user, whereas the interface is exposed(暴露) to the user. In an object-oriented language, a class is a module that supports information hiding. In a C+ class, we can use keywords such as public and private to control access (存取) to the classs properties and operations.,14/44,Encapsulation(封装),The standard mechanism for procedural programming of data manipulation (操作) is passing arguments to and returning a value from a function. In object-oriented programming, data and the procedures to manipulate the data can be encapsulated (封装) or contained within a class. A typical role of the classs encapsulated data members is to support its encapsulated methods.,15/44,Abstract Data Types(抽象数据类型,ADT),ADT: exposes public interface, hides implementation details. ADT: 值集 操作集,class RECT private: double top, left; double bottom, right; public: void init( double, double, double, double ); void draw(); void move( double x, double y); ;,优点: 模块化程度高 密封性可保证数据完整 实现部分可独立更换,16/44,Class(类),Class is a user-defined type(用户自定义类型). A type is a concrete representation of a concept in application domain(领域). We design a new type to provide a definition of a concept that has no direct counterpart(复本) among the built-in(内建、内置) types.,17/44,1.3 Client/Server Model,A client requests services from a Server object by invoking one of its methods, which is characterized as sending a message to the object.,Client,Server,GET http:/server/,C,S,in C: Server S; int ret = S.GET ( “http:/server/” );,18/44,1.4 Inheritance and Polymorphism,Inheritance is a convenient and efficient way to specialize (特化) or extend(扩展) a parent class by creating a child class.,Base class,Derived class,Inheritance,Super/Parent class,Subclass,is a,19/44,1.4 Inheritance and Polymorphism,Inheritance allows the designer to combine features of classes in a common parent class. Inheritance supports a form of code reuse(代码重用).,InformationDialog,MenuWindow,DialogWindow,EditWindow,QuestionDialog,Window,Inheritance hierarchy,20/44,1.4 Inheritance and Polymorphism(多态),single inheritance(单继承) : a child class has exactly one direct parent. multiple inheritance(多继承) : a child class may have multiple direct parents.,Manager,Temporary,Consultant,Worker,21/44,1.4 Inheritance and Polymorphism,Polymorphism: In an object-oriented language, there can be many methods with the same signature.,静态绑定: Ex. int i1 = 10 5; int add(int, int); double d2 = 10.2 5.0; double add(double, double);,22/44,1.4 Inheritance and Polymorphism,Its common for each class in the hierarchy to provide its own appropriate version of a polymorphic method. 动态绑定: Ex.,23/44,1.5 Interface and Components(接口和组件),A classs interface consists of what it exposes. Abstract base class(抽象类),24/44,有时,即使是用有向无环图(directed acyclic graph)也不足以组织一个程序中的概念,因为有些概念看上去是固有地相互依赖着。这时,我们应当试着将这样的环形依赖限制在程序的局部,以免使它们影响整个程序的结构。 化解依赖图最好的工具之一,是明确地分离接口与实现,C+的抽象类(Abstract Classes)就是这样的工具。,Abstract base class(抽象类),25/44,1.5 Interface and Components(接口和组件),C+ uses abstract base class(抽象类) to specify an interface as a collection of public, high-level methods.,class stdFile public: virtual void Open(const char* name, int mode) = 0; virtual void Close() = 0; virtual void Rewind() = 0; ;,26/44,1.5 Interface and Components(接口和组件),A software component is a pre-built part that can be combined with other such parts to build an application (应用程序). A component is placed in a container (容器), a software construct designed to integrate components by handling communications among them. A software component is typically delivered as a binary file and, therefore, its contents are hidden from the programmer who uses it.,27

温馨提示

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

评论

0/150

提交评论