《面向对象的程序设计.ppt》由会员分享,可在线阅读,更多相关《面向对象的程序设计.ppt(30页珍藏版)》请在金锄头文库上搜索。
1、C+面向对象程序设计面向对象程序设计Object-Oriented Programming in C+山东工商学院山东工商学院. .计算机科学与技术学院计算机科学与技术学院刘培强刘培强http:/ http:/ 2010-2011-01 电信08 计科09Chap.1 Object-Oriented Programming1Contentn1.1 Object-Oriented and Procedural Programmingn1.2 Classes and Abstract Data Typesn1.3 The Client/Server Model and Message Passin
2、gn1.4 Inheritance and Polymorphismn1.5 Interfaces and Componentsn1.6 Template21.1 Object-Oriented and Procedural Programming3程序设计范型程序设计范型 ( Programming Paradigms/style )( Programming Paradigms/style ) 程序设计范型程序设计范型程序设计时所采用的基本方式模型程序设计时所采用的基本方式模型。它决。它决定了在程序设计时的思维方式及所采用的工具,它受应用领定了在程序设计时的思维方式及所采用的工具,它受应用
3、领域的约束,以某一类程序设计语言为基础域的约束,以某一类程序设计语言为基础。 程序设计风格程序设计风格(Programming StylesProgramming Styles)通常由思维方式和)通常由思维方式和语言支持所决定,而不同的领域要求的思维方式不同,因而语言支持所决定,而不同的领域要求的思维方式不同,因而程序设计风格不同,对语言的支持要求也不同。程序设计风格不同,对语言的支持要求也不同。4Procedural Programming(面向过程编程)Decide which procedures you want; (程序设计的核心)use the best algorithms yo
4、u can find.(过程的内涵)以功能为中心,基于功能分解的程序设计范型程序可以被看作为函数调用序列(图中没有画出三级函数)main programdataProcedure 1Procedure 2Procedure 35Procedural ProgrammingnProcedural programming is associated with a design technique knows as top-down design(自顶向下)(自顶向下). .BuildChassisBuildEngineBuildDriveTrainAssembleInspectBuild an a
5、utomobileMainProblemProblem decomposition问题分解问题分解6Procedural ProgrammingnIn top-down design, a problem is associated with a procedure.BuildChassisBuildEngineBuildDriveTrainAssembleInspectMainProblembuildC()buildE()buildD() assemble() inspect()main()void void buildCbuildC() () void inspect() void ins
6、pect() int main()int main() buildCbuildC();(); buildEbuildE();(); buildDbuildD();(); assemble(); assemble(); inspect(); inspect(); 7Object-Oriented Programming (OOP)动物界软体动物门脊索动物门爬行纲哺乳纲食肉目灵长目人科人属人种界门纲目科属种角龙科林奈物种继承层次图8Object-Oriented ProgrammingDecide which classes you want;provide a full set of opera
7、tions for each class; make commonality(共性) explicit by using inheritance(继承).9Object-Oriented ProgrammingnClass( (类类) ) 是对一组类似事物的共同属性和行为特征的抽象是对一组类似事物的共同属性和行为特征的抽象描述描述.nObjects( (对象对象) ) in a class share properties, features, or attributes. In C+, these attributes are called data members( (数据成员数据成员)
8、).nA 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( (方法方法) ).10Object-Oriented ProgrammingnRelationships(关系)Human nose eyesworkhas aHuman Animal is aMary Leakeyinstance of111.2 Clas
9、ses and Abstract Data Types12Information Hiding(信息隐蔽)nThe implementation is hidden from the user, whereas the interface is exposed(暴露) to the user.nIn an object-oriented language, a class is a module that supports information hiding.nIn a C+ class, we can use keywords such as public and private to c
10、ontrol access (存取) to the classs properties and operations.13Encapsulation(封装)nThe standard mechanism for procedural programming of data manipulation (操作) is passing arguments to and returning a value from a function.nIn object-oriented programming, data and the procedures to manipulate the data can
11、 be encapsulated (封装) or contained within a class.nA typical role of the classs encapsulated data members is to support its encapsulated methods.14Abstract Data Types(抽象数据类型,ADT)nADT: exposes public interface, hides implementation details. nADT: 值集 操作集class RECT private: double top, left; double bot
12、tom, right; public: void init( double, double, double, double ); void draw(); void move( double x, double y);优点: 模块化程度高模块化程度高 密封性可保证数据完整密封性可保证数据完整 实现部分可独立更换实现部分可独立更换15Class(类)nClass is a user-defined type(用户自定义类型). A type is a concrete representation of a concept in application domain(领域).nWe design
13、 a new type to provide a definition of a concept that has no direct counterpart(复本) among the built-in(内建、内置) types.161.3 Client/Server ModelnA client requests services from a Server object by invoking one of its methods, which is characterized as sending a message to the object.ClientServerGET http
14、:/server/CSin C: Server S; int ret = S.GET ( “http:/server/” );171.4 Inheritance and PolymorphismnInheritance is a convenient and efficient way to specialize (特化特化) or extend(扩展展) a parent class by creating a child class.HumanAnimalBase classDerived classInheritanceSuper/Parent classSubclassis ais a
15、181.4 Inheritance and PolymorphismnInheritance allows the designer to combine features of classes in a common parent class.nInheritance supports a form of code reuse(代码重用).InformationDialogMenuWindowDialogWindowEditWindowQuestionDialogWindowInheritance hierarchy191.4 Inheritance and Polymorphism(多态)
16、nsingle inheritance( (单继承单继承) ) : a child class has exactly one direct parent.nmultiple inheritance( (多继承多继承) ) : a child class may have multiple direct parents.ManagerTemporary Consultant Worker201.4 Inheritance and PolymorphismnPolymorphism: In an object-oriented language, there can be many method
17、s with the same signature. 静态绑定: Ex. int i1 = 10 5; int add(int, int); double d2 = 10.2 5.0; double add(double, double);211.4 Inheritance and PolymorphismnIts common for each class in the hierarchy to provide its own appropriate version of a polymorphic method.动态绑定:动态绑定:Ex. 221.5 Interface and Compo
18、nents(接口和组件)nA classs interface consists of what it exposes.n Abstract base class(抽象类)23n有有时时,即即使使是是用用有有向向无无环环图图(directed acyclic graph)也也不不足足以以组组织织一一个个程程序序中中的的概概念念,因因为为有有些些概概念念看看上上去去是是固固有有地地相相互互依依赖赖着着。这这时时,我我们们应应当当试试着着将将这这样样的的环环形形依依赖赖限限制制在在程程序序的的局局部部,以以免免使使它它们们影影响响整整个个程程序序的的结结构构。 化化解解依依赖赖图图最最好好的的工工
19、具具之之一一,是是明明确确地地分分离离接接口口与与实实现现,C+的的抽抽象象类类(Abstract Classes)就就是是这这样的工具。样的工具。 例:例:母鸡母鸡鸡蛋鸡蛋母鸡是鸡蛋母鸡是鸡蛋孵化出来的孵化出来的鸡蛋是鸡蛋是母鸡产的母鸡产的Abstract base class(抽象类)241.5 Interface and Components(接口和组件)nC+ uses abstract base class(抽象类) to specify an interface as a collection of public, high-level methods.class class st
20、dFilestdFile public: public: virtual virtual void void Open(constOpen(const char* name, int mode) char* name, int mode) = 0= 0; ; virtualvirtual void Close() void Close() = 0= 0; ; virtualvirtual void Rewind() void Rewind() = 0= 0; ;251.5 Interface and Components(接口和组件)nA software component is a pre
21、-built part that can be combined with other such parts to build an application (应用程序).nA component is placed in a container (容器), a software construct designed to integrate components by handling communications among them.nA software component is typically delivered as a binary file and, therefore,
22、its contents are hidden from the programmer who uses it.261.6 Template (模板)n表现概念之间共性的另表现概念之间共性的另一种方式是模板一种方式是模板(Templates)。)。一个一个类模板阐明了与一组类类模板阐明了与一组类有关的共同特征,同时有关的共同特征,同时允许用参数带入这组类允许用参数带入这组类中,使每个类具有独特中,使每个类具有独特的特性。利用类模板可的特性。利用类模板可自动产生出具有共性和自动产生出具有共性和特性的不同的类。特性的不同的类。例:例:一队军人一队军人一队学生一队学生一队大雁一队大雁共性:队列;排头
23、共性:队列;排头特性:个体数;队形特性:个体数;队形27例:例:队列型实体队列型实体队员类别队员类别,个体数个体数,队形队形队列型实体队列型实体军人军人,人数人数,队形队形一队军人一队军人队列型实体队列型实体大雁大雁,雁数雁数,队形队形一队大雁一队大雁队列型实体队列型实体学生学生,人数人数,队形队形一队学生一队学生n表现概念之间共性的另一种表现概念之间共性的另一种方式是模板(方式是模板(Templates)。)。一个类模板阐明了与一组类一个类模板阐明了与一组类有关的共同特征,同时允许有关的共同特征,同时允许用参数带入这组类中,使每用参数带入这组类中,使每个类具有独特的特性。利用个类具有独特的特
24、性。利用类模板可自动产生出具有共类模板可自动产生出具有共性和特性的不同的类。性和特性的不同的类。1.6 Template (模板)28SummarynC+ is a hybrid language that can be used simply as an extended and improved standard C, which is a procedural language.nC+ also can be used as an object-oriented language that has distinctive programming constructs not found in C and other procedural languages.nC+是一种混合型的语言,它支持过程程序设计、模块程序设是一种混合型的语言,它支持过程程序设计、模块程序设计、面向对象程序设计及类属程序设计。计、面向对象程序设计及类属程序设计。n面向对象程序设计的基本技术是:类、对象、封装、消息传递、面向对象程序设计的基本技术是:类、对象、封装、消息传递、多态等。多态等。n课程是以课程是以C+语言为载体,重点讲授面向对象程序设计技术。语言为载体,重点讲授面向对象程序设计技术。29