You are here:

C++/testing the code


QUESTION: Hi again,
I did as you suggested and it is working now :)
could you help me to come up with a test program that tests the functions? I have made a test.cpp to test copy constructor or assignment operator and Iterator being nested to List but I don't know how to test the other functions such as operator== or operator!= or operator*,etc.
below I pasted both header and the test files for you to see in a paste bin since the code was getting too to paste here.
If you rather that I paste the code here, please let me know.

ANSWER: the paste bin seems to be a good idea. the code is much more readable that it would be if you place it as part of the allexperts question.

to test operator== or operator!= and operator*, you will have to write something like:

 List<string> personnel;

 personnel.add("first ");
 personnel.add("third ");

 // test operator* of iterator
 List<string>::Iterator iter = personnel.getFirst() ;
 for( int i = 0 ; i<6 ; ++i )
   std::cout << i << '.' << *iter << '\n' ;

 //test operator==, operator !=
 List<string>::Iterator iter_one = personnel.getFirst() ;
 List<string>::Iterator iter_two = iter_one ;
 std::cout << std::boolalpha ;

 ++iter_one ;
 ++iter_two ;
 std::cout << "iter_one => " << *iter_one << '\n' ;
 std::cout << "iter_two => " << *iter_two << '\n' ;
 std::cout << "iter_one == iter_two ? " << (iter_one==iter_two) << '\n' ;
 std::cout << "iter_one != iter_two ? " << (iter_one!=iter_two) << '\n' ;

 ++iter_one ; ++iter_one ;
 ++iter_two ; ++iter_two ; ++iter_two ;
 std::cout << "iter_one => " << *iter_one << '\n' ;
 std::cout << "iter_two => " << *iter_two << '\n' ;
 std::cout << "iter_one == iter_two ? " << (iter_one==iter_two) << '\n' ;
 std::cout << "iter_one != iter_two ? " << (iter_one!=iter_two) << '\n' ;

---------- FOLLOW-UP ----------

QUESTION: I'm not too familiar with  std::cout <<
I searched on line a bit and it seems like that is the same if I add usingnamespace std and then simply use cout<< right?
I also didn't understand this line:
std::cout << std::boolalpha ;
what is boolalpha?

ANSWER: everything in the C++ standard library resides in the namespace std.
std::cout, std::string, std::boolalpha and so on.

a using directive in your code
using namespace std ;
makes the compiler look into the namespace std to resolve unqualified names and hence just cout, string, boolalpha in your code would suffice.

std::boolalpha is a 'manipulator'.
std::cout << std::boolalpha ;
makes cout print boolean values as literals ( eg. 'true' and 'false') instead of as numbers ('1' and '0').

---------- FOLLOW-UP ----------

QUESTION: Thanks for another great and clear explanation.
As you probably noticed I am fairly new at programming.
in this example are Element and List both considered as container classes? could you explain?
also why do I need an Iterator class? just to go through the list objects? I mean what is the advantage of having the iterator class or iterator objects?

a 'container' or to be more precise, a 'sequence container' is a class that holds
a. a collection of elements, all of which are of the same type.
b. the elements form a sequence; ie. there is a notion of a first element, a second element and so on up to the last element.

the idea of an iterator is that it is possible to perform operations on different sequences in an identical manner. the iterator abstracts away the details of implementation of the sequence by
a. 'pointing to' (identifying) a particular element
b. allowing access the element that is pointed to
c. move from one element to the next element
d. test the position ( ==, !=  ) to see if we are 'pointing' to the same or different elements

as such, the List is like a container (though it not yet fully STL compliant).
the Iterator of the List is like a container iteraor (also not yet fully STL compliant).
the Element is merely an implementation detail of the List.

here is a very simple introductory tutorial on containers and iterators in the C++ standard library.  


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.