Presentation is loading. Please wait.

Presentation is loading. Please wait.

SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.

Similar presentations


Presentation on theme: "SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture."— Presentation transcript:

1 SMIE-121 Software Design II http://smiesd.sinaapp.com/ raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture 05 Data Abstraction & Encapsulation

2 Software Design II – Data Abstraction and Encapsulation 2 / 62June 4, 2016 Design and programming are human activities; forget that and all is lost. --Bjarne Stroustrup, 1991

3 Software Design II – Data Abstraction and Encapsulation 3 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

4 Software Design II – Data Abstraction and Encapsulation 4 / 62June 4, 2016 Defining Abstraction Abstraction is the process of extracting common features from specific examples Abstraction is a process of defining the essential concepts while ignoring the inessential details

5 Software Design II – Data Abstraction and Encapsulation 5 / 62June 4, 2016 Different Types of Abstraction Data Abstraction Programming languages define constructs to simplify the way information is presented to the programmer Functional Abstraction Programming languages have constructs that ‘gift wrap’ very complex and low level instructions into instructions that are much more readable Object Abstraction OOP languages take the concept even further and abstract programming constructs as objects.

6 Software Design II – Data Abstraction and Encapsulation 6 / 62June 4, 2016 Anything that you can describe in your OOP program can be represented as an object, and that representation can be created, manipulated and destroyed to represent how you use the real object that it models. Everything is an Object

7 Software Design II – Data Abstraction and Encapsulation 7 / 62June 4, 2016 An object is a self-contained entity with attributes and behaviors Defining an Object information an object must know:  identity – uniqueness  attributes – structure  state – current condition behavior an object must do:  methods – what it can do  events – what it responds to

8 Software Design II – Data Abstraction and Encapsulation 8 / 62June 4, 2016 Class as Abstraction A class is an abstraction of its instances. It defines all the attributes and methods that its instances must also have. Person name sex age tellSex() tellAge()

9 Software Design II – Data Abstraction and Encapsulation 9 / 62June 4, 2016 A Class acts as the template from which an instance of an object is created. The class defines the properties of the object and the methods used to control the object's behavior. A Class specifies the structure of data as well as the methods which manipulate that data. Such data and methods are contained in each instance of the class. A Class is a model or template that can be instantiated to create objects with a common definition, and therefore common properties, operations and behavior. Defining a Class

10 Software Design II – Data Abstraction and Encapsulation 10 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

11 Software Design II – Data Abstraction and Encapsulation 11 / 62June 4, 2016 11 Constructors are methods which set the initial state of an object Constructors are called when an object is created A default constructor is a constructor with no parameters and do nothing Restrictions on constructors constructor name must be the same as the class name constructor cannot return a value, not even void only initialize attributes of the object Constructors

12 Software Design II – Data Abstraction and Encapsulation 12 / 62June 4, 2016 Constructors Constructor function Can initialize class members Same name as the class, no return type Member variables can be initialized by the constructor or set afterwards Defining objects Initializers can be provided Initializers passed as arguments to the class’ constructor

13 Software Design II – Data Abstraction and Encapsulation 13 / 62June 4, 2016 Format ClassName objectName ( value1, value2, … ); Constructor assigns value1, value2, etc. to its member variables If not enough values specified, rightmost parameters set to their default (specified by programmer) MyClass myObject( 3, 4.0 ); Constructors

14 Software Design II – Data Abstraction and Encapsulation 14 / 62June 4, 2016 Default Arguments with Constructors Default constructor One per class No arguments or all default arguments Can be invoked without arguments Default arguments ( 缺省参数需要在函数声明的时候写 上,如果定义跟声明分开,则在定义函数的时候不需要 在写上缺省参数 ) Set in default constructor function prototype (in class definition) Do not set defaults in the function definition, outside of a class Example: SampleClass( int = 0, float = 0); Constructor has same name as class

15 Software Design II – Data Abstraction and Encapsulation 15 / 62June 4, 2016 Using Constructors

16 time2.h

17 time2.cpp (Part 1 of 2)

18 time2.cpp (Part 2 of 2)

19 fig16_07.cpp (Part 1 of 2)

20 fig16_07.cpp (Part 2 of 2)

21 Constructed with: all arguments defaulted: 00:00 12:00:00 AM hour specified; minute and second defaulted: 02:00 2:00:00 AM hour and minute specified; second defaulted: 21:34 9:34:00 PM hour, minute, and second specified: 12:25 12:25:42 PM all invalid values specified: 00:00 12:00:00 AM Program Output

