c109s04t1 - CSE 109 Test 1 Wednesday 25 February 2004 8:10...

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 Wednesday 25 February 2004 8:10 AM >>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<< 1. Assume the class A below. Write the declaration and code for the subclass, B, of A, which enables a user to set the value of k with setK() and to get the value of k with getK(). In particular, the following code should produce the output "The value is 6". B x(2,3); x.setK(6); cout<<"The value is "<<x.getK()<<endl; class A {public: A(int n=0,int p=0):k(n),q(p){} protected: int k,q; }; ----------------------------------- class B:public A {public: B(int n=0,int p=0); void setK(int p); int getK(); }; B::B(int n=0,int p=0):A(n,p){} void B::setK(int p){k=p;} int B::getK(){return k;} 2. Assume class A above. Add the declaration(s) and code needed to implement the operator +=, which increments k by some amount. In particular the code B x(10,3); x+=5; cout<<"The value is "<<x.getK()<<endl; would produce the output "The value is 15". ------------------------------------------------ A& operator+=(int p); A& A::operator+=(int p) {k+=p; return *this; } 3. Write a program that assumes the command line contains 0 or more arguments, each in the form of a hexadecimal digit and writes the sum of the values represented.
Image of page 1

Info iconThis 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