




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Lecture 04 Fundamentals of OOP Object &ClassSoftware Design II Fundamentals of OOP: Object & Class2 November 30, 2021Design and programming are human activities;forget that and all is lost.-Bjarne Stroustrup, 1991Software Design II Fundamentals of OOP: Object & Class3 November 30, 2021Ou
2、tlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslClass Scope and Accessing Class MemberslSeparating Interface from ImplementationlControlling Access to MemberslAccess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class4 Nov
3、ember 30, 2021Complexity of SoftwarelComplexity of Computing (algorithm)Sort algorithms SVM (Support Vector Machine)lComplexity of ScaleLOC of a softwareCoordination and cooperation between softwareslMost of time, the CoS is more unavoidable for and has greater impact on a softwarelThe constant incr
4、ease of complexity in software drives software engineering, as well as software techniquesSoftware Design II Fundamentals of OOP: Object & Class5 November 30, 2021Monolithic programsSoftware Design II Fundamentals of OOP: Object & Class6 November 30, 2021Modular programmingSoftware Design II
5、 Fundamentals of OOP: Object & Class7 November 30, 2021Object-oriented programmingSoftware Design II Fundamentals of OOP: Object & Class8 November 30, 2021Modularization and AbstractionlModularization implements Divide and Conquer philosophy.lAbstraction is a nature extension of modularize,
6、giving each module a name, and embody it as a particular concept.lObject Oriented technique modularizes and abstracts software systems by leveraging concepts in the real world, which are already existed in our mind.lBy this means, our software engineering activities, such as design, programming, deb
7、ugging, etc, are consistent with our cognition, and therefore being nature and efficient.lThe evolution of programming is a way of abstract level ascendingSoftware Design II Fundamentals of OOP: Object & Class9 November 30, 2021Abstraction Level (I)lIn the beginning, at the birth of computing, t
8、here were no programming languages. Programs looked something like this:No abstraction at all, all are machine implementation details. When programming, we need translation in our mind.Software Design II Fundamentals of OOP: Object & Class10 November 30, 2021Abstraction Level (II)lAnd then we ha
9、ve a little abstraction, hide a little bit of machine implementation details:Mimic of an assembly languageSoftware Design II Fundamentals of OOP: Object & Class11 November 30, 2021Abstraction Level (III)lHere is the same program in JavaScript (rhino 1.7)Software Design II Fundamentals of OOP: Ob
10、ject & Class12 November 30, 2021Abstraction Level (IV)lOther possible implementations: PythonCoffeeScriptlWith concepts sum and range, the code is clear and easy. Thats the power of abstraction.lWith concept reduce, mathematical head coders will appreciate the code.Software Design II Fundamental
11、s of OOP: Object & Class13 November 30, 2021Object Orient lOO is a software technique of modularization and abstraction on software by reusing our existing concepts about real world, namely objects.Software Design II Fundamentals of OOP: Object & Class14 November 30, 2021Object Orient Progra
12、mminglObject-Oriented Programming (OOP)(面向对象编程)Programming using objectslObject(对象)Represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, and even a loan. Has a unique identity, state, and behaviors. State: a set of data fields (or propert
13、ies) with valuesBehavior: a set of functionsSoftware Design II Fundamentals of OOP: Object & Class15 November 30, 2021Real World vs. Cyber WorldCyber WorldReal worldSolutionProblemComputer Programmaterialization Abstraction(Functions,Classes/Objects)EntitySoftware Design II Fundamentals of OOP:
14、Object & Class16 November 30, 2021OOP Formal IntroductionOOP Formal IntroductionlObject-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called Objects, which are instantiated from classesData and functions closely relatedlInformation hiding Implem
15、entation details are hidden within the classes themselveslUnit of C+ programming: the class A class is like a blueprint reusableObjects are instantiated (created) from the class For example, a house is an instance of a “blueprint class”C programmers concentrate on functionsSoftware Design II Fundame
16、ntals of OOP: Object & Class17 November 30, 2021Big Picture ViewlOO techniques view software systems assystems of communicating objectslEach object is an instance of a classAll objects of a class share similar featuresattributesmethodsClasses can be specialized by subclasseslObjects communicate
17、by sending messagesSoftware Design II Fundamentals of OOP: Object & Class18 November 30, 2021OutlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslClass Scope and Accessing Class MemberslSeparating Interface from ImplementationlControlling Access to MemberslAc
18、cess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class19 November 30, 2021lClassesModel objects that have attributes (data members) and behaviors (member functions)Defined using keyword classADT & ClassADT & Class1classclass Time Time 2public:public:3
19、Time(); Time();4 void setTime( int, int, int ); void setTime( int, int, int );5 void void printMilitaryprintMilitary();();6 void printStandard(); void printStandard();7private:private:8 intint hour; hour; / 0 - 23 / 0 - 23 9 intint minute; minute; / 0 - 59 / 0 - 59 10 intint second; second; / 0 - 59
20、 / 0 - 59 11 ; ; / end class Time / end class Time PublicPublic: and PrivatePrivate: are member-access specifiers.setTimesetTime, printMilitaryprintMilitary, and printStandardprintStandard are member functions.TimeTime is the constructor.hourhour, minuteminute, and secondsecond are data members.Soft
21、ware Design II Fundamentals of OOP: Object & Class20 November 30, 2021ClassClassclass Name public: data fields functions Name()private: data fields functions; class Circle public: / The radius of this circle double radius; / Construct a circle object Circle() radius = 1; / Construct a circle obj
22、ect Circle(double newRadius) radius = newRadius; / Return the area of this circle double getArea() return radius * radius * 3.14159; ; Data field Function Constructors 1. Capitalize the initials of class name2. No initial value when declaring data fieldsSoftware Design II Fundamentals of OOP: Object
23、 & Class21 November 30, 2021lFormatBody delineated with braces ( and )Class definition terminates with a semicolon(分号)lAccess Authority (访问权限)PublicPublic - accessible wherever the program has access to an object of class TimePrivatePrivate - accessible only to member functions of the classProte
24、ctedProtected - discussed later in the courseClassClassSoftware Design II Fundamentals of OOP: Object & Class22 November 30, 2021Classl“public” and “private” can appear any times in any order in a classlData fields and functions can be declared in any order in a classFor example, all the followi
25、ng declarations are equivalent class Circle public: Circle(); Circle(double); private: double radius; public: double getArea(); double getRadius(); void setRadius(double); ; (a) (b) class Circle private: double radius; public: double getArea(); double getRadius(); void setRadius(double); public: Cir
26、cle(); Circle(double); ; class Circle public: Circle(); Circle(double); double getArea(); double getRadius(); void setRadius(double); private: double radius; ; (c) Preferred: one public and one private, public firstSoftware Design II Fundamentals of OOP: Object & Class23 November 30, 2021Constru
27、ctor(构造函数)lA special type of function to construct objects from the classPlays the role of initializing objectsExactly the same name as the classNo return typenot even voidExecuted automatically by the system, NOT YOU!Software Design II Fundamentals of OOP: Object & Class24 November 30, 2021Cons
28、tructor(构造函数)lCan be overloadedTo construct objects with different initial data valueslNo-arg constructorA constructor with no parameterslDefault constructorNo parameters, empty bodyProvided automatically only if no explicitly declared constructorsclass Circle public: double radius; double getArea()
29、 return radius * radius * 3.14159; ; Software Design II Fundamentals of OOP: Object & Class25 November 30, 2021lThe syntax to create an object ClassName variableName; For example, Circle circle1; Circle a;lA constructor is invoked when an object is createdTime sunset, / object of type Time array
30、OfTimes 5 , / array of Time objects *pointerToTime, / pointer to a Time object &dinnerTime = sunset; / reference to a Time objectNote: The class name becomes the new type specifier.Creating ObjectsCreating ObjectsSoftware Design II Fundamentals of OOP: Object & Class26 November 30, 2021lBina
31、ry scope resolution operator (:)Specifies which class owns the member function Different classes can have the same name for member functions作用域操作符。:操作符在其左操作数的作用域内找到其右操作数的名字。用于访问某个命名空间中的名字,如std:cout,表明名字cout 来自命名空间std。同样地,可用来从某个类取名字,如string:size_type,表明size_type 是由string 类定义的。 lFormat for definition
32、class member functions ReturnType ClassName:MemberFunctionName( ) ADT & ClassADT & ClassSoftware Design II Fundamentals of OOP: Object & Class27 November 30, 2021lIf member function is defined inside the classScope resolution operator and class name are not needed Defining a function out
33、side a class does not change it being public or privatelClasses encourage software reuseInheritance allows new classes to be derived from old oneslIn following program Time constructor initializes the data members to 0Ensures that the object is in a consistent state when it is createdADT & Class
34、ADT & ClassOutline28Outline29OutlineThe initial military time is 00:00The initial military time is 00:00The initial standard time is 12:00:00 AMThe initial standard time is 12:00:00 AM Military time after setTime is 13:27Military time after setTime is 13:27Standard time after setTime is 1:27:06
35、PMStandard time after setTime is 1:27:06 PM After attempting invalid settings:After attempting invalid settings:Military time: 00:00Military time: 00:00Standard time: 12:00:00 AMStandard time: 12:00:00 AMProgram Output30Software Design II Fundamentals of OOP: Object & Class31 November 30, 2021Ou
36、tlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslScope and Accessing Class MemberslSeparating Interface from ImplementationlControlling Access to MemberslAccess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class32 November
37、 30, 2021Scope & Accessing Class Members Scope & Accessing Class Members lClass scope Data members and member functionslFile scope Nonmember functionslFunction scope Variables defined in member functions, destroyed after function completeslInside a scope Members accessible by all member func
38、tions Referenced by nameSoftware Design II Fundamentals of OOP: Object & Class33 November 30, 2021Scope & Accessing Class Members Scope & Accessing Class Members function scopelocal scopefile scopefunction scopelocal scopeSoftware Design II Fundamentals of OOP: Object & Class34 Novem
39、ber 30, 2021lOutside a scope Use handles An object name, a reference to an object or a pointer to an objectlAccessing class members Same as structsDot (. .) for objects and arrow (-) for pointersExample: t.hour is the hour element of tTimePtr-hour is the hour elementScope & Accessing Class Membe
40、rs Scope & Accessing Class Members OutlineAssign 7 to x and print using the objects name: 7Assign 7 to x and print using the objects name: 7Assign 8 to x and print using a reference: 8Assign 8 to x and print using a reference: 8Assign 10 to x and print using a pointer: 10Assign 10 to x and print
41、 using a pointer: 10Program Output35Software Design II Fundamentals of OOP: Object & Class36 November 30, 2021OutlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslScope and Accessing Class MemberslSeparating Interface from ImplementationlControlling Access to
42、 MemberslAccess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class37 November 30, 2021Separating Interface from Implementation Separating Interface from Implementation lSeparating interface from implementation Easier to modify programs C+ programs can be split
43、intoHeader files contains class definitions and function prototypesSource-code files contains member function definitionslProgram Outline:Using the same Time class as before, create a header fileCreate a source code fileLoad the header file to get the class definitionsDefine the member functions of
44、the classSoftware Design II Fundamentals of OOP: Object & Class38 November 30, 2021l将程序分成多个文件的理由:1)将相关类和函数放在一个特定的文件中,可通过对不同的文件进行单独编写、编译、连接编写、编译、连接,来避免了重复工作。2)程序管理更容易。 类的声明类的声明-类声明文件(类声明文件(* *.h.h) 类成员的实现类成员的实现-类实现文件(类实现文件(* *.cpp.cpp) 主函数主函数-类的使用文件(类的使用文件(* *.cpp.cpp)3)充分利用了类的封装特性。在对程序进行调试修改时,只需
45、对某一个类的定义和实现进行操作。Separating Interface from Implementation Separating Interface from Implementation Outlinetime1.h使用条件预处理指令来避免头文件被重复包含使用条件预处理指令来避免头文件被重复包含39Outlinetime1.cpp (Part 1 of 2)只需要包含类声明文件只需要包含类声明文件40Outlinetime1.cpp (Part 2 of 2)41Outlinefig16_04.cpp (Part 1 of 2)42Outlinefig16_04.cpp (Part 2
46、 of 2)The initial military time is 00:00The initial military time is 00:00The initial standard time is 12:00:00 AMThe initial standard time is 12:00:00 AM Military time after setTime is 13:27Military time after setTime is 13:27Standard time after setTime is 1:27:06 PMStandard time after setTime is 1
47、:27:06 PM After attempting invalid settings:After attempting invalid settings:Military time: 00:00Military time: 00:00Standard time: 12:00:00 AMStandard time: 12:00:00 AM Program OutputProgram Output43Software Design II Fundamentals of OOP: Object & Class44 November 30, 2021time1.cpp#include “ti
48、me1.h”fig16_04.cpp#include “time1.h”time1.h编译编译连接time1.objFig16_04.obj可执行程序Fig16_04.exeSoftware Design II Fundamentals of OOP: Object & Class45 November 30, 2021OutlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslScope and Accessing Class MemberslSeparating
49、Interface from ImplementationlControlling Access to MemberslAccess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class46 November 30, 2021lPurpose of public Give clients a view of the services the class provides (interface)lPurpose of privateDefault settingHide
50、details of how the class accomplishes its tasks (implementation)Private members only accessible through the public interface using public member functionsControlling Access to Members Controlling Access to Members Outlinefig16_05.cpp47OutlineProgram OutputBorland C+ command-line compiler error messa
51、gesBorland C+ command-line compiler error messagesTime1.cpp:Time1.cpp:Fig16_05.cpp:Fig16_05.cpp:Error E2247 Fig16_05.cpp 15: Error E2247 Fig16_05.cpp 15: Time:hour is not accessible in function main()Time:hour is not accessible in function main()Error E2247 Fig16_05.cpp 18: Error E2247 Fig16_05.cpp
52、18: Time:minute is not accessible in function main()Time:minute is not accessible in function main() * * * * 2 errors in Compile 2 errors in Compile * * * *Microsoft Visual C+ compiler error messagesMicrosoft Visual C+ compiler error messagesCompiling.Compiling.Fig16_05.cppFig16_05.cppD:Fig16_05.cpp
53、(15) : D:Fig16_05.cpp(15) : error C2248: hour : cannot access private error C2248: hour : cannot access private member declared in class Timemember declared in class TimeD:Fig16_05time1.h(18) : see declaration of hourD:Fig16_05time1.h(18) : see declaration of hourD:Fig16_05.cpp(18) : D:Fig16_05.cpp(
54、18) : error C2248: minute : cannot access privateerror C2248: minute : cannot access privatemember declared in class Timemember declared in class TimeD:time1.h(19) : see declaration of minuteD:time1.h(19) : see declaration of minuteError executing cl.exe.Error executing cl.exe. test.exe - 2 error(s)
55、, 0 warning(s)test.exe - 2 error(s), 0 warning(s)48Software Design II Fundamentals of OOP: Object & Class49 November 30, 2021OutlinelThe evolution way to the Object Oriented techniquelAbstract Data Type & ClasslScope and Accessing Class MemberslSeparating Interface from ImplementationlContro
56、lling Access to MemberslAccess Functions and Utility FunctionsSoftware Design II Fundamentals of OOP: Object & Class50 November 30, 2021Access Access Functions and Utility Functions Functions and Utility Functions lUtility functions private functions that support the operation of public function
57、s Not intended to be used directly by clientslAccess functions public functions that read/display data or check conditionsFor a container, it could call the isEmpty functionlNextProgram to take in monthly sales and output the totalImplementation not shown, only access functionsOutlinesalesp.h51Outli
58、nesalesp.cpp (Part 1 of 3)52Outlinesalesp.cpp (Part 2 of 3)53Outlinesalesp.cpp (Part 3 of 3)54Outlinefig16_06.cppEnter sales amount for month 1: 5314.76Enter sales amount for month 1: 5314.76Enter sales amount for month 2: 4292.38Enter sales amount for month 2: 4292.38Enter sales amount for month 3:
59、 4589.83Enter sales amount for month 3: 4589.83Enter sales amount for month 4: 5534.03Enter sales amount for month 4: 5534.03Enter sales amount for month 5: 4376.34Enter sales amount for month 5: 4376.34Enter sales amount for month 6: 5698.45Enter sales amount for month 6: 5698.45Enter sales amount for month 7: 4439.22Enter sales amount for month 7: 4439.22Enter sales amount for month 8: 5893.57Enter sales amount for month 8: 5893.57Enter sales amount for month 9: 4909.67Enter sales
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汉英儿童二语分级读物对比研究
- 社区心理健康普及
- 风湿病用药护理
- 2025年学校安全日教育主题活动
- 海外宠物培训课件
- 电商文化培训
- 同济大学内科学教学体系
- 预防接种知识培训课件
- 顺利消防2021课件
- 项目总工程师培训课件
- 水利水电工程施工机械台班费定额
- 山东某智慧农场项目可行性研究报告
- 新版《医疗器械经营质量管理规范》(2024)培训试题及答案
- 2025年N1叉车司机考试试题(附答案)
- 新建自体血液回收机项目立项申请报告
- GB/T 45004-2024钢铁行业低碳企业评价指南
- 2024年鲜食玉米项目可行性研究报告
- 5.1延续文化血脉-(教学设计) 2024-2025学年统编版道德与法治九年级上册
- 《正弦、余弦函数的性质-第一课时(周期性和奇偶性)》名师课件2
- 2024年部编版七年级语文上册全程电子课本
- 2024-2025学年初中信息技术(信息科技)七年级上册苏科版(2023)教学设计合集
评论
0/150
提交评论