Sunteți pe pagina 1din 14

Object oriented

design
Introduction
min() and max() make no special assumptions about the storage of the array
elements
Therefore require that we examine each element.
we required the elements to be sorted, the implementation of both
operations would become a simple index: the first and last element.
There is no way for the user to override the more general implementations of
min(), max(), and find()
Cont..
// Unsorted, with no bounds-checking
class IntArray{ ... };
// Unsorted, with bounds-checking
class IntArrayRC{ ... };
// Sorted, without bounds-checking
class IntSortedArray{ ... };
Cont..
Drawbacks
We must maintain three array implementations containing considerable dup-
licated code.
The three array implementations are distinct types, we must write separate
functions to operate on them, although the general operations within the
functions may be identical.
void process_array( IntArray& );
void process_array( IntArrayRC& );
void process_array( IntSortedArray& );
Implementation
The object-oriented paradigm provides us with exactly these abilities. It is
provided by the inheritance mechanism.
When an IntArrayRC class (that is, an IntArray class with range-checking)
inherits from an IntArray class.
it has access to the data members and member functions of IntArray without
requiring that we maintain two copies of the code.
The new class need provide only the data members and member functions
necessary to implement its additional semantics.
Cont..
#include <IntArray.h>
void swap( IntArray &ia, int i, int j )
{
int tmp = ia[ i ];
ia[ i ] = ia[ j ];
ia[ j ] = tmp;
}
Cont..
IntArray ia;
IntArrayRC iarc;
IntSortedArray ias;
// ok: ia is an IntArray
swap( ia, 0, 10 );
// ok: iarc is a subtype of IntArray
swap( iarc, 0, 10 );
// ok: ias is also a subtype of IntArray
swap( ias, 0, 10 );
// error: string is not a subtype ...
string str( "not an IntArray!" );
swap( str, 0, 10 );
Cont..
One provider
Many user class.
Cont..
Virtual type dependent member function
Two equality operators and size() independent of type
Virtual during run time based on type basis
void init( IntArray &ia )
{
for ( int ix = 0; ix < ia.size(); ++ix )
ia[ ix ] = ix;
}
Cont..
Cont..
IntArrayRC needs to define only those aspects of its implementation that are
different or in addition to the implementation of IntArray.
1. It must provide its own instance of the subscript operator, one that
provides range-checking.
2. It must provide an operation to do the actual range-checking. (Because it
is not part of the public interface, we declare it to be private.)
3. It must provide its own set of automatic initialization functionsthat is, its
own set of constructors.
cont..
class IntArrayRC : public IntArray
inline int&
IntArrayRC::operator[]( int index )
{
check_range( index );
return ia[ index ];
}
Exception based design
Exceptions are run-time program anomalies -out-of-bounds array index, the inability to
open a specified file, the exhaustion of available program dynamic memory.
It supports a uniform syntax and style that supports fine-tuning by individual programmers
The primary components of the exception handling facility
1. When an exception is raised, normal program execution is suspended until the
exception is handled. In C++, the raising of an exception is carried out by a throw"
expression.
if ( ! infile ) {

string errMsg( "unable to open file: " );

errMsg += fileName;

throw errMsg;

}
Cont..
program exceptions are raised and handled in separate function or member
function invocations
Once the exception is handled, normal program execution resumes.
the handling of an exception is carried out by a catch clause.
catch( string exceptionMsg )
{
log_message( exceptionMsg );
return false;
}

S-ar putea să vă placă și