c109s04f - CSE 109 Final Examination Monday 10 May 2004...

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

View Full Document Right Arrow Icon
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 protected: char str[22]; //for storage of c-string }; {for(int j=0; j<w.length(); j++) w[j]=char(w[j]-'a'+'A'); } {for(int j=0; j<w.length(); j++) 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; size++; } MyWord::MyWord(char s):Word(s){size=1;} M MyWord::MyWord(const MyWord&w):Word(w){size=w.size;} M int MyWord::length()const{return size;} i char & MyWord::operator[](int index)
Background image of page 1

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

View Full DocumentRight Arrow Icon
{check(index>=0 & indexreturn str[index];} char MyWord::operator[](int index)const {check(index>=0 & indexreturn 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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 7

c109s04f - CSE 109 Final Examination Monday 10 May 2004...

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