Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
32 <strong>Programming</strong> with the STL<br />
An outline of the class (BitsetStorage is the type of the word that contains the bits):<br />
class BitReference {<br />
public:<br />
BitReference(Bitset::BitStorage* pb, size_t p)<br />
: p_bits(pb), pos(p) {}<br />
// Operations will be added later<br />
protected:<br />
Bitset::BitStorage* p_bits; // pointer to the word containing bits<br />
size_t pos; // position of the bit in the word<br />
};<br />
Now, operator[] in class Bitset may be defined as follows:<br />
BitReference operator[](size_t pos) {<br />
return BitReference(&bits, pos);<br />
}<br />
The actual bit fiddling is performed in the BitReference class. In order to see what we need to<br />
implement in this class, we must study the results of some expressions involving operator[]:<br />
bs[3] = true; // bs.operator[](3) = true; =><br />
// BitReference(&bs.bits,3) = true; =><br />
// BitReference(&bs.bits,3).operator=(true);<br />
From this follows that we must implement the following operator function in BitReference:<br />
BitReference& operator=(bool x); // for bs[i] = x<br />
This function should set the bit referenced by the BitReference object to the value of x (just like<br />
the set function in the original Bitset class). When we investigate (do this on your own) other<br />
possibilities to use operator[] we find that we also need the following functions:<br />
BitReference& operator=(const BitReference& bsr); // for bs[i] = bs[j]<br />
operator bool() const; // for x = bs[i]<br />
A4. Use the files bitset.h, bitset.cc, bitreference.h, bitreference.cc, and bitsettest1.cc. Implement<br />
the functions in bitreference.cc and test. It is a good idea to execute the program line<br />
by line under control of gdb, even if your implementation is correct, so you can follow<br />
the execution closely and see what happens (turn off the -O2 switch on the compilation<br />
command line if you do this).<br />
2.3 Iterators<br />
From one of the OH slides: “An iterator “points” to a value. All iterators are DefaultConstructible<br />
and Assignable and support ++it and it++.” A ForwardIterator should additionally be<br />
EqualityComparable and support *it, both for reading and writing via the iterator.<br />
The most important requirement is that an iterator should point to a value. A BitsetIterator<br />
should point to a Boolean value, and we already have something that does this: the class<br />
BitReference! The additional requirements (++, equality test, and *) can be implemented in a<br />
derived class. It will look like this: 11<br />
11 This class only contains the constructs that are necessary for the bitset test program. For example, we have not<br />
implemented postfix ++ and comparison with ==.