HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C++ PROGRAMMING - CONTAINERS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

A container is an object that holds instances of other objects. In analysis terms, containers represent the "hasA" relationship, as opposed to the "isA" relationship of inheritance.
Click here to be kept informed of our new Tutorials.


TUTORIAL TAKEN FROM COURSE : C++ PROGRAMMING

FULL COURSE DETAILS

Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more popular. This course introduces object oriented design and covers the whole syntax of C++ with lots of examples. The aim is to give thorough hands on experience to a small group so that the maximum benefit is obtained by each reader.

TO ACCESS THE FULL COURSE AND HUNDREDS OF OTHERS, CLICK HERE.


An example of a container might be an array of objects, or the stack class that we have met in various exercises.

Types of Container

A container that contains objects of a single type is said to be homogenous.

A container that contains objects of a variety of types is termed heterogeneous.

In C++, a heterogeneous container will usually contain objects that are derived from a common base class; this allows programs to iterate through the container using base class member functions.

Containers may store objects by reference or by value. If a container stores only copies of objects, that container is said to have value semantics. If on the other hand the container stores pointers to the objects that it is intended to store, the container is said to have reference semantics.

Containers with value semantics exhibit the following behaviour:

  • No object exists in two or more containers (there is no sharing).
  • Copies of objects are stored in the container. When an object is stored in the container a new object is created inside the container.
  • When the container is destroyed, the objects it contains are also destroyed.

Containers with reference semantics behave as follows:

  • An object may be "in" two or more containers, as each container contains only a reference to the object.
  • Placing an object into a container does not involve copying the object.
  • When the container is destroyed, the objects in the container are not destroyed.

There is a problem when an object that is in a reference container is destroyed. Unless some mechanism exists to notify all containers, the container will be left with a dangling reference.

The ideal container, from a programming point of view, is one which is homogenous, with value semantics. This form is directly supported by the use of C++ templates.

Containers with Iterators

An iterator allows clients of the container class to step through the objects in the container, one at a time. The iterator is usually defined as a friend class and declared as a separate object. Typical functions provided by the iterator class include:

 at_end to indicate when there are no more objects to be examined.
 current to return the current object in the container.
 first to return the first object in the container.
 last to return the last object in the container.
 next to advance the iterator to the next object in the container.
 restart to reset the iterator to the first object in the container.

This is not a definitive list. For example it only supports traversing the container in one direction.

When a container is changed by the addition or removal of an object, the iterator's position may be invalidated. Thus the container itself must be aware of all active iterator objects so that they may be notified. Similarly, if an iterator is able to change a container, it must notify all other iterators.




5 RELATED COURSES AVAILABLE
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Sunday 7th September 2008  © COPYRIGHT 2008 - VISUALSOFT