Working with Classes - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Working with Classes

Description:

Working with Classes Chapter 6 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide provides a cohesive ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 25
Provided by: edub106
Category:

less

Transcript and Presenter's Notes

Title: Working with Classes


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

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

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

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

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

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

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

9
  • List
  • Initialize
  • Insert
  • Remove
  • Read

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

11
Good Class Interfaces
  • 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

12
(No Transcript)
13
  • This class might have additional routines and
    data to support these services, but users of the
    class dont need to know anything about them
  • The following example shows bad class interface

14
Its hard to see any connection among the command
stack and report routines or the global data.
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
More Class Interface guidelines
  • Provide services in pairs with their opposites If
    you have an operation that turns a light on,
    youll probably need one to turn it off. If you
    have an operation to add an item to a list,
    youll probably need one to delete an item from
    the list.
  • Move unrelated information to another class if
    you found that half a classs routines work with
    half the classs data, and half the routines work
    with the other half of the data .. Break them up!

17
Design and Implementation Issues
18
Containment (has a relationships)
  • Containment is the simple idea that a class
    contains
  • 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

19
Containment (has a relationships) .....
  • Number of data elements
  • 72 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.

20
Inheritance (is a relationships)
  • 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
  • The common elements can be routine interfaces,
    implementations, data members, or data types

21
Inheritance (is a relationships)
  • the new class is a more specialized version of
    the older class
  • 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.

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

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

24
Summary of Reasons to Create a Class
  • Hide global data
  • Streamline parameter passing
  • Make central points of control
  • Facilitate reusable code
  • Package related operations
Write a Comment
User Comments (0)
About PowerShow.com