hw4 - Homework 4 Time due: 9:00 PM Tuesday, May 26 1. The...

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

View Full Document Right Arrow Icon
Homework 4 Time due: 9:00 PM Tuesday, May 26 1. The files Multiset.h and Multiset.cpp contain the definition and implementation of Multiset implemented using a doubly-linked list. A client who wants to use a Multiset has to change the typedef in Multiset.h, and within one source file, cannot have two Multisets containing different types. Change Multiset to be a class template, so that a client can say #include "Multiset.h" #include <string> using std::string; ... Multiset<int> msi; Multiset<string> mss; msi.insert(7); mss.insert("7-Up"); ... Also, change combine 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?) (Hint: The template typename parameter doesn't have to be named T ; it can be a name of your choosing. You might find that by choosing ItemType you'll have fewer changes to make.) Because of most current compilers' limitations, the definition and implementation of your Multiset class template should be in just one file, Multiset.h, which is all that you will turn in for this problem. 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: template <typename T> class M { ... struct N { ... }; N* f(); ... }; If we attempt to implement f this way: template <typename T> M<T>::N* M<T>::f() // shouldn't compile { ... } the technicality requires the compiler to not recognize M<T>::N as a type name; it must be announced as a type name this way:
Background image of page 1

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

View Full DocumentRight Arrow Icon
template <typename T> typename M<T>::N* M<T>::f() // OK; must use the keyword typename { ... } 2. Consider this program: 3. #include "Multiset.h" // class template from problem 1 4. 5. class Point 6. { 7. public: 8. Point(int x, int y) : m_x(x), m_y(y) {} 9. Point() : m_x(0), m_y(0) {} 10. double x() const { return m_x; } 11. double y() const { return m_y; } 12. private: 13. double m_x; 14. double m_y; 15. }; 16. 17. int main() 18. { 19. Multiset<int> msi; 20. msi.insert(42); 21. Multiset<Point> msp; 22. msp.insert(Point(42,13)); 23. } Explain in a sentence or two why the call to Multiset<Point>::insert causes at least one compilation error. (Notice that the call to Multiset<int>::insert is fine. Don't just transcribe a compiler error message; your answer must indicate you understand the reason. 24. A file has a name. A file is either a plain file (like a text file, an image file, a C++ source program, etc.) or a directory. Directories contain zero or more files. The following program reflects this structure: 25. #include <iostream> 26. #include <string> 27. #include <vector> 28. 29.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2012 for the course CS CS 32 taught by Professor Mojavi during the Spring '08 term at UCLA.

Page1 / 10

hw4 - Homework 4 Time due: 9:00 PM Tuesday, May 26 1. The...

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

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