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