22 Software Design II – Data Abstraction and Encapsulation 22 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

23 Software Design II – Data Abstraction and Encapsulation 23 / 62June 4, 2016 Destructors Member function of class Performs termination housekeeping before the system reclaims the object’s memory Complement of the constructor Name is tilde ( ~ ) followed by the class name ~Time Recall that the constructor’s name is the class name Receives no parameters, returns no value (无形参定 义) One destructor per class - no overloading allowed

24 Software Design II – Data Abstraction and Encapsulation 24 / 62June 4, 2016 Destructors 析构函数在对象生命周期结束时,回收其所占用的内存空间。 析构函数是 “ 反向 ” 的构造函数,析构函数不允许有返回值,不带参 数,一个类中只有一个。 析构函数的作用正好与构造函数相反。当对象超出其作用范围,对 应的内存空间被系统回收或被程序用 delete 删除时,析构函数将被 调用。

25 Software Design II – Data Abstraction and Encapsulation 25 / 62June 4, 2016 When Constructors and Destructors Are Called Constructors and destructors called automatically Order depends on scope of objects Global scope objects ( 在 main 函数外声明的变量 ) Constructors called before any other function (including main ) Destructors called when main terminates (or exit function called) Destructors not called if program terminates with abort

26 Software Design II – Data Abstraction and Encapsulation 26 / 62June 4, 2016 Automatic local objects Constructors called when objects defined Destructors called when objects leave scope (when the block in which they are defined is exited) Destructors not called if program ends with exit or abort static local objects Constructors called when execution reaches the point where the objects are defined Destructors called when main terminates or the exit function is called Destructors not called if the program ends with abort When Constructors and Destructors Are Called

27 create.h

28 create.cpp

29 fig16_08.cpp (Part 1 of 2)

30 fig16_08.cpp (Part 2 of 2)

31 Object 1 constructor (global created before main) Object 2 constructor (local automatic in main) Object 3 constructor (local static in main) Object 5 constructor (local automatic in create) Object 6 constructor (local static in create) Object 7 constructor (local automatic in create) Object 7 destructor Object 5 destructor Object 4 constructor (local automatic in main) Object 4 destructor Object 2 destructor Object 6 destructor Object 3 destructor Object 1 destructor Program Output

32 Software Design II – Data Abstraction and Encapsulation 32 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

33 Software Design II – Data Abstraction and Encapsulation 33 / 62June 4, 2016 Data Members and Member Functions Classes provide public member functions Set (i.e., write) or get (i.e., read) values of private data members E.g., one action to operate the private data members of class Naming Member function that sets interestRate typically named setInterestRate Member function that gets interestRate would typically be called getInterestRate

34 Software Design II – Data Abstraction and Encapsulation 34 / 62June 4, 2016 Do set and get capabilities effectively make data members public ? No! Programmer decides what the function can set and what information the function can get public set functions should Check attempts to modify data members Ensure that the new value is appropriate for that data item Example: an attempt to set the day of the month to 37 would be rejected Programmer must include these features Data Members and Member Functions

35 time3.h (Part 1 of 2)

36 time3.h (Part 2 of 2)

37 time3.cpp (Part 1 of 3)

38 time3.cpp (Part 2 of 3)

39 time3.cpp (Part 3 of 3)

40 fig16_09.cpp (Part 1 of 3)

41 fig16_09.cpp (Part 2 of 3)

42 fig16_09.cpp (Part 3 of 3) Result of setting all valid values: Hour: 17 Minute: 34 Second: 25 Result of attempting to set invalid hour and second: Hour: 0 Minute: 43 Second: 0 Incrementing minute 3 times: Start time: 11:58:00 AM minute + 1: 11:59:00 AM minute + 1: 12:00:00 PM minute + 1: 12:01:00 PM Program Output

43 Software Design II – Data Abstraction and Encapsulation 43 / 62June 4, 2016 A Subtle Trap: Returning a Reference to a Private Data Member Reference to an object Alias for the name of the object Can be used on the left side of an assignment statement Reference can receive a value, which changes the original object as well One way to use this capability Have a public member function of a class return a non- const reference to a private data member This reference can be modified, which changes the original data

44 time4.h

45 time4.cpp (Part 1 of 2)

