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.  For example, if the executable file
    is p3, then
       p3
    would produce the output  "The sum is 0"
       p3  1 A 3 B
    would produce the output  "The sum is 25"
-------------------------------------------------
int main(int ct,char *arg[])
{    int sum,val;
     sum=0;
     for(int j=1; j<ct;j++)
      {if(arg[j][0]>='0' && arg[j][0]<='9')
         val=arg[j][0]-'0';
       else
         val=arg[j][0]-'A'+10;
       sum+=val;
      }
      cout<<"The   sum is "<<sum<<endl;
}
4.  Below is the contents of my hpstack.h.  Add the declaration(s) and
    code needed so that the function swap(), swaps the top two entries
    on the stack.  In particular, the code
      HPStack h;
      h.push(2).push(3).swap();
      cout<<"The stack: "<<h<<endl;
    produces the output:  "The stack: [-->0-->0-->0-->0-->3-->2]"
    class HPStack
    {public:
      HPStack();
      HPStack(const HPStack & hp);
      HPStack& push(int n);
      int pop();
      int peek()const;
      HPStack & add();
      HPStack & sub();
      HPStack & mult();
      HPStack & div();
      friend ostream &operator<<(ostream & out,const HPStack &hp);
     private:
      int top, stack[40];
    };
------------------------------------------------
   void swap();
   void HPStack::swap()
   {int p1,p2;
    p1=pop();
    p2=pop();
    push(p1);
    push(p2);
   }
5.  Below are copies of bintree.h (which neither declares nor implements the
    methods for iteration) and rek.h. Write the code for inTree() which returns
    whether a given Key is in the tree.
class BinTree
{public:
  static const int LEFT=0, RIGHT=1,UP=0, DOWN=1,
            PREORDER=0, INORDER=1, POSTORDER=2;
  BinTree();
  BinTree(const BinTree&b);
  ~BinTree();
  BinTree&operator+=(const Rek &r);
  Rek & operator[](const Key &k);
  Rek operator[](const Key &k)const;
  bool inTree(const Key &k);
  void display(ostream & out=cout,int direction=0,int order=1,char *indent="");
 private:
  class Node
    {public:
        Node(const Rek & r,Node *left=NULL,Node *right=NULL);
        Rek entry;
        Node *child[2];
    };
  void copy(Node*oldRt,Node*&rt);
  void destroy(Node *&rt);
  void add(Node*&rt,const Rek&r);
  void display(ostream & out,Node *rt,int dir,int order,
        char *indent,int deep);
  void show(ostream &out,const Rek &r,bool doshow,int deep,
          char *indent);
  static void check(bool b,char *mess);
  Node *root;
};

typedef int Key;
class Rek
 {public:
    Rek();
    Rek(Key k,int addr=0,int val=0);
    Key key()const;
    int getValue();
    void setValue(int v);
    void setAddr(int a);
    int getAddr();
    friend ostream & operator<<(ostream &out,const Rek &r);
    private:
      Key keyVal;
      int value,address;
 };
---------------------------------------------
bool BinTree::inTree(const Key &k);
 {return inTree(k,root);}
bool inTree(const Key &k, Node * rt);
bool BinTree::inTree(const Key &k,Node * rt)
{if(rt==NULL)
   return false;
 else return rt->entry.key()==k
                || inTree(k,rt->child[rt->entry.key()<k]);
 //if no order assumed:
 //    return rt->entry.key()==k ||
 //     inTree(k,rt->child[LEFT]) || inTree(k,rt->child[RIGHT]);
}

