Download Designing Classes in OOP: UH Lecture, Spring 2005 and more Exams Computer Science in PDF only on Docsity!
D. Mirkovic, C++ Programming, Spring 2005
Lecture 5: Designing Classes
Dragan Mirkovic
Department of Computer Science
University of Houston
Announcements
- Today:
- Designing classes in OOL
- Ch 5. In Irvine.
- Midterm exam
D. Mirkovic, C++ Programming, Spring 2005
Introduction
- So far we have discussed the syntax
and semantics of C++
- Here we discuss the basic ideas on the
design of classes and components
- Object model
- How to build components
Analysis, Design and Programming
- OO programming concentrates on the physical entities that make up an application
- OO design: method that uses oo decomposition and abstraction to implement a model of a system
- OO decomposition:
- Using class and object abstractions to logically structure a system
- OO analysis:
- Examination of application requirements in terms of classes and objects
D. Mirkovic, C++ Programming, Spring 2005
Abstraction types
- Two general types of abtractions:
- Control (action) type:
- Operations (insert, move, resize, find, …)
- Entity type
- Specific static elements in the problem domain
- Easy to recognize (correspond to real objects)
- Examples:
- Window, display, cursor, screen, …
- Employee, Student, Course, …
Encapsulation
- Enclosing of both variables and
functions inside a class with a well
defined interface
- Achieved though information hiding
- Implementation details are hidden
- User interface is public and well defined
- Leads to standardization of interfaces
- Great improvement in software development
D. Mirkovic, C++ Programming, Spring 2005
Modularity
- One of the oldest and best concepts in programming - Compilation units (sharing and hiding of information) - Subroutines (common subtasks used in different parts of the application) - Modules
- Procedural languages achieve modularity by using procedural abstractions - Defining problem in terms of its operations
Hierarchy
- Hierarchy (inheritance)
- Parent-child relationship between classes
- Derived classes:
- Inherit operations and data from their base classes
- Only additional attributes need to be defined
- Example:
x y x y z
D. Mirkovic, C++ Programming, Spring 2005
Software design goals
- Simplicity
- Well designed and meaningful objects
- User friendly class interfaces
- Flexibility
- Easy accommodation of changes
- Extensibility
- Additions are easy to implement without destroying initial design
- Portability
- Reusability
- Well designed software can be used in large number of applications
Designing OO Programs
- OO program should be a faithful model of reality - It is important to identify key concepts in an application - Construct corresponding classes - Iterative process
- Traditional approach:
- Top-down procedural design
- Dependencies are hierarchical
- Changes may be difficult to implement
D. Mirkovic, C++ Programming, Spring 2005
Components
- Component = group of related classes
that work together
- Well defined interfaces
- Design steps:
- Find the classes
- Specify class operations
- Specify class dependencies
- Specify class interfaces
Find the Classes
- This is the most obvious step in the
design
- If the class is not obvious it shouldn't be there!
- Discovery of inherent structure of the
application
- It is important to understand the
application well
D. Mirkovic, C++ Programming, Spring 2005
Class interfaces
- Function prototypes for member
functions define the class interface
- The interfaces can be shared with other
programmers
Example
- Doctor’s Office Scheduling
- 1 st^ Step: Specifications
- Automatically schedule appointments for patients
- Multiple patients and doctors
- 15 min time slots between 8:00 am and 6:00 pm.
- Print out a separate daily schedule for each doctor
- Interactive program with screen i/o and file output.
D. Mirkovic, C++ Programming, Spring 2005
2 nd^ Step: Analysis
- Find the classes:
- What are the objects in the application
- Doctor
- Patient
- DailySchedule
- Appointment
- Scheduler
- What would be a typical scenario for the appointment scheduling process: - A Patient enters her/his name into the Scheduler - Patient chooses a Doctor - Scheduler adds the appointment to the doctor’s schedule and to the patients records - Scheduler confirms the appointment
Class Dependencies
- Interactions between objects Scheduler
Doctor Patient
DailySchedule Appointment Notation: Composite Link
D. Mirkovic, C++ Programming, Spring 2005
Additional Classes
- We will need a time slot class
- Translation and formatting of appointment times (enter/display time class in hh:mm format, store as integer) TimeSlot { public:TimeSlot( const unsigned n = 0 ); unsignedfriend istream AsInteger() & operator >>(istream const; // Returns & theinp, integer value TimeSlot & T); of the time. friend// ------ Static ostream & operator <<(ostreammember functions ------- & os, const TimeSlot & T); staticstatic unsigned GetStartHour();unsigned GetApptLen(); staticstatic voidvoid SetStartHour( unsigned nSetApptLen( unsigned n ); ); private:static unsigned StartHour; staticunsigned unsigned ApptLen; intValue; };void^ SetIntValue(^ unsigned^ n^ );
3 rd^ Step: Design
- Once all classes have been identified we can specify their interfaces
- Result of the design phase is the header file with all classes and their interfaces defined
- Classes:
- TimeSlot
- Doctor
- Patient
- DailySchedule
- Appointment
- Scheduler
D. Mirkovic, C++ Programming, Spring 2005
4 th^ Step: Main program
- The main program creates an instance of the Scheduler #include "doctors.h" #include "schedlr.h“ static Doctor doctorArray[NumDoctors]; int main() { cout << "Doctors Office Scheduling Program\n\n"; Scheduler officeSchedule( doctorArray ); officeSchedule.ScheduleAllAppointments(); officeSchedule.PrintAllAppointments("appts.txt"); return 0; }
5 th^ Step: Class Implementations
- See the code in the text.