CSE 9B
Final Review Information


      OH    *UPDATED* EXTRA Final Exam Office Hours (Smarx APM 2331):
            Fri 6/6 1-4pm,    Mon 6/9 1-4pm,   Tue 10-11 6/10
The following are the review problems for the final exam.

I.  OVERLOADED OPERATORS/this POINTER

a) Write in the overloaded operator>> function definition

b) Why do you have to WRITE an overloaded operator>> function

c) What if the overloaded operator<< function was not a friend?    

d) In this code "if (this == &rd) return  *this;", what does this and *this
   refer to in the following statement?

     young = young;

#include <iostream.h>
class Age
{
   int years;
  public:
    Age( ) { years = 20; }
    Age& operator= ( const Age& ra ); 
    friend ostream& operator<< (ostream &os, const Age& ra)
           {os << ra.years; return os;}
                    operator>> (                     ) {                     }
};

Age& Age::operator= (const Age& ra)
{
  if (this == &ra)   return *this;
  else   years = ra.years;  return *this; 
} 

int main ( void )
{
  Age young, old;
  cout << "Enter your age: ";
  cin >> young;
  old = young;
  cout << old << endl;
  return 0;
}

----------------------------------------------------------------------------

2) Describe these function declarations.

 float fn ( int &  ri );
 e.g. fn is a function returning a float and being passed a reference to an
      int.
 int & fn ( float );
 inline double fn ( void );
 void fn ( int * );

----------------------------------------------------------------------------

3) Describe these member/friend function declarations.

class Age
{
   int years;
  public:
   Age(void);
   Age(int i = 0);
   Age(int i);
   Age(Age &ra);
   Age(const Age &ra);
   Age count ( void ) { years--; }
   Age operator- ( const Time & );
   Age & operator-= ( const Time );
   friend Age minus ( int, Time & );
   friend Age operator- ( int &, const Time & );
};

============================================================================

II.  POINTERS and DYNAMIC ARRAYS / STORAGE CLASSES 

a) Are there compiler errors if you didn't write the Copy constructor?

b) If you didn't write the Copy constructor, where is the problem?

c) If it was "char p[20];" vs "char *p;" do we need to write a Copy
   constructor?

d) Which part of memory (STACK, HEAP, DATA, or TEXT) is the:
   variable  len stored?
   variable  s   stored?
   value    *s   stored?

extern "C"
{
#include <string.h>
}
#include <iostream.h>
class Strg
{
 private:
   char *s;
   int   len;
 public: 
   Strg(void){ len = 5; s = new char[len + 1]; strcpy(s, "Hello"); }
   Strg(Strg &r){ len = strlen(r.s); s = new char[len + 1]; strcpy(s, r.s); }
   ~Strg() { delete [] s; } 
   friend ostream& operator<<(ostream &os, const Strg& r){os<<r.s; return os;}
};

void fun ( Strg s ) { cout << s << endl;  }

int main ( void )      
{
  Strg s1 ;
  fun( s1 );
  cout << s1 << endl;
  return 0;
}

============================================================================

III. RECURSION (Ch 12)

1) What gets printed? 

#include <iostream.h>
int fn ( int n )
{
  if ( n < 2 )
    return n;
  else
    return ( n + fn ( n - 1 ) );
}

int main ( void )      
{
  int i;
  for ( i = 0 ; i < 5 ; ++i )
    cout << fn(i) << endl;
   return 0;
}

----------------------------------------------------------------------------

2) What is printed if you type in the input:   12345ABC

extern "C"
{
#include <ctype.h>
}
#include <iostream.h>
void  fn ( void )
{
  char ch;
  if ( isdigit(ch = cin.get()) )
    fn();
  cout << ch;
}
 
int main ( void )      
{
  fn();
  return 0;
}

============================================================================

IV. POINTERS and LINKED LISTS 

a) What if loop was like this:  for( p = &head; p ; p = q ){..}

b) What if void insert_node() was like this:
   void insert_node ( Node *prev, Node *add )
   {
      prev->next = add;
      add->next = prev->next;
   }

c) What gets printed? 

#include <iostream.h>
struct Node
{
  int    value;
  Node  *next;
};

void insert_node ( Node *prev, Node *add )
{
  add->next = prev->next;
  prev->next = add;
}

int main ( void )      
{
  int   i;
  Node head, *p = &head, *q;
  head.next = 0;
  for ( i = 0 ; i < 4 ; ++i )
  {
    q = new Node;
    q->value = i ;
    insert_node ( p, q );
    p = q;
  }
  for( p = head.next; p ; p = q )
  {
    cout << p->value << endl;
    q = p->next;
    delete p;
  }
  return 0;
}

Home || Course Description || Course Outline || Lab Hours || FAQ


Webmaster: Leslie Franklin <franklin@cs.ucsd.edu>
Last Updated: June 3, 1997