CSc 17  Test 2  20 July 2002
****************SUGGESTED ANSWERS*************************
1. (10 pts)  What is the output of the following program?
void a(int *p,int *&q)
{p[1]=5;
 q=p;
 p=new int(14);
}

int main()
{ int *x,*y,v[]={1,2,3,4},z[]={6,7,8,9};

  x=v;
  x[1]=0;
  for(int j=0;j<2;j++)
    cout<<v[j]<<"  "<<x[j]<<endl;
  y=z;
  a(x,y);
  for(int j=0;j<2;j++)
    cout<<x[j]<<"  "<<y[j]<<"  "<<z[j]<<endl;
  return 0;
}
--------------------------------------------
1  1
0  0
1  1  6
5  5  7
2.  (20 pts)  Given the DubNode class below write a function equal2() which
which returns whether a linked list of doubles has two consecutive values
which are identical.  If we have DubNode *head; and head points to a linked
list of doubles then equal2(head) will return true for the list
1.0->2.0->2.0->3.0 and false for the list 1.0->2.0->3.0.
class DubNode
 {public:
    DubNode(double x=0,DubNode *nx=NULL);
    double getData();
    void setData(double x);
    DubNode* next();
    void setNext(DubNode *nx);
  private:
    DubNode *nextDubNode;
    double data;
    static void assert(bool b,char *mess);
 };
------------------------------------------
 bool equal2(DubNode *hd);


 bool equal2(DubNode *hd)
  {while(hd!=NULL && hd->next()!=NULL && hd->getData()!=hd->next()->getData())
    hd=hd->next();
   return hd!=NULL && hd->next()!=NULL;
  }

3.  (20 pts) For class A below declare and define the copy constructor and the
  destructor.
class A
{public:
  A(int sz=0); //allocate array of int of size sz, if sz<=0 set data=NULL
 private:
  int *data,  //for array of data of size 'size'
  size;
  static void assert(bool b,char *mess); //if b==false, diagnose error and exit
};
--------------------------------------------
class A
{public:
  A(int sz=0); //allocate array of int of size sz, if sz<=0 set data=NULL
  A(const A&a);
  ~A();
 private:
  int *data,  //for array of data of size 'size'
        size;
  static void assert(bool b,char *mess); //if b==false, diagnose error and exit
};

A::A(const A&a)
{size=a.size;
 if(a.data==NULL)
   data=NULL;
 else
   {data=new int[size];
    assert(data!=NULL," (A(const A&)) Heap overflow");
    for(int j=0;j<size;j++)
     data[j]=a.data[j];
   }
}

A::~A()
{if(data!=NULL)
  delete []data;
}

4.  (25 pts) For the class B below declare and define the member function set()
which sets the boolean variable and the member function get() which returns the
boolean variable and over load && so that, e.g., B(true)&&B(false) returns
false, B(true)&&true returns true, true&&B(false) returns false, etc.
class B
{public:
  B(bool x=true);
 private:
   bool b;
};
----------------------------------------------
class B
{public:
  B(bool x=true);
  void set(bool x);
  bool get();
  friend bool operator&&(const B & x, const B & y);
 private:
   bool b;
};

void B::set(bool x)
{b=x;}

bool B::get()
{return b;}

bool operator &&(const B& x,const B &y)
{return x.b&&y.b;}

5.  (25 pts)  For the class C below overload "<<" and "+=" so that the
following code produces the output:   C(3)+=4 = C(7), C(7)+=C(3) = C(10)
  C x(3);
  cout<<x<<"+=4 = ";
  x+=4;
  cout<<x<<", "<<x<<"+=C(3) = ";
  x+=3;
  cout<<x<<;

class C
{public:
   C(int v=0);
 private:
  int d;
};
----------------------------------------------------------
class C
{public:
   C(int v=0);
   C&operator+=(const C&c);
   friend ostream&operator<<(ostream &out,const C&c);
 private:
   int d;
};

C&C::operator+=(const C&c)
{d+=c.d;
 return *this;
}

ostream&operator<<(ostream &out,const C&c)
{out<<"C("<<c.d<<")";
 return out ;
}
