版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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 Polymo
2、rphism 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/
3、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 des
4、ign 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 Pr
5、ogramming (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(类) 是对一
6、组类似事物的共同属性和行为特征的抽象描述. 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 method
7、s(方法).,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,
8、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
9、 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(抽象数
10、据类型,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
11、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
12、 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 exten
13、d(扩展) 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(代码重用).,Infor
14、mationDialog,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,
15、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 fo
16、r 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)也不足以组织一个程序中的概念,因为有些概念看上去是固有地相互依赖着。这时,我们
17、应当试着将这样的环形依赖限制在程序的局部,以免使它们影响整个程序的结构。 化解依赖图最好的工具之一,是明确地分离接口与实现,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
18、 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/44,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年唐山幼儿师范高等专科学校单招职业适应性考试题库及答案详解(夺冠)
- 2026年度重点人群健康管理服务提升工作实施方案
- 客户合同续约请求回复函(3篇)范文
- 数据挖掘企业数据分析方案手册
- 文明礼仪自律承诺书范文3篇
- 2026年吕梁师范高等专科学校单招综合素质考试题库及一套参考答案详解
- 2026秋招:荣耀题库及答案
- 2026秋招:迈瑞医疗题库及答案
- 2026秋招:精算师试题及答案
- 2026秋招:健康管理师试题及答案
- 数字经济概论 课件全套 第1-16章 数字经济概览 -数字经济反垄断监管
- 三违行为清单
- 装置护栏围栏爬梯安全色要求及涂刷标准
- 互联网+大赛路演PPT制作
- 黑龙江省义务教育学校标准化建设
- 手动变速器检修课件
- 导游基础知识(中职)全套PPT教学课件
- 文化人类学完整版
- GB/T 14692-2008技术制图投影法
- 六年级上册数学试题 - 分数乘除章节测试 苏教版(图片版)无答案
- 公共营养师考试试题(含答案)
评论
0/150
提交评论