c109f05t1 - CSE 109 Test 1 Friday 7 October 2005

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

View Full Document Right Arrow Icon
CSE 109 Test 1 Friday 7 October 2005 <<<<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>. 1. The class Counter below is meant to model a hand held counter. It starts out at 0 and can be reset to 0. Each "click" increments the count by 1. Write a subclass XCounter which adds some functionality by allowing it to be incremented or decremented by numbers other than 1, using the += and -= operators. First I show the class Counter, and then I show how the code for XCounter could be used. class Counter {public: Counter(); void click(); //increment counter by 1 void reset(); //set counter to 0 int getCount()const; //return the count on the counter private: int count; }; //Sample code for XCounter XCounter ct; ct+=5; //increment counter by 5 ct-=3; //decrement counter by 2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< class XCounter : public Counter {public: XCounter(){} XCounter& operator+=(int n); XCounter& operator-=(int n); private: static void check(bool b, char *mess); }; XCounter & XCounter::operator +=(int n) {check(n>0,"Can't increment with 0 or negative n"); for(int j=0;j<n;j++) click(); return *this; } XCounter & XCounter::operator -=(int n) {int save;
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
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