hw4spec - <?xml version="1.0"...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CS32 Homework 4, Winter 2010</title> </head> <body> <div align="right"> Winter 2010 CS 32 </div> <div align="center"> <h2>Homework 4</h2> <p> <strong>Time due: 9:00 PM Tuesday, March 2</strong> </p> </div> <ol> <li> <p> The files <a href="Sequence.h.txt">Sequence.h</a> and <a href="Sequence.cpp.txt">Sequence.cpp</a> contain the definition and implementation of Sequence implemented using a doubly-linked list. A client who wants to use a Sequence has to change the typedef in Sequence.h, and within one source file, cannot have two Sequences containing different types. </p><p> Change Sequence to be a class template, so that a client can say </p> <pre> #include "Sequence.h" #include <string> using std::string; ... Sequence<int> si; Sequence<string> ss; si.insert(9); ss.insert("District 9"); ... </pre> <p> Also, change <code>subsequence</code> and <code>interleave</code> to be function templates. </p><p> (Hint: Transforming the typedef-based solution is a mechanical task that takes five minutes if you know what needs to be done. What makes this problem non-trivial for you is that you haven't done it before; the syntax for declaring templates is new to you, so you may not get it right the first time. Have you looked at Chapter 8, pp. 419-426?) </p><p> (Hint: The template typename parameter doesn't have to be named <code>T</code>; it can be a name of your choosing. You might find that by choosing <code>ItemType</code> you'll have fewer changes to make.) </p><p> Because of most current compilers' limitations, the definition <em>and</em> implementation of your Sequence class template should be in just one file, Sequence.h, which is all that you will turn in for this problem. </p><p> There's a C++ language technicality that your compiler may or may not enforce.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
It relates to a type declared inside a class template, like <code>N</code> below: </p> <pre> template <typename T> class S { ... struct N { ... }; N* f(); ... }; </pre> <p> If we attempt to implement <code>f</code> this way: </p> <pre> template <typename T> S&lt;T>::N* S&lt;T>::f() // shouldn't compile { ... } </pre> <p> the technicality requires the compiler to not recognize <code>S<T>::N</code> as a type name; it must be announced as a type name this way: </p> <pre> template <typename T> typename S&lt;T>::N* S&lt;T>::f() // OK { ... } </pre> </li> <li> <p> Consider this program: </p> <pre> #include "Sequence.h" // class template from problem 1 class Complex { public: Complex(double r = 0, double i = 0) : m_real(r), m_imag(i) {} double real() const { return m_real; } double imag() const { return m_imag; } private: double m_real; double m_imag; }; int main()
Background image of page 2
{ Sequence<int> si; si.insert(50); // OK Sequence<Complex> sc; sc.insert(0, Complex(50,20)); // OK
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course COM SCI 32 taught by Professor Smallberg during the Spring '07 term at UCLA.

Page1 / 10

hw4spec - &lt;?xml version=&quot;1.0&quot;...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online