CS 32 Homework 4

CS 32 Homework 4 - Winter 2009 CS 32 Homework 4 Time due:...

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

View Full Document Right Arrow Icon
Winter 2009 CS 32 Homework 4 Time due: 9:00 PM Tuesday, March 3 The files Set.h and Set.cpp contain the definition and implementation of Set implemented using a doubly-linked list. A client who wants to use a Set has to change the typedef in Set.h, and within one source file, cannot have two Sets containing different types. f Change Set to be a class template, so that a client can say b #include "Set.h" #include <string> using std::string; ... Set<int> si; Set<string> ss; si.insert(808); ss.insert("808s & Heartbreak"); ... . Also, change unite and subtract to be function templates. (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. 416- 423?) 4 Because of most current compilers' limitations, the definition and implementation of your Set class template should be in just one file, Set.h, which is all that you will turn in for this problem. o Your Set.h file must be such that this test program that we will try with your header must build and execute successfully: w #include "Set.h" #include <iostream> #include <string> #include <cassert> using namespace std; int main()
Background image of page 1

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

View Full DocumentRight Arrow Icon
{ Set<int> si; Set<string> ss; assert(si.empty()); assert(ss.size() == 0); assert(si.insert(10)); assert(ss.insert("hello")); assert(si.contains(10)); assert(si.erase(10)); string s; assert(ss.get(0, s) && s == "hello"); Set<string> ss2(ss); ss.swap(ss2); ss = ss2; unite(si,si,si); unite(ss,ss2,ss); subtract(si,si,si); subtract(ss,ss2,ss); cout << "Passed all tests" << endl; } There's a C++ language technicality that your compiler may or may not enforce. It relates to a type declared inside a class template, like N below: b template <typename T> class S { ... struct N { ... }; N* f(); ... }; } If we attempt to implement f this way: template <typename T> S<T>::N* S<T>::f() // shouldn't compile { ... } the technicality requires the compiler to not recognize S<T>::N as a type name; it must be announced as a type name this way: n template <typename T> typename S<T>::N* S<T>::f() // OK
Background image of page 2
{ ... } Consider this program: C #include "Set.h" // class template from problem 1 #include <string> using namespace std; class Bottle { public: Bottle(string c) : m_contents(c) {} Bottle() : m_contents("Empty") {} string contents() const { return m_contents; } private: string m_contents; }; int main() { Set<int> si; si.insert(7); // OK Set<Bottle> sb; sb.insert(Bottle("7-Up")); // error! } Explain in a sentence or two why the call to Set<Bottle>::insert causes at least one compilation error. (Notice that the call to Set<int>::insert is fine.) Don't just transcribe a compiler error message; your answer must indicate you understand the reason. An Internet domain has a
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 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 11

CS 32 Homework 4 - Winter 2009 CS 32 Homework 4 Time due:...

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