Chapter 14 2 Object Oriented Software Development DaleWeems
Chapter 14 -2 Object. Oriented Software Development Dale/Weems 1
Composition (or Containment) l Composition (containment) is a mechanism by which the internal data (the state) of one class includes an object of another class 2
An Entry Object #include "Time. h" #include "Name. h" #include <string> class Entry { public: string Name. Str() const; // Returns a string made up of first name and last name string Time. Str() const; // Returns a string made up of hour, colon, minutes Entry(); // Default constructor Entry(……) // Parameterized constructor private: Name name; Time time; } 3
A Time. Card object has a Time object #include “time. h” class Time. Card { public: void Punch ( /* in */ int void Print ( ) const ; Time. Card ( /* in */ long /* in */ int Time. Card ( ) ; private: long id ; Time time. Stamp ; hours, minutes, seconds ) ; id. Num, init. Hrs, init. Mins, init. Secs ) ; }; 4
Time. Card Class Time. Card has a Time object Private data: Punch id time. Stamp Print. . . Time. Card Set Increment Write. . . Private data: hrs mins secs 5
Implementation of Time. Card Class Constructor Time. Card : : Time. Card ( /* in */ long int int id. Num, init. Hrs, init. Mins, init. Secs ) : time. Stamp (init. Hrs, init. Mins, init. Secs) // constructor initializer // Precondition: 0 <= init. Hrs <= 23 && 0 <= init. Mins <= 59 // 0 <= init. Secs <= 59 && init. Num is assigned // Postcondition: // id == id. Num && time. Stamp set by its constructor { id = id. Num ; } 6 6
Order in Which Constructors are Executed Given a class X, l if X is a derived class its base class constructor is executed first l next, constructors for member objects (if any) are executed (using their own default constructors if none is specified) l finally, the body of X’s constructor is executed 7
In C++. . . When the type of a formal parameter is a parent class, the argument used can be l l the same type as the formal parameter or, any descendant class type 8
Static Binding l Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter(s) l When pass-by-value is used, static binding occurs 9
Static Binding Is Based on Formal Parameter Type void Print ( /* in */ Time some. Time) { cout << “Time is “; some. Time. Write ( ); cout << endl; } CLIENT CODE Time start. Time(8, 30, 0); Ext. Time end. Time(10, 45, 0, CST); Print ( start. Time); Print ( end. Time); OUTPUT Time is 08: 30: 00 Time is 10: 45: 00 10
Dynamic Binding l Dynamic binding is the run-time determination of which function to call for a particular object of a descendant class based on the type of the argument l Feclaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding 11
Virtual Member Function // Specification file ( “time. h”) class Time { public: . . . virtual void Write () const; // Forces dynamic binding. . . private: int hrs; int mins; int secs; }; 12
Dynamic binding requires pass-by-reference void Print ( /* in */ Time& some. Time) { cout << “Time is “; some. Time. Write ( ); cout << endl; } CLIENT CODE Time start. Time ( 8, 30, 0); Ext. Time end. Time (10, 45, 0, CST); Print ( start. Time); Print ( end. Time); OUTPUT Time is 08: 30: 00 Time is 10: 45: 00 CST 13
Polymorphic operation l An operation that has multiple meanings depending on the type of the object to which it is bound at run time, e. g. , the Write function 14
Using virtual functions in C++ l Dynamic binding requires pass-by-reference when passing a class object to a function l In the declaration for a virtual function, the word virtual appears only in the base class l If a base class declares a virtual function, it must implement that function, even if the body is empty l A derived class is not required to re-implement a virtual function; if it does not, the base class version is used 15
Slicing Problem l When using pass by value n. A copy of the argument is sent to the parameter n Only common data members are copied when an object of the child class is passed to an object of the parent class n A child class is often “larger” than its parent Ext. Time (hrs, mins, secs, zone) Time => (hrs, mins, secs) 16
Slicing Problem 17
Pass by reference l Slicing problem also occurs with assignment: parent. Class. Object = child. Class. Object l No slicing with passing by reference: void Print( /* in */ Time& some. Time ) l However, still the Write function of the parent class is used in the call within the Print function: some. Time. Write(); 18
Object-Oriented Design Identify the Objects and Operations l Determine the relationship among objects l Design and Implement the driver l 19
Object-Oriented Design Application (Problem) Domain: • • The aspect of the world that we are modeling Checking accounts Bank tellers Spreadsheet rows (columns) Robot arms (legs) Solution Domain: • • The computer program that solves the real-life problem Counters Lists Menus Windows 20
Identify the Objects and Operations The problem: … The program must handle a customer’s savings account. The customer is allowed to deposit funds into the account and withdraw funds from the account, and the bank must pay interest on a quarterly basis. … Key nouns Key phrases Savings account Customer Deposit funds Withdraw funds Pay interest 21
Object Table l l Savings account l Customer Deposit Withdraw Pay interest Balance Note: Object table may change. New objects may be added 22
Determine the Relationships among Objects l Using inheritance or composition to build objects hierarchy Savings Account for preferred customers Savings Account for children under 12 23
Implementation of the Design l Choose a suitable data representation Built-in data type n Existing ADT n Create a new ADT n l Create algorithms for the abstract operations n Top-down design is often the best technique to create algorithms for the function tasks 24
Design the Driver The driver is the glue that puts the objects together l In C++, the driver becomes the main function l The driver has very little to do but process user commands or input some data and then delegate tasks to various objects. l 25
Case Study l l Beginning of the Appointment Calendar Program Class Entry composed of a Name class and a Time class Current Time class represents active time (seconds with Increment) Need a static Time class for time in the appointment calendar Is inheritance appropriate? 26
The End of Chapter 14 Part 2 27
- Slides: 27