Object-oriented design CSE 432: Object-Oriented Software Engineering.
1 Chapter 6 Object-Oriented Software Development.
-
date post
21-Dec-2015 -
Category
Documents
-
view
218 -
download
1
Transcript of 1 Chapter 6 Object-Oriented Software Development.
![Page 1: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/1.jpg)
1
Chapter 6
Object-Oriented Software Development
![Page 2: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/2.jpg)
2
Chapter 6 Topics
Structured Programming vs. Object-Oriented Programming
Using Inheritance to Create a New C++ class Type
Using Composition (Containment) to Create a New C++ class Type
Static vs. Dynamic Binding of Operations to Objects
Virtual Member Functions
![Page 3: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/3.jpg)
3
Two Programming Paradigms
Structural (Procedural) Object-Oriented PROGRAM PROGRAM
FUNCTION
FUNCTION
FUNCTION
OBJECT
Operations
Data
OBJECT
Operations
Data
OBJECT
Operations
Data
![Page 4: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/4.jpg)
4
Object-Oriented Programming Language Features
1. Data abstraction
2. Inheritance of properties
3. Dynamic binding of operations to objects
![Page 5: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/5.jpg)
5
OOP Terms C++ Equivalents
Object Class object or class instance
Instance variable Private data member
Method Public member function
Message passing Function call ( to a public member function )
![Page 6: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/6.jpg)
6
What is an object?
OBJECT
Operations
Data
set of methods(public member functions)
internal state(values of private data members)
![Page 7: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/7.jpg)
7
Inheritance Hierarchy Among Vehicles
vehicle
wheeled vehicle boat
bicyclecar
four-door two-door
Every car is a wheeled vehicle.
![Page 8: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/8.jpg)
8
Inheritance
is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class
the class being inherited from is the Base Class (Superclass)
the class that inherits is the Derived Class (Subclass)
the derived class is then specialized by adding properties specific to it
![Page 9: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/9.jpg)
9
class Time Specification
// SPECIFICATION FILE ( time.h )
class Time{
public :
void Set ( int hours , int minutes , int seconds ) ;void Increment ( ) ;void Write ( ) const ;Time ( int initHrs, int initMins, int initSecs ) ; // constructor Time ( ) ; // default constructor
private :
int hrs ; int mins ; int secs ;
} ;
![Page 10: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/10.jpg)
10
Class Interface Diagram
Private data:
hrs
mins
secs
Set
Increment
Write
Time
Time
Time class
![Page 11: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/11.jpg)
11
Using Inheritance to Add Features
// SPECIFICATION FILE ( exttime.h)#include “time.h”enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;
class ExtTime : public Time // Time is the base class{public :
void Set ( int hours, int minutes, int seconds , ZoneType timeZone ) ;
void Write ( ) const ; ExtTime ( int initHrs , int initMins , int initSecs ,
ZoneType initZone ) ; // constructor ExtTime ( ) ; // default constructor
private :ZoneType zone ; // added data member
} ;
![Page 12: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/12.jpg)
12
class ExtTime: public Time
says class Time is a public base class of the derived class ExtTime
as a result, all public members of Time (except constructors) are also public members of ExtTime
in this example, new constructors are provided, new data member zone is added, and member functions Set and Write are overridden
![Page 13: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/13.jpg)
13
Class Interface Diagram
Private data:
hrs
mins
secs
ExtTime class
Set
Increment
Write
Time
Time
Set
Increment
Write
ExtTime
ExtTime
Private data:zone
![Page 14: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/14.jpg)
14
Client Code Using ExtTime
#include “exttime.h” . . .
ExtTime thisTime ( 8, 35, 0, PST ) ; ExtTime thatTime ; // default constructor called
thatTime.Write( ) ; // outputs 00:00:00 EST cout << endl ;
thatTime.Set (16, 49, 23, CDT) ; thatTime.Write( ) ; // outputs 16:49:23 CDT
cout << endl ;
thisTime.Increment ( ) ; thisTime.Increment ( ) ; thisTime.Write ( ) ; // outputs 08:35:02 PST cout << endl ;
![Page 15: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/15.jpg)
15
Constructor Rules for Derived Classes
at run time, the base class constructor is implicitly called first, before the body of the derived class’s constructor executes
if the base class constructor requires parameters, they must be passed by the derived class’s constructor
![Page 16: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/16.jpg)
16
Implementation of ExtTime Default Constructor
ExtTime :: ExtTime ( )
// Default Constructor
// Postcondition:
// hrs == 0 && mins == 0 && secs == 0
// (via an implicit call to base class default constructor )
// && zone == EST
{
zone = EST ;
}
![Page 17: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/17.jpg)
17
Implementation of Another ExtTime Class Constructor
ExtTime :: ExtTime ( /* in */ int initHrs,
/* in */ int initMins,
/* in */ int initSecs,
/* in */ ZoneType initZone )
: Time (initHrs, initMins, initSecs) // constructor initializer
// Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59
// 0 <= initSecs <= 59 && initZone is assigned
// Postcondition:
// zone == initZone && Time set by base class constructor
{
zone = initZone ;
}
![Page 18: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/18.jpg)
18
Implementation of ExtTime::Set function
void ExtTime :: Set ( /* in */ int hours,
/* in */ int minutes,
/* in */ int seconds,
/* in */ ZoneType time Zone )
// Precondition: 0 <= hours <= 23 && 0 <= minutes <= 59
// 0 <= seconds <= 59 && timeZone is assigned
// Postcondition:
// zone == timeZone && Time set by base class function
{
Time :: Set (hours, minutes, seconds);
zone = timeZone ;
}
![Page 19: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/19.jpg)
19
Implementation of ExtTime::Write Function
void ExtTime :: Write ( ) const
// Postcondition:
// Time has been output in form HH:MM:SS ZZZ
// where ZZZ is the time zone abbreviation
{
static string zoneString[8] =
{
“EST”, CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”
} ;
Time :: Write ( ) ;
cout << ‘ ‘ << zoneString [zone] ;
}
![Page 20: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/20.jpg)
20
often several program files use the same header file containing typedef statements, constants, or class type declarations--but, it is a compile-time error to define the same identifier twice
this preprocessor directive syntax is used to avoid the compilation error that would otherwise occur from multiple uses of #include for the same header file
#ifndef Preprocessor_Identifier
#define Preprocessor_Identifier . . .
#endif
Avoiding Multiple Inclusion of Header Files
![Page 21: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/21.jpg)
21
Composition (or Containment)
is a mechanism by which the internal data (the state) of one class includes an object of another class
![Page 22: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/22.jpg)
22
A TimeCard object has a Time object #include “time.h”
class TimeCard { public: void Punch ( /* in */ int hours,
/* in */ int minutes, /* in */ int seconds ) ; void Print ( ) const ;
TimeCard ( /* in */ long idNum,
/* in */ int initHrs,
/* in */ int initMins,
/* in */ int initSecs ) ; TimeCard ( ) ; private: long id ; Time timeStamp ; } ;
![Page 23: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/23.jpg)
23
TimeCard ClassTimeCard has a Time object
Private data:
hrs
mins
secs
Punch
Private data:
id
timeStamp
Increment
SetPrint . . .
TimeCard
TimeCard
Write . . .
![Page 24: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/24.jpg)
24
Implementation of TimeCard Class Constructor
TimeCard :: TimeCard ( /* in */ long idNum,
/* in */ int initHrs,
/* in */ int initMins,
/* in */ int initSecs )
: timeStamp (initHrs, initMins, initSecs) // constructor initializer
// Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59
// 0 <= initSecs <= 59 && initNum is assigned
// Postcondition:
// id == idNum && timeStamp set by its constructor
{
id = idNum ;
}
![Page 25: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/25.jpg)
25
Order in Which Constructors are Executed
Given a class X,
if X is a derived class its base class constructor is executed first
next, constructors for member objects (if any) are executed (using their own default constructors if none is specified)
finally, the body of X’s constructor is executed
![Page 26: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/26.jpg)
26
In C++ . . .
When the type of a formal parameter is a
parent class, the argument used can be:
the same type as the formal parameter,
or,
any descendant class type.
![Page 27: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/27.jpg)
27
Static Binding
is the compile-time determination of which function to call for a particular object based on the type of the formal parameter
when pass-by-value is used, static binding occurs
![Page 28: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/28.jpg)
28
Static Binding Is Based on Formal Parameter Type
void Print ( /* in */ Time someTime ){
cout << “Time is “ ;someTime.Write ( ) ;cout << endl ;
}
CLIENT CODE OUTPUT
Time startTime ( 8, 30, 0 ) ; Time is 08:30:00ExtTime endTime (10, 45, 0, CST) ; Time is 10:45:00
Print ( startTime ) ;Print ( endTime ) ;
![Page 29: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/29.jpg)
29
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
declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding
![Page 30: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/30.jpg)
30
Virtual Member Function
// SPECIFICATION FILE ( time.h )
class TimeType{
public :
. . .
virtual void Write ( ) const ; // for dynamic binding
. . .
private :
int hrs ; int mins ; int secs ;
} ;
![Page 31: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/31.jpg)
31
Dynamic binding requires pass-by-reference
void Print ( /* in */ Time & someTime ){
cout << “Time is “ ;someTime.Write ( ) ;cout << endl ;
}
CLIENT CODE OUTPUT
Time startTime ( 8, 30, 0 ) ; Time is 08:30:00ExtTime endTime (10, 45, 0, CST) ; Time is 10:45:00 CST
Print ( startTime ) ;Print ( endTime ) ;
![Page 32: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/32.jpg)
32
Using virtual functions in C++
dynamic binding requires pass-by-reference when passing a class object to a function
in the declaration for a virtual function, the word virtual appears only in the base class
if a base class declares a virtual function, it must implement that function, even if the body is empty
a derived class is not required to re-implement a virtual function. If it does not, the base class version is used
![Page 33: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/33.jpg)
33
Private data
value
ComparedTo
Initialize
class ItemType
An example: ItemType Class Interface Diagram
![Page 34: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/34.jpg)
34
Sorted list contains an array of ItemTypeIs this containment or inheritance?
SortedType class
IsFull
LengthIs
ResetList
DeleteItem
InsertItem
MakeEmpty
RetrieveItem
Private data:
length
info [ 0 ] [ 1 ] [ 2 ]
[MAX_ITEMS-1]
currentPos
GetNextItem
![Page 35: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/35.jpg)
35
class QueType<char>
QueType
~QueType
Enqueue
Dequeue . . .
Private Data:
qFront
qRear
‘C’ ‘Z’ ‘T’
![Page 36: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/36.jpg)
36
// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE
#include "ItemType.h" // for ItemType
template<class ItemType>
class QueType {
public:
QueType( ); // CONSTRUCTOR
~QueType( ) ; // DESTRUCTORbool IsEmpty( ) const;
bool IsFull( ) const;
void Enqueue( ItemType item );
void Dequeue( ItemType& item );
void MakeEmpty( );
private:
NodeType<ItemType>* qFront;
NodeType<ItemType>* qRear;
};
36
![Page 37: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/37.jpg)
3737
// DERIVED CLASS CountedQue FROM BASE CLASS QueType
template<class ItemType>
class CountedQue : public QueType<ItemType>
{
public:
CountedQue( );
void Enqueue( ItemType newItem );
void Dequeue( ItemType& item );
int LengthIs( ) const;
// Returns number of items on the counted queue.
private:
int length;
};
SAYS ALL PUBLIC MEMBERS OF QueType CAN BEINVOKED FOR OBJECTS OF TYPE CountedQue
![Page 38: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/38.jpg)
38
class CountedQue<char>
QueType
~QueType
Enqueue
Dequeue . . .
Private Data:
qFront
qRear
‘C’ ‘Z’ ‘T’
CountedQue
LengthIs
Enqueue
Dequeue . . .
Private Data:
length 3
![Page 39: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/39.jpg)
3939
// Member function definitions for class CountedQue
template<class ItemType>
CountedQue<ItemType>::CountedQue( ) : QueType<ItemType>( )
{
length = 0 ;
}
template<class ItemType>
int CountedQue<ItemType>::LengthIs( ) const
{
return length ;
}
![Page 40: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/40.jpg)
40
template<class ItemType>
void CountedQue<ItemType>::Enqueue( ItemType newItem )
// Adds newItem to the rear of the queue.
// Increments length.
{
length++;
QueType<ItemType>::Enqueue( newItem );
}
template<class ItemType>
void CountedQue<ItemType>::Dequeue(ItemType& item )
// Removes item from the rear of the queue.
// Decrements length.
{
length--;
QueType<ItemType>::Dequeue( item );
}40
![Page 41: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/41.jpg)
41
Example of using Protected#include <iostream.h>
class DynBase{ protected: int *barr; // pointer to base class dynamic array
public: DynBase () { cout << "Allocate 3 element DynBase array" << endl;
barr = new int[3]; }
![Page 42: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/42.jpg)
42
~DynBase () // not a virtual destructor { cout << "Delete 3 element DynBase array" << endl; delete [] barr; }};
![Page 43: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/43.jpg)
43
class DynDerived: public DynBase{ private: int *darr; // pointer to derived class dynamic array public: DynDerived () : DynBase() { cout << "Allocate 4 element DynDerived array" << endl;
darr = new int[4]; }
![Page 44: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/44.jpg)
44
~DynDerived () { cout << "Delete 4 element DynDerived array” << endl; delete [] darr; }};
![Page 45: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/45.jpg)
45
int main(){ DynBase *p = new DynDerived;
delete p; return 0;}
![Page 46: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/46.jpg)
46
/*Run: (DynBase destructor is not virtual):
Allocate 3 element DynBase arrayAllocate 4 element DynDerived arrayDelete 3 element DynBase array*/
![Page 47: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/47.jpg)
47
#include <iostream.h>
class DynBase{ protected: int *barr; // pointer to base class dynamic array
public: DynBase () { cout << "Allocate 3 element DynBase array" << endl;
barr = new int[3]; }
![Page 48: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/48.jpg)
48
virtual ~DynBase () // virtual destructor { cout << "Delete 3 element DynBase array" << endl;
delete [] barr; }};
![Page 49: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/49.jpg)
49
class DynDerived: public DynBase{ private: int *darr; // pointer to derived class dynamic array public: DynDerived () : DynBase() { cout << "Allocate 4 element DynDerived array" << endl;
darr = new int[4]; }
![Page 50: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/50.jpg)
50
~DynDerived () { cout << "Delete 4 element DynDerived array" << endl;
delete [] darr; }};
![Page 51: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/51.jpg)
51
int main(){ DynBase *p = new DynDerived;
delete p; return 0;}
![Page 52: 1 Chapter 6 Object-Oriented Software Development.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649d575503460f94a36881/html5/thumbnails/52.jpg)
52
/*Run: (DynBase destructor is virtual):
Allocate 3 element DynBase arrayAllocate 4 element DynDerived arrayDelete 4 element DynDerived arrayDelete 3 element DynBase array*/