CSE 109 Final Examination SPRING 2004

CSE 109 Final Examination SPRING 2004 - CSE 109 Final...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: CSE 109 Final Examination Monday 10 May 2004 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<< 1. Below is the declaration of class Word. Write the declaration and definitions (code) for a subclass of Word called MyWord such that the functions below work as indicated. class Word {public: Word(char *st=""); Word(char s); Word(const Word & w); ~Word(); friend bool operator<(const Word &a, const Word &b); friend bool operator<=(const Word &a, const Word &b); friend bool operator>(const Word &a, const Word &b); friend bool operator>=(const Word &a, const Word &b); friend bool operator==(const Word &a, const Word &b); friend bool operator!=(const Word &a, const Word &b); Word operator=(const Word &w); friend ostream & operator <<(ostream &out,const Word &w); protected: char str[22]; //for storage of c-string }; void upcase(MyWord &w)//make all lower case characters in w uppercase {for(int j=0; j<w.length(); j++) if(w[j]>='a' && w[j]<='z') w[j]=char(w[j]-'a'+'A'); } void upAndOut(const MyWord & w)//display w in uppercase letters {for(int j=0; j<w.length(); j++) if(w[j]>='a' && w[j]<='z') cout<<char(w[j]-'a'+'A'); } void test() // test the constructors {MyWord v('a'),r("hello"), x(r); x[0]='H'; cout<<v<<r<<x<<endl;//display "ahelloHello"} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> class MyWord:public Word {public: MyWord(char *st=""); MyWord(char s); MyWord(const MyWord&w); int length()const; char & operator(int index); char operator(int index)const; private: int size; static void check(bool b, char *mess); }; MyWord::MyWord(char *st):Word(st){size=0; while(size<22 && str[size]!='\0') size++; } MyWord::MyWord(char s):Word(s){size=1;} MyWord::MyWord(const MyWord&w):Word(w){size=w.size;} int MyWord::length()const{return size;} char & MyWord::operator(int index) {check(index>=0 && index<size,"Index out of range"); return str[index];} char MyWord::operator(int index)const {check(index>=0 && index<size,"Index out of range"); return str[index];} void MyWord::check(bool b, char *mess) {if(!b) {cerr<<"ERROR: "<<mess<<endl; exit(1); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write a program that reads a line of text from cin and determines whether it obeys the syntax for Exp given by the syntax diagrams below. It should display "Good syntax" if the line is correct and "Bad syntax if the line is incorrect. Note represents a rectangle, and () a circle. incorrect....
View Full Document

This note was uploaded on 05/05/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 8

CSE 109 Final Examination SPRING 2004 - CSE 109 Final...

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