CSC 17 Final Examination SPRING 1998

CSC 17 Final Examination SPRING 1998 - CSc 17 Final...

Info iconThis 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. (4) Write the contents of SafeChar to a file only after either (1) or (2) has occurred, else an error. 3.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/05/2008 for the course CSC 017 taught by Professor Edkay during the Spring '08 term at Lehigh University .

Page1 / 4

CSC 17 Final Examination SPRING 1998 - CSc 17 Final...

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

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