0% found this document useful (0 votes)
59 views19 pages

Class 6: Dynamic Memory Allocation and Object Oriented Design

The document discusses dynamic memory allocation and object-oriented design. It describes how objects can be allocated statically by compilers or dynamically at runtime. The key difference is that static allocation is more efficient while dynamic is more flexible. It also covers pointers, classes, member functions, constructors, and information hiding as fundamental concepts of object-oriented programming in C++.

Uploaded by

Pradeep Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views19 pages

Class 6: Dynamic Memory Allocation and Object Oriented Design

The document discusses dynamic memory allocation and object-oriented design. It describes how objects can be allocated statically by compilers or dynamically at runtime. The key difference is that static allocation is more efficient while dynamic is more flexible. It also covers pointers, classes, member functions, constructors, and information hiding as fundamental concepts of object-oriented programming in C++.

Uploaded by

Pradeep Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Class 6

Dynamic Memory Allocation and Object oriented Design


DMA

objects can be allocated


statically
by the compiler as it processes our program source code
Dynamically
by a run-time library invoked during program execution
The primary trade-off between the two methods of memory allocation is
efficiency versus flexibility
Cont..

static memory allocation is considerably more efficient to allocate - it is


done prior to program start-up.
It is less flexible- it requires that we know prior to the execution of the
program the amount and type of memory that we need.
In general, the storage of an unknown number of elements requires the
flexibility of dynamic memory allocation.
int ival = 1024;
int ival2 = ival + 1;
Cont..

// a pointer to an object to type int


int *pint;
C++ - address-of operator (&) -
when applied to an object, returns that object's address value. Thus, to
assign
pint to ival's memory address, we write
int *pint;
pint = &ival; // assign pint address of ival
Cont..

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.

2. The allocation and deallocation of static objects is handled automatically


by the compiler.The allocation and deallocation of dynamic objects, in
contrast, must be managed explicitly by the programmer.
delete pint;
delete [] pia;
Object Based design

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..

Ability to specify the size.


Initialize the set of values.
To provide access via an index.
class classname {
public:
// the public set of operations
private:
// the private implementation
};
Cont..

// a single IntArray class object


IntArray myArray;
// a pointer to a single IntArray class object
IntArray *pArray = new IntArray;
A class definition consists of two parts: the class head, composed of the
keyword class and an associated class name, and
the class body - enclosed by braces and terminated with a semicolon. The
class head, by itself, serves as a declaration of the class
class IntArray;
Cont..

Class body contains both public and private declarations.


Cont..

A named member function, such as min(), is invoked using one of two


member access operators. There are actually two operators: a
dot operator (.) for class objects
an arrow operator ( ->) for pointers to class objects.
For example, to find the minimum value within our myArray class object
// initialize min_val with the smallest element within myArray
int min_val = myArray.min();
int max_val = pArray->max();
Cont..

IntArray myArray0, myArray1;


the assignment operator is applied as follows:
// invokes the copy assignment member function:
// myArray0.operator=( myArray1 )
myArray0 = myArray1;
// invokes the equality member function:
// myArray0.operator==( myArray1 )
if ( myArray0 == myArray1 )
cout "!!our assignment operator works!\n";
Cont..

The division between the public interface and private implementation of a


class is referred to as information hiding.
If the private implementation of the class needs to be modified or extended,
only the relatively small number of member functions requiring access to that
implementation needs to be modified
If there is an error in the state of the private class implementation, the
amount of program code we need to examine in general is localized to the
relatively small number of member functions requiring access to that
implementation;
The entire program need not be examined.
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..

C++ supports a facility known as function overloading. Function overloading


allows the same name to be used for two or more functions.
a unique parameter list, either by the number or the types of the parameters.
For example, the following is a valid set of min() overloaded functions.
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 )

// set the data members

_size = sz;

ia = new int[ _size ];

// initialize the memory

for ( int ix=0; ix < _size; ++ix )

ia[ ix ] = 0;

}
Cont..

Constructors three types:


Initialize a value
Initialize an array
Initialize a copy value using .rhs operator

Similar way we have destructor for deleting of allocated memories.

You might also like