Introduction into Object-Related Programming with C++
A prerequesite for this tutorial is deeper knowledge of C.
This tutorial should help one to understand the basis of object related programming. The pros and cons will be outlined and discussed with an example for an implementation. This lesson implements an interface for the STL vector container object, as this object suits very well for implementing functions to handle this STL vector. Further explanations of the STL vector class can be found here . In general, ob ject related programming offers structuring of a problem to the programmer. Due to this fact this kind of coding is very modular and therefore very extendible. The object related programming suits best for data related problems. On the other side we will see that this kind of coding does not suit best for implementing algorithms.
Ingredients
In this lesson the following data are required:
![]() | source code demo file main.cpp
|
![]() | source code class demo header file container.hh
|
![]() | source code class demo file container.cpp |
Tasks
- Open the three demofiles and read them.
- Implement the functionbody for the push() function within container.cpp.
- Implement the functionbody for the getSize() function within container.cpp.
- Implement the functionbody for the getElement() function within container.cpp.
- Implement the functionbody for the printElements() function within container.cpp.
- Implement an algorithm for sorting the elements of the vector within the bubbleSort() function within container.cpp.
- What is the disadvantage of defining an algorithm within an object?
Hints
- The methods and variables of a class are available from the moment of instantiation to the moment of destruction. Due to this we are able to store data within an ob ject.
- As the push function is a member function of the container class, we have access to the private vector ob ject of the class. The push back(value) function takes one parameter(value) of the designated type and adds it to the end of a STL vector. It is important that you only add parameters to the vector which are of the same type as the STL vector, otherwise a compiler error will occure.
cont.push back(element);
- We are able to return a value to the caller by the return keyword.
return cont.size();
- As we have the element position passed to, we are able to simply make use of the
at() function.
return cont.at(i);
- Create an STL - random access iterator - which is capable of traversing a STL vector of type double.
std::vector<double>::iterator iter;
Traverse the elements of the vector by the use of this iterator
for ( iter = cont.begin(); iter != cont.end(); ++iter )
and print the elements by dereferentiating
std::cout << *iter << std::endl;.
- The bubble sort algorithm compares 2 direct neighbours if the right neighbour is lesser or greater than the left neighbour. If the right one is lesser than the left neighbour, swap positions. If the right one is greater than the left one, no action is required. Traverse each element of the vector and make use of this algorithm. Repeat the whole procedure as soon as there is a minimum of one swap during one traversal. Use recursion to minimize code lines. Recursion means to call the function out of the same function which is in use. In other words: The function calls itself.
- keyword: accessability - You are only able to get access to the algorithm by creating an ob ject. The algorithm for itself is bound to the ob jects lifetime. We use the keyword encapsulation for this fact. As the algorithm is encapsulated within the ob ject. In scientific computation we do not want to encapsulate the algorithm. As a result of this scientific programmers have to combine this kind of coding with other ones e.g. generic programming.
