Class 6: Dynamic Memory Allocation and Object Oriented Design
Class 6: Dynamic Memory Allocation and Object Oriented Design
To access the actual object pint addresses, we must first dereference pint
using the dereference operator (*).
Example
how we would indirectly add 1 to ival through pint:
// indirectly adding 1 to ival through pint
*pint = *pint + 1;
Cont..
The two primary differences between static and dynamic memory allocation
1. Static objects are named variables that we manipulate directly, whereas
dynamic objects are unnamed variables we manipulate indirectly through
pointers.
Our array class is to have some self-knowledge built into its implementation.
As a first step, it will know its size.
Our array class is to support the assignment of one array with another and the
comparison of two arrays for both equality and inequality.
Our array class is to support the following queries as to the values contained
within it. What is the minimum value contained within the array?
What is the maximum value? Does a specific value occur within the array,
and, if so, what is the index of its first position?
Ability to sort itself
Cont..
class IntArray
{
public:
// ...
int size() const { return _size; }
private:
// internal data representation
int _size;
int *ia;
};
Cont..
For loop
IntArray array;
public//..
int array_size = array.size();
private//..
int array_size = array._size;
for ( int index = 0; index < array.size(); ++index )
for ( int index = 0; index < array._size; ++index )
Cont..
Constructors
class IntArray {
public:
explicit IntArray( int sz = DefaultArraySize );
IntArray( int *array, int array_size );
IntArray( const IntArray &rhs );
// ...
private:
static const int DefaultArraySize = 12;
// ...
If value not declared
IntArray array1( 1024 );
Cont..
IntArray::
IntArray( int sz )
_size = sz;
ia[ ix ] = 0;
}
Cont..