140 likes | 264 Views
OBJECT ORIENTED DESIGN. CMPS260 ULL Computer Science Department. OBJECT DEFINITION. IDENTIFY THE OBJECTS IN AN APPLICATION SPECIFY THEIR STRUCTURES, VALUES AND OPERATIONS HOOK THEM TOGETHER TO SOLVE THE PROBLEM (SWE). OBJECTS. OBJECTS ARE THE ENTITIES IN THE PROBLEM STATEMENT (NOUNS)
E N D
OBJECT ORIENTED DESIGN CMPS260 ULL Computer Science Department
OBJECT DEFINITION • IDENTIFY THE OBJECTS IN AN APPLICATION • SPECIFY THEIR STRUCTURES, VALUES AND OPERATIONS • HOOK THEM TOGETHER TO SOLVE THE PROBLEM (SWE)
OBJECTS • OBJECTS ARE THE ENTITIES IN THE PROBLEM STATEMENT (NOUNS) • OBJECTS HAVE STRUCTURES, VALUES AND OPERATIONS • OBJECTS INTERACT WITH OTHER OBJECTS (VISIBILITY) • OBJECTS ARE ABSTRACTIONS
ABSTRACTION • THE REMOVAL OF NON-ESSENTIAL DETAILS • PROVIDES AN INTERFACE FOR THE USER • HIDES THE IMPLEMENTATION FROM THE USER • PROVIDES FOR ENFORCEMENT
CLASSES • ABSTRACT DATA TYPES • SPECIFICATION (INTERFACE) • IMPLEMENTATION • COMPILED SEPARATELY • INTERACT VIA MESSAGE PASSING
DRIVER • PROCEDURAL CONTROL PROGRAM • CREATES OBJECTS • USES OBJECTS • CONTROLS SYSTEM FLOW • THE SMALLER THE BETTER
SUMMARY • OBJECTS ARE THE BASIC COMPONENTS • ABSTRACTIONS ARE DEFINED AND ENFORCED • PROCEDURAL ASPECTS ARE INCLUDED IN THE CLASSES AND THE DRIVER
SAMPLE OBJECTS • PERSON • STUDENT (DERIVED FROM PERSON) • COURSE • CLASS (INSTANCE OF A COURSE) • SCHEDULE (CONTAINS A STUDENT AND THEIR CLASSES)
EXAMPLE ATM MACHINE INTERFACE VERIFY, WITHDRAW … ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IMPLEMENTATION HIDDEN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ENFORCEMENT CRITICAL
ATM OBJECT DIAGRAM IMPLEMENTATION (HIDDEN) (BETTER BE) INTERFACE (PUBLIC) ACCT DATA ??? USERS VERIFYACCT WITHDRAW FUNDS IN & OUT ??? DEPOSIT CHK BAL
~~~~~~~~~~~~~~~~~~~ SPECIFICATION FILE ~~~~~~~~~~~~~~~~~~~// Declare a class to represent the date ADT// This is file DateType.hclass DateType{public: void Initialize(int newMonth, int newDay, int newYear); int YearIs() const; // returns year; int MonthIs() const; // returns month; int DayIs() const; // returns day; enum RelationType {LESS, EQUAL, GREATER}; RelationType ComparedTo(DateType aDate);private: int year; int month; int day;};
~~~~~~~~~~~~~~~~~~~ IMPLEMENTATION FILE ~~~~~~~~~~~~~~~~~~~// Define member functions of class DateType.// This is file DateType.cpp.#include "DateType.h" // gain access to the specificationvoid DateType::Initialize(int newMonth, int newDay, int newYear)// Post: year is set to newYear.// month is set to newMonth.// day is set to newDay.{ year = newYear; month = newMonth; day = newDay;}// Accessor funtions for the private data membersint DateType::MonthIs() const// Accessor function for month.{return month;}int DateType::YearIs() const// Accessor function for year.{return year;}int DateType::DayIs() const// Accessor function for day.{return day;}
RelationType DateType::ComparedTo(DateType aDate)// Pre: Self and aDate have been initialized.// Post: Function value = LESS, if self comes before aDate.// = EQUAL, if self is the same as aDate.// = GREATER, if self comes after aDate.{ if (year < aDate.year) return LESS; else if (year > aDate.year) return GREATER; else if (month < aDate.month) return LESS; else if (month > aDate.month) return GREATER; else if (day < aDate.day) return LESS; else if (day > aDate.day) return GREATER; else return EQUAL;}
~~~~~~~~~~~~~~~~~~~ DRIVER ~~~~~~~~~~~~~~~~~~~#include "DateType.h"#include <iostream>using namespace std;int main(){ DateType today; DateType anotherDay; today.Initialize(8, 24, 2006); anotherDay.Initialize(8, 25, 2006); cout << " Today is " << today.MonthIs() << "/" << today.DayIs() << "/" << today.YearIs() << endl; cout << " Another date is " << anotherDay.MonthIs() << "/" << anotherDay.DayIs() << "/" << anotherDay.YearIs() << endl; switch (today.ComparedTo(anotherDay)) { case LESS : cout << "today comes before anotherDay"; break; case GREATER : cout << "today comes after anotherDay"; break; case EQUAL : cout << "today and anotherDay are the same"; break; } return 0;}