c17s98f - CSc 17 Final Examination Page 1 Friday 15 May...

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

View Full Document Right Arrow Icon
CSc 17 Final Examination Friday 15 May 1998 8-11AM Page 1 >>>>>>>>>>>>>>>>>>>>ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. (25 pts) One should never retrieve the contents of a variable before one has stored a value in the variable. An ADT SafeVar could prevent this error. Consider the ADT SafeChar, with the following properties: (1) A character can be stored in a SafeChar; (2) The value of SafeChar can be retrieved only after (1) has occurred otherwise an error occurs. Write a class SafeChar which implements this ADT, i.e., write both the declaration and the definitions (implementation). class SafeChar {public: SafeChar(); char get(); void put(char ch); private: char kh; bool stored; }; SafeChar::SafeChar() {stored=false;} void SafeChar::put(char ch) {kh=ch; stored=true;} char SafeChar::get() {if(!stored) {cout<<"Error: tried to access undefined value.\n" <<"Press <enter> to quit program .... "; cin.get(); } else return kh; } 2. (10 pts) Expand the above specification of the ADT so that it allows input and output. (1) The same as above. (2) Read from a file into SafeChar. (3) Retrieve a char from SafeChar only after either (1) or (2) has occurred, else an error.
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