46 time4.cpp (Part 2 of 2)

47 fig16_10.cpp (Part 1 of 2)

48 fig16_10.cpp (Part 2 of 2) Program Output Hour before modification: 20 Hour after modification: 30 ********************************* POOR PROGRAMMING PRACTICE!!!!!!!! badSetHour as an lvalue, Hour: 74 *********************************

49 Software Design II – Data Abstraction and Encapsulation 49 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

50 Software Design II – Data Abstraction and Encapsulation 50 / 62June 4, 2016 Information Hiding & Encapsulation Information Hiding: Each program component should hide as much information as possible from the users of the component. Encapsulation is the process of hiding an object’s implementation from another object, while presenting only the interfaces that should be visible. An object should be self-governing Any changes to the object's state (its variables) should be accomplished by that object's methods Encapsulation lets you protect information in your objects from being used incorrectly

51 Software Design II – Data Abstraction and Encapsulation 51 / 62June 4, 2016 Encapsulation You can take two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program From the external view, an object is an encapsulated entity, providing services These services define the interface to the object

52 Software Design II – Data Abstraction and Encapsulation 52 / 62June 4, 2016 Encapsulation An encapsulated object can be thought of as a black box Its inner workings are hidden to the client, which only invokes the interface methods Client Methods Data

53 Software Design II – Data Abstraction and Encapsulation 53 / 62June 4, 2016 Principles of Encapsulation “Don’t ask how I do it, but this is what I can do” - The encapsulated object “I don’t care how, just do your job, and I’ll do mine” - One encapsulated object to another

54 Software Design II – Data Abstraction and Encapsulation 54 / 62June 4, 2016 Encapsulating a Class Members of a class must always be declared with the minimum level of visibility. Provide setters and getters (also known as mutators/accessors) to allow controlled change/access to private data. Provide other public methods (known as interfaces ) that other objects must adhere to in order to interact with the object.

55 Software Design II – Data Abstraction and Encapsulation 55 / 62June 4, 2016 Setters and Getters public: void setSex(char s) { // validate here sex = s; } char getSex() { // format here return sex; } private: char sex; Setters and Getters allow controlled access to class data Setters are methods that (only) alter the state of an object Use setters to validate data before changing the object state Getters are methods that (only) return information about the state of an object Use getters to format data before returning the object’s state

56 Software Design II – Data Abstraction and Encapsulation 56 / 62June 4, 2016 Assignment by Default Memberwise Copy Assignment operator ( = ) Sets variables equal, i.e., x = y ; Can be used to assign an object to another object of the same type Memberwise copy — member by member copy myObject1 = myObject2 ; Objects may be (will be introduced later) Passed as function arguments Returned from functions (call-by-value default) Use pointers for call by reference

57 fig16_11.cpp (Part 1 of 2)

58 fig16_11.cpp (Part 2 of 2) date1 = 7-4-1993 date2 = 1-1-1990 After default memberwise copy, date2 = 7-4-1993 Program Output

59 Software Design II – Data Abstraction and Encapsulation 59 / 62June 4, 2016 Outline Recap abstraction, object & class Initializing class objects: constructors Destructors Data members and member Functions Information hiding & encapsulation UML and software reusability

60 Software Design II – Data Abstraction and Encapsulation 60 / 62June 4, 2016 Unified Model Language UML is a general-purpose modeling language of software engineering It provides a set of graphic notation techniques to create visual models of object-oriented software-intensive systems. It was developed by Grady Booch, Ivar Jacobson and James Rumbaugh at Rational Software in the 1990s It was adopted by the Object Management Group (OMG) in 1997 In 2000 the UML was accepted by the International Organization for Standardization (ISO) as a standard for modeling software-intensive systems.

61 Software Design II – Data Abstraction and Encapsulation 61 / 62June 4, 2016 Class Diagrams

62 Software Design II – Data Abstraction and Encapsulation 62 / 62June 4, 2016 Software Reusability Object-oriented programmers Concentrate on implementing useful classes Tremendous opportunity to capture and catalog classes Accessed by large segments of the programming community Class libraries exist for this purpose Software Constructed from existing, well-defined, carefully tested, portable, widely available components Speeds development of powerful, high-quality software

63 Software Design II – Data Abstraction and Encapsulation 63 / 62June 4, 2016 Thank you!


Download ppt "SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture."

Similar presentations


Ads by Google