{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–4. 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:
Image of page 1

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

View Full Document Right 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. using namespace std; 30. 31. class File 32. { 33. public: 34. File(string nm) : m_name(nm) {} 35. virtual ~File() {}; 36. string name() const { return m_name; }
Image of page 2
37.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern