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

Slides:



Advertisements
Similar presentations
Class and Objects.
Advertisements

OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Object-Oriented PHP (1)
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Introduction to Classes and Data Abstraction
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
C++ fundamentals.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Chapter 16 Classes and Data Abstraction Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
計算機程式語言 Lecture 09-1 國立台灣大學生物機電系 林達德 9 9 Classes: A Deeper Look, Part 1.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Chapter 16: Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
About the Presentations
Encapsulation & Abstraction
Introduction to Classes
Corresponds with Chapter 7
Classes: A Deeper Look Outline
Chapter 9 Objects and Classes
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture 05 Data Abstraction & Encapsulation

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

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

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

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.

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

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

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()

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

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

Software Design II – Data Abstraction and Encapsulation 11 / 62June 4, 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

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

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

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

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

time2.h

time2.cpp (Part 1 of 2)

time2.cpp (Part 2 of 2)

fig16_07.cpp (Part 1 of 2)

fig16_07.cpp (Part 2 of 2)

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

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

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

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

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

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

create.h

create.cpp

fig16_08.cpp (Part 1 of 2)

fig16_08.cpp (Part 2 of 2)

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

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

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

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

time3.h (Part 1 of 2)

time3.h (Part 2 of 2)

time3.cpp (Part 1 of 3)

time3.cpp (Part 2 of 3)

time3.cpp (Part 3 of 3)

fig16_09.cpp (Part 1 of 3)

fig16_09.cpp (Part 2 of 3)

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

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

time4.h

time4.cpp (Part 1 of 2)

time4.cpp (Part 2 of 2)

fig16_10.cpp (Part 1 of 2)

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 *********************************

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

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

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

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

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

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.

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

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

fig16_11.cpp (Part 1 of 2)

fig16_11.cpp (Part 2 of 2) date1 = date2 = After default memberwise copy, date2 = Program Output

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

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.

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

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

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