c109s03t2 - CSE 109 Test 2 Wednesday 9 April 2003...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Test 2 Wednesday 9 April 2003 <><><><><><><><><>SUGGESTED ANSWERS<><><><><><><><><>><><><><><> 1. An Association is a pair consisting of a "key" and a "value." Write the template class for Association so that the following code compiles and produces the output indicated. The class need have no other functionality. Association <int,double> a(2,2.7); Association <Word, Word> b("hope","esperanza"); a.setValue(2.3); cout<<x<<" "<<b<<endl; // displays: {2, 2.3} {hope, esperanza} ================================================================ #include <fstream.h> # template <class KEY, class VALUE> class Association {public: Association(const KEY &k,const VALUE &v); void setValue(const VALUE &v); template <class K, class V> friend ostream & operator <<(ostream &out,const Association<K,V> & a); private: KEY key; VALUE value; }; } template <class KEY, class VALUE> Association<KEY,VALUE>::Association(const KEY &k,const VALUE &v) :key(k),value(v){} template <class KEY, class VALUE> void Association<KEY,VALUE>::setValue(const VALUE &v) {value=v;} template <class KEY, class VALUE> ostream &operator <<(ostream &out,const Association<KEY,VALUE> &a) {out<<"{"<<a.key<<", "<<a.value<<"}"; return out;} 2. Assume you are developing a parser for telephone numbers, e.g.,
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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