Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Working with Classes Chapter 6. 2 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide.

Similar presentations


Presentation on theme: "1 Working with Classes Chapter 6. 2 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide."— Presentation transcript:

1 1 Working with Classes Chapter 6

2 2 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide provides a cohesive set of services. A class is a collection of data and routines that share a well-defined responsibility or provide provides a cohesive set of services. As programmer, you can ignore the rest of program while you are working on any part of code. This is accomplished by implementing classes As programmer, you can ignore the rest of program while you are working on any part of code. This is accomplished by implementing classes

3 3 Abstract Data Types (ADTs) Understanding ADTs is essential to understanding object-oriented programming Understanding ADTs is essential to understanding object-oriented programming ADT is a collection of data and operations that work on that data ADT is a collection of data and operations that work on that data ADT is used to manipulate real-world entities ADT is used to manipulate real-world entities The operations: The operations: describe the data to the rest of the program and describe the data to the rest of the program and allow the rest of the program to change the data allow the rest of the program to change the data

4 4 Benefits of Using ADTs Hide implementation details Hide implementation details Changes don’t affect the whole program Changes don’t affect the whole program You can make the interface more informative You can make the interface more informative It’s easier to improve performance It’s easier to improve performance The program becomes more self-documenting The program becomes more self-documenting You don’t have to pass data all over your program You don’t have to pass data all over your program You’re able to work with real-world entities rather You’re able to work with real-world entities rather

5 5 Examples of ADTs and likely operations Blender Blender Turn on Turn on Turn off Turn off Set speed Set speed Cruise Control Cruise Control Set speed Set speed Get current settings Get current settings Resume former speed Resume former speed Deactivate Deactivate

6 6 Examples of ADTs and likely operations Fuel Tank Fuel Tank Fill tank Fill tank Drain tank Drain tank Get tank capacity Get tank capacity Get tank status Get tank status Menu Menu Start new menu Start new menu Delete menu Delete menu Add menu item Add menu item Remove menu item Remove menu item Activate menu item Activate menu item Display menu Display menu Hide menu Hide menu Get menu choice Get menu choice

7 7 Examples of ADTs and likely operations Stack Stack Initialize stack Initialize stack Push item onto stack Push item onto stack Pop item from stack Pop item from stack Read top of stack Read top of stack File File Open file Open file Read file Read file Write file Write file Close file Close file Set current file location Set current file location

8 8 Examples of ADTs and likely operations Elevator Elevator Move up one floor Move up one floor Move down one floor Move down one floor Move to specific floor Move to specific floor Report current floor Report current floor Return to home floor Return to home floor Light Light Turn on Turn on Turn off Turn off

9 9 List List Initialize Initialize Insert Insert Remove Remove Read Read

10 10 ADT Guides Treat common objects such as files as ADTs Treat common objects such as files as ADTs Treat even simple items as ADTs Treat even simple items as ADTs Refer to an ADT independently of the medium it’s stored on Refer to an ADT independently of the medium it’s stored on

11 11 Good Class Interfaces To create a high quality class we must create a good interface To create a high quality class we must create a good interface Class interface is collection of public routines that could be seen and used by other programs or classes Class interface is collection of public routines that could be seen and used by other programs or classes

12 12

13 13 This class might have additional routines and data to support these services, but users of the class don’t need to know anything about them This class might have additional routines and data to support these services, but users of the class don’t need to know anything about them The following example shows bad class interface The following example shows bad class interface

14 14 It’s hard to see any connection among the command stack and report routines or the global data.

15 15 It could be revised to present a consistent abstraction as illustrated in the following example some of these routines were moved to other, more appropriate classes and some were converted to private routines used by InitializeProgram() and ShutDownProgram().

16 16 More Class Interface guidelines Provide services in pairs with their opposites If you have an operation that turns a light on, you’ll probably need one to turn it off. If you have an operation to add an item to a list, you’ll probably need one to delete an item from the list. Provide services in pairs with their opposites If you have an operation that turns a light on, you’ll probably need one to turn it off. If you have an operation to add an item to a list, you’ll probably need one to delete an item from the list. Move unrelated information to another class if you found that half a class’s routines work with half the class’s data, and half the routines work with the other half of the data.. Break them up! Move unrelated information to another class if you found that half a class’s routines work with half the class’s data, and half the routines work with the other half of the data.. Break them up!

17 17 Design and Implementation Issues

18 18 Containment (“has a” relationships) Containment is the simple idea that a class contains Containment is the simple idea that a class contains One way of thinking of containment is as a “has a” relationship One way of thinking of containment is as a “has a” relationship an employee “has a” name, “has a” phone number, and “has a” tax ID. Therefore.. name, phone number, or tax ID member data (attributes) of the Employee class an employee “has a” name, “has a” phone number, and “has a” tax ID. Therefore.. name, phone number, or tax ID member data (attributes) of the Employee class

19 19 Containment (“has a” relationships)..... Number of data elements: Number of data elements: “7±2” is a number of items a person can remember while performing other tasks. “7±2” is a number of items a person can remember while performing other tasks. If a class contains more than about seven data members, the class should be decomposed into multiple smaller classes. If a class contains more than about seven data members, the class should be decomposed into multiple smaller classes.

20 20 Inheritance (“is a” relationships) Applied when one class is a specialization of another class Applied when one class is a specialization of another class Aims to create simpler code by defining a base class that specifies common elements of two or more derived classes Aims to create simpler code by defining a base class that specifies common elements of two or more derived classes The common elements can be routine interfaces, implementations, data members, or data types The common elements can be routine interfaces, implementations, data members, or data types

21 21 Inheritance (“is a” relationships) … the new class “is a” more specialized version of the older class the new class “is a” more specialized version of the older class Inheritance adds complexity to a program, therefore, it is a dangerous technique Inheritance adds complexity to a program, therefore, it is a dangerous technique Examples: in UOB; Instructor is an employee.. So instructor class is derived from employee class. Examples: in UOB; Instructor is an employee.. So instructor class is derived from employee class.

22 22 Member Functions and Data Guidelines Keep the number of routines in a class as small as possible Keep the number of routines in a class as small as possible Minimize direct and indirect routine calls to other classes Minimize direct and indirect routine calls to other classes

23 23 Summary of Reasons to Create a Class Model real-world objects Model real-world objects Reduce complexity Reduce complexity Isolate complexity Isolate complexity Hide implementation details Hide implementation details Limit effects of changes Limit effects of changes

24 24 Summary of Reasons to Create a Class… Hide global data Hide global data Streamline parameter passing Streamline parameter passing Make central points of control Make central points of control Facilitate reusable code Facilitate reusable code Package related operations Package related operations


Download ppt "1 Working with Classes Chapter 6. 2 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide."

Similar presentations


Ads by Google