Container Class
A container class is a class designed to
hold and organize multiple instances of another class. There are many different
kinds of container classes, each of which has various advantages,
disadvantages, and restrictions in their use. By far the most commonly used
container in programming is the array, which you have already seen many
examples of. Although C++ has built-in array functionality, programmers will
often use an array container class instead because of the additional benefits
it provides. Unlike built-in arrays, array container classes generally provide
dynamically resizing (when elements are added or removed) and do
bounds-checking. This not only makes array container classes more convenient
than normal arrays, but safer too.
Container
classes typically implement a fairly standardized minimal set of functionality.
Most well-defined containers will include functions that:
· Create an empty container (via a constructor)
· Insert a new object into the container
· Remove an object from the container
· Report the number of objects currently in the container
· Empty the container of all objects
· Provide access to the stored objects
· Sort the elements (optional)
Sometimes
certain container classes will omit some of this functionality. For example,
arrays container classes often omit the insert and delete functions because
they are slow and the class designer does not want to encourage their use.
Container
classes generally come in two different varieties.
Value containers are compositions
that store copies of the objects that they are holding (and thus are responsible for creating and
destroying those copies).
Reference containers are aggregations that store
pointers or references to other objects (and thus are not responsible for creation or destruction of those
objects).
Unlike in
real life, where containers can hold whatever you put in them, in C++,
containers typically only hold one type of data. For example, if you have an
array of integers, it will only hold integers. Unlike some other languages, C++
generally does not allow you to mix types inside a container. If you want one
container class that holds integers and another that holds doubles, you will
have to write two separate containers to do this (or use templates, which is an
advanced C++ feature). Despite the restrictions on their use, containers are
immensely useful, and they make programming easier, safer, and faster.
An array container class
In this
example, we are going to write an integer array class that implements most of
the common functionality that containers should have. This array class is going
to be a value container, which will hold copies of the elements its organizing.
First,
let’s create the IntArray.h file:
#ifndef
INTARRAY_H #define INTARRAY_H class IntArray
{
};
#endif
Our
IntArray is going to need to keep track of two values: the data itself, and the
size of the array. Because we want our array to be able to change in size,
we’ll have to do some dynamic allocation, which means we’ll have to use a
pointer to store the data.
#ifndef
INTARRAY_H #define INTARRAY_H class IntArray
{
private:
intm_nLength; int *m_pnData; };
#endif
Now we
need to add some constructors that will allow us to create IntArrays. We are
going to add two constructors: one that constructs an empty array, and one that
will allow us to construct an array of a predetermined size.
#ifndef
INTARRAY_H #define INTARRAY_H
class
IntArray
{
private:
intm_nLength; int *m_pnData; public: IntArray()
{
m_nLength
= 0; m_pnData = 0;
}
IntArray(intnLength)
{
m_pnData
= new int[nLength]; m_nLength = nLength;
}
};
#endif
We’ll
also need some functions to help us clean up IntArrays. First, we’ll write a
destructor, which simply deallocates any dynamically allocated data. Second,
we’ll write a
function
called Erase(), which will erase the array and set the length to 0. ~IntArray()
{
delete[]
m_pnData;
}
void
Erase()
{
delete[]
m_pnData;
// We need
to make sure we set m_pnData to 0 here, otherwise it will
// be left
pointing at deallocated memory!
m_pnData
= 0; m_nLength = 0;
}
Now let’s
overload the [] operator so we can access the elements of the array. We should
bounds check the index to make sure it’s valid, which is best done using the
assert() function. We’ll also add an access function to return the length of
the array.
#ifndef
INTARRAY_H #define INTARRAY_H
#include
<assert.h> // for assert() class IntArray
{
private:
intm_nLength; int *m_pnData; public: IntArray()
{
m_nLength
= 0; m_pnData = 0;
}
IntArray(intnLength)
{
m_pnData
= new int[nLength]; m_nLength = nLength;
}
delete[]
m_pnData;
}
void
Erase()
{
delete[]
m_pnData;
// We need
to make sure we set m_pnData to 0 here, otherwise it will
// be left
pointing at deallocated memory!
m_pnData
= 0; m_nLength = 0;
}
int&
operator[](intnIndex)
{
assert(nIndex>=
0 &&nIndex<m_nLength); return m_pnData[nIndex];
}
intGetLength()
{ return m_nLength; } };
#endif
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.