// person.cc   Example base class to illustrate inheritance
// P. Conrad for CISC220, 06J

#include "person.h"


Person_C::Person_C(const char * const theFname,
		   const char * const theLname,
		   int theId)
{

  allocateAndCopy(fname,theFname);
  allocateAndCopy(lname,theLname);
  id = theId;

}

Person_C::Person_C(const Person_C & orig)
{

  allocateAndCopy(fname,orig.fname);
  allocateAndCopy(lname,orig.lname);
  id = orig.id;

}

Person_C & Person_C::operator =(const Person_C & right)
{
  // check for self-assignment

  if (&right == this)
    return (*this);

  // recycle old storage
  delete [] fname;
  delete [] lname;
  fname = lname = NULL;  // to avoid dangling refs

  // allocate new storage and copy values

  // Real code:
  // fname = allocateAndCopy(right.fname);
  // lname = allocateAndCopy(right.lname);

  // for TDD:
   allocateAndCopy(fname,"testing");
   allocateAndCopy(lname,"testing");

  id = 0;  /* temp for TDD */
  // id = right.id;
  
  // return reference to self to enable chaining (e.g. a = b = c;)

  return (*this);

}

Person_C::~Person_C()
{
  delete [] fname;
  delete [] lname;
  
  // next line not strictly needed, but helpful for debugging
  // makes sure that dangling refs cause a seg fault

  fname = lname = NULL; 
}


void Person_C::print(std::ostream & out) const
{
  out << id << " " << fname << " " << lname;
}

// the following illustrates that the parameters don't have to be
// right and left---here we use "where and what" instead.

std::ostream & operator << (std::ostream & where, 
			    const Person_C & what)
{
  what.print(where);
  return (where);
}




