Broad Network


Creating a Simple Sequence Container in C++

Container Library Sequences in C++ Simplified – Part 3

Forward: In this part of the series, we create a simple sequence container in C++.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

It is now time to create a simple sequence container. This is the part 3 of my series, Containers Library Sequences in C++ Simplified. You must have read the first two parts before reading this part, since this is a continuation. In this part of the series, we create a simple sequence container in C++. Recall that a sequence container is a class template whose fundamental component is a list in dynamic memory. A sequence container is simply called a Sequence. The elements of the list of a sequence can be accessed using indices.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Properties of the Simple Sequence
The class template of the simple sequence container has just one property (data member). The property is a pointer to the first element of the list in the dynamic memory. A practical sequence will have a good number of properties.  

Methods of the Simple Sequence
The names of the methods (member functions) of the simple sequence container are:

- constructor
- obtainValue
- putValue
- addElement

We now look at the meaning of the methods.

constructor
Constructor is the constructor function. The name of the constructor is the name of the class (always like that). The constructor function does not have any returned value. It has two parameters (arguments): the first one is an int and it gives you the number of initial elements in the list. The second one is a template placeholder, which represents the object type in the elements of the list. With any template, the placeholder can be any of the fundamental types, any of the derived types, e.g. pointer, or instantiated objects.

obtainValue
This method has one argument, which is the index of an element of interest in the list. The function returns the value of the element.

putValue
This method has two arguments: the first one is an index to an element of interest. The second one is the value you want to give for the element. This value given, replaces whatever value was there, similar to what is done with arrays. The method does not return anything.

addElement
This method adds an element with a value at the end of the list. The method has one argument, which is the value. The method does not return anything. The method will use a simple iterator like the one we saw in the previous part of the series. This method assumes that you know the size of the list. You can write a method that will determine the size (length) of the list and then use the size in the addElement method; I will not go into that for this simple container.

Let us now look at the coding:

The Class Template Code Skeleton
This is the code skeleton for the class:

#include <iostream>
using namespace std;

template <class T>
class SimpleSqn
    {
        public:
            T *listPtr;

            SimpleSqn(int noIniCells, T defaultObj)
             {
             }
            T obtainValue(int indx)
                {
                }
            void putValue(int indx, T obj)
                {
                }
            void addElement(T obj)
                {
                }
    };


int main()
    {

        return 0;
    }

The skeleton is self-explanatory. Note that the name of the class is SimpleSqn. Also note that the type placeholder is T. For the rest of the article we shall look at the code of the segments. We shall also talk about the iterator that works with the addElement(T obj) method.

The Constructor Code
This is the constructor code:

            SimpleSqn(int noIniCells, T defaultObj)
               {
                    listPtr = new T[noIniCells]; //create list with number of initial elements
                    if (listPtr != NULL)  //was list created
                        {//if yes, put in the default values.
                            for (int i = 0; i < noIniCells; ++i)
                                {
                                    listPtr[i] = defaultObj;
                                }
                        }
                    else  //if list was not created, report
                       {
                           cout << "Initial list could not be created in dynamic memory.";
                       }
               }

The first line creates the list and assigns the list pointer from the operator, new (address of first element) to the property of the class template. Next, you have the if-construct. The if-condition checks if the list was actually created. If it was, the if-block fills the elements with the default value. If the list was not created, the else block reports.

The obtainValue Method
This method takes the index of an element in the list as argument and returns the value of the corresponding element using the array pointer of the list. This is the code:

            T obtainValue(int indx)
                {
                    return listPtr[indx];
                }

The putValue Method
This method takes the index of an element in the list and the new value as arguments. It uses the array pointer of the list to put the value in the corresponding element. This is it:

            void putValue(int indx, T obj)
                {
                    listPtr[indx] = obj;
                }

The addElement Method
This method adds a new element with its value at the end of the list. It takes only the value as argument. This is the code:

                {//assume the highest index is 4
                 listPtr[5] = obj;
                 Iter myIter(5, listPtr);
                 int *returnPtr = myIter.retrnPtr();
                 if (*returnPtr != obj)
                 {
                 cout << "Element could not be added in dynamic memory.";
                 }
                }

The code assumes that before the element is added there are five elements already in your list giving a maximum index of 4. So the element to be added has index 5. The first statement in the code does this. The next statement instantiates an iterator using the number of elements and the list pointer of the sequence. The statement following returns a pointer (different from the list pointer) that points to the supposed added element. This pointer is received by returnPtr. This is what the if-construct the follows does.

One way to know if the adding of the new element was successful is to check if *returnPtr was not the added element. That is what the if-construct does.

The Iterator
The iterator is exactly the same as the one we saw in the previous part of the series. So I will not talk about it here.

The complete code can be downloaded from the link below.

In the complete code, the iterator class description is typed before the class template description. If the reverse is done, the class template will not see the iterator.

We have just finished with this division. The next division will begin with the next part of the series.

Chrys

http://www.broad-network.com/ChrysanthusForcha/SimpleSqn-cpp.zip

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message