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

Info icon This 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 <<(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;} M MyWord::MyWord(const MyWord&w):Word(w){size=w.size;} M int MyWord::length()const{return size;} i char & MyWord::operator[](int index)
Image of page 1

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

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