Solution 06
Home ] Up ]

 

//file  : Project06_CAddress04.cpp
//date  : 04/15/2002
//author: AOU

//What is new?
//  swap



#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>


const int TEST_COUNT = 5;


char *testFirstNames[] =
  {
  "Joe", "Tom", "Kim", "Ann", "Bob", "May", "Jay", "Dan"
  };
const int COUNT_FNAMES = sizeof(testFirstNames)/sizeof(testFirstNames[0]);

char *testLastNames[] =
  {
  "Smith", "Howard", "Ray", "Rather", "Letterman", "Leno"
  };
const int COUNT_LNAMES = sizeof(testLastNames)/sizeof(testLastNames[0]);

char *testStreets[] =
  {
  "123 Main St", "23 Broadway", "12 Joplin St", "34 Rouse Dr"
  };
const int COUNT_STREETS = sizeof(testStreets)/sizeof(testStreets[0]);

char *testCities[] =
  {
  "Pittsburg", "Parsons", "Joplin", "Miami", "Neosho"
  };
const int COUNT_CITIES = sizeof(testCities)/sizeof(testCities[0]);


char *testStates[] =
  {
  "KS", "MO", "OK", "AR", "TX"
  };
const int COUNT_STATES = sizeof(testStates)/sizeof(testStates[0]);

char *testZips[] =
  {
  "66762", "66921", "44234", "22312", "62123"
  };
const int COUNT_ZIPS = sizeof(testZips)/sizeof(testZips[0]);


class CAddress
  {
  private:
    char *m_firstName;
    char *m_lastName;
    char *m_street;
    char *m_city;
    char *m_state;
    char *m_zip;
    int   m_age;

  public:
    CAddress(void);
    CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age);
    CAddress(const CAddress &a);
    CAddress(char ch);
    ~CAddress(void);
    friend ostream & operator << (ostream &bob, const CAddress &a);
    CAddress & operator =(const CAddress &a);
    void swap(CAddress &a2);
  };


void testCAddressConstructorDefault(void);
void testCAddressConstructorValue(void);
void testCAddressConstructorCopy(void);
void testCAddressConstructorRandom(void);
void testCAddressOperatorAssign(void);
void testCAddressSwap(void);


void main(void)
  {
  srand(time(NULL));
  //testCAddressConstructorDefault();
  //testCAddressConstructorValue();
  //testCAddressConstructorCopy();
  //testCAddressConstructorRandom();
  //testCAddressOperatorAssign();
  testCAddressSwap();

  }


void testCAddressSwap(void)
  {
  cout << "testCAddressSwap\n";
  cout << "================\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CAddress a1('r'), a2('r');
    cout << "a1=\n";
    cout << a1 << endl;
    cout << "a2=\n";
    cout << a2 << endl;
    a1.swap(a2);
    cout << "After CAddress a1.swap(a2);\n";
    cout << "a1=\n";
    cout << a1 << endl;
    cout << "a2=\n";
    cout << a2 << endl;
    cout << "-------------------------\n";
    }
  }


void CAddress::swap(CAddress &a2)
  {
  CAddress temp;
  temp = *this;
  *this = a2;
  a2 = temp;
  }


void testCAddressConstructorCopy(void)
  {
  cout << "testCAddressConstructorCopy\n";
  cout << "===========================\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CAddress a2('r');
    cout << "a2=\n";
    cout << a2 << endl;
    CAddress a3(a2);
    cout << "After CAddress a3(a2);\n";
    cout << "a3=\n";
    cout << a3 << endl;
    cout << "-------------------------\n";
    }
  }


CAddress::CAddress(const CAddress &a)
  {
  this->m_firstName = new char[strlen(a.m_firstName)+1];
  strcpy(this->m_firstName, a.m_firstName);

  this->m_lastName = new char[strlen(a.m_lastName)+1];
  strcpy(this->m_lastName, a.m_lastName);

  this->m_street = new char[strlen(a.m_street)+1];
  strcpy(this->m_street, a.m_street);

  this->m_city = new char[strlen(a.m_city)+1];
  strcpy(this->m_city, a.m_city);

  this->m_state = new char[strlen(a.m_state)+1];
  strcpy(this->m_state, a.m_state);

  this->m_zip = new char[strlen(a.m_zip)+1];
  strcpy(this->m_zip, a.m_zip);

  this->m_age = a.m_age;
  }


void testCAddressOperatorAssign(void)
  {
  cout << "testCAddressOperatorAssign\n";
  cout << "==========================\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CAddress a1('r'), a2('r'), a3('r');
    cout << "a1 a2 a3\n";
    cout << a1 << endl << a2 << endl << a3 << endl;
    a1 = a3 = a2;
    cout << "After a1 = a3 = a2;\n";
    cout << "a1 a2 a3\n";
    cout << a1 << endl << a2 << endl << a3 << endl;
    cout << "-----------------------------------\n";
    }
  }


CAddress & CAddress::operator =(const CAddress &a)
  {
  m_firstName = new char[strlen(a.m_firstName) + 1];
  strcpy(m_firstName, a.m_firstName);

  m_lastName = new char[strlen(a.m_lastName) + 1];
  strcpy(m_lastName, a.m_lastName);

  m_street = new char[strlen(a.m_street) + 1];
  strcpy(m_street, a.m_street);

  m_city = new char[strlen(a.m_city) + 1];
  strcpy(m_city, a.m_city);

  m_state = new char[strlen(a.m_state) + 1];
  strcpy(m_state, a.m_state);

  m_zip = new char[strlen(a.m_zip) + 1];
  strcpy(m_zip, a.m_zip);

  m_age = a.m_age;

  return *this;
  }


CAddress::~CAddress(void)
  {
  //cout << "Destructor called\n";
  delete []m_firstName;
  delete []m_lastName;
  delete []m_street;
  delete []m_city;
  delete []m_state;
  delete []m_zip;

  m_firstName = NULL;
  m_lastName = NULL;
  m_street = NULL;
  m_city = NULL;
  m_state = NULL;
  m_zip = NULL;
  m_age = 0;
  }


void testCAddressConstructorRandom(void)
  {
  cout << "testCAddressConstructorRandom\n";
  cout << "=============================\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CAddress a('r');
    cout << "After CAddress a('r');\n";
    cout << a << endl;
    cout << "------------------------\n";
    }
  }


CAddress::CAddress(char ch)
  {
  if ('r' == ch || 'R' == ch)
      {
      int i;
      i=rand()%COUNT_FNAMES;
      this->m_firstName = new char[strlen(testFirstNames[i])+1];
      strcpy(this->m_firstName, testFirstNames[i]);

      i=rand()%COUNT_LNAMES;
      this->m_lastName = new char[strlen(testLastNames[i])+1];
      strcpy(this->m_lastName, testLastNames[i]);

      i=rand()%COUNT_STREETS;
      this->m_street = new char[strlen(testStreets[i])+1];
      strcpy(this->m_street, testStreets[i]);

      i=rand()%COUNT_CITIES;
      this->m_city = new char[strlen(testCities[i])+1];
      strcpy(this->m_city, testCities[i]);

      i=rand()%COUNT_STATES;
      this->m_state = new char[strlen(testStates[i])+1];
      strcpy(this->m_state, testStates[i]);

      i=rand()%COUNT_ZIPS;
      this->m_zip = new char[strlen(testZips[i])+1];
      strcpy(this->m_zip, testZips[i]);

	    this->m_age = rand()%50 + 17;
      }
    else if ('i' == ch || 'I' == ch)
      {
      }
    else
      {
      m_firstName = NULL;
      m_lastName = NULL;
      m_street = NULL;
      m_city = NULL;
      m_state = NULL;
      m_zip = NULL;
      m_age = 0;
      }
  }


void testCAddressConstructorValue(void)
  {
  cout << "testCAddressConstructorValue\n";
  cout << "============================\n";
  CAddress a2("John", "Smith", "123 Main St", "Pittsburg", "KS", "66762", 23);
  cout << a2 << endl;
  }


CAddress::CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age)
  {
  m_firstName = new char[strlen(fn) + 1];
  strcpy(m_firstName, fn);

  m_lastName = new char[strlen(ln) + 1];
  strcpy(m_lastName, ln);

  m_street = new char[strlen(sa) + 1];
  strcpy(m_street, sa);

  m_city = new char[strlen(ct) + 1];
  strcpy(m_city, ct);

  m_state = new char[strlen(st) + 1];
  strcpy(m_state, st);

  m_zip = new char[strlen(zp) + 1];
  strcpy(m_zip, zp);

  this->m_age = age;
  }


ostream & operator << (ostream &bob, const CAddress &a)
  {
  bob << "First Name: ";
  
  if (a.m_firstName != NULL) 
    bob << a.m_firstName; 

  bob << "\n Last Name: ";
  if (a.m_lastName != NULL) 
    bob << a.m_lastName;

  bob << "\n    Street: ";
  if (a.m_street != NULL) 
    bob << a.m_street;

  bob << "\n      City: ";
  if (a.m_city != NULL) 
    bob << a.m_city;

  bob << "\n     State: ";
  if (a.m_state != NULL) 
    bob << a.m_state;

  bob << "\n       Zip: ";
  if (a.m_zip != NULL) 
    bob << a.m_zip;

  bob << "\n       Age: ";
  if (a.m_age != 0)
    bob << a.m_age;

  bob << endl;

  return bob;
  }

 
void testCAddressConstructorDefault(void)
  {
  cout << "testCAddressConstructorDefault\n";
  cout << "==============================\n";
  CAddress a1;
  cout << a1 << endl;
  }


CAddress::CAddress(void)
  {
  m_firstName = NULL;
  m_lastName = NULL;
  m_street = NULL;
  m_city = NULL;
  m_state = NULL;
  m_zip = NULL;
  m_age = 0;
  }

/*OUTPUT:
testCAddressSwap
================
a1=
First Name: Dan
 Last Name: Smith
    Street: 23 Broadway
      City: Pittsburg
     State: AR
       Zip: 66762
       Age: 27

a2=
First Name: Joe
 Last Name: Leno
    Street: 23 Broadway
      City: Neosho
     State: AR
       Zip: 66762
       Age: 51

After CAddress a1.swap(a2);
a1=
First Name: Joe
 Last Name: Leno
    Street: 23 Broadway
      City: Neosho
     State: AR
       Zip: 66762
       Age: 51

a2=
First Name: Dan
 Last Name: Smith
    Street: 23 Broadway
      City: Pittsburg
     State: AR
       Zip: 66762
       Age: 27

-------------------------
a1=
First Name: May
 Last Name: Smith
    Street: 12 Joplin St
      City: Parsons
     State: KS
       Zip: 62123
       Age: 41

a2=
First Name: Tom
 Last Name: Smith
    Street: 23 Broadway
      City: Parsons
     State: KS
       Zip: 22312
       Age: 51

After CAddress a1.swap(a2);
a1=
First Name: Tom
 Last Name: Smith
    Street: 23 Broadway
      City: Parsons
     State: KS
       Zip: 22312
       Age: 51

a2=
First Name: May
 Last Name: Smith
    Street: 12 Joplin St
      City: Parsons
     State: KS
       Zip: 62123
       Age: 41

-------------------------
a1=
First Name: Tom
 Last Name: Rather
    Street: 123 Main St
      City: Parsons
     State: OK
       Zip: 66762
       Age: 64

a2=
First Name: Jay
 Last Name: Rather
    Street: 34 Rouse Dr
      City: Pittsburg
     State: TX
       Zip: 66921
       Age: 25

After CAddress a1.swap(a2);
a1=
First Name: Jay
 Last Name: Rather
    Street: 34 Rouse Dr
      City: Pittsburg
     State: TX
       Zip: 66921
       Age: 25

a2=
First Name: Tom
 Last Name: Rather
    Street: 123 Main St
      City: Parsons
     State: OK
       Zip: 66762
       Age: 64

-------------------------
a1=
First Name: Dan
 Last Name: Smith
    Street: 12 Joplin St
      City: Neosho
     State: OK
       Zip: 66921
       Age: 65

a2=
First Name: Dan
 Last Name: Letterman
    Street: 12 Joplin St
      City: Pittsburg
     State: TX
       Zip: 44234
       Age: 17

After CAddress a1.swap(a2);
a1=
First Name: Dan
 Last Name: Letterman
    Street: 12 Joplin St
      City: Pittsburg
     State: TX
       Zip: 44234
       Age: 17

a2=
First Name: Dan
 Last Name: Smith
    Street: 12 Joplin St
      City: Neosho
     State: OK
       Zip: 66921
       Age: 65

-------------------------
a1=
First Name: Jay
 Last Name: Smith
    Street: 23 Broadway
      City: Miami
     State: OK
       Zip: 22312
       Age: 21

a2=
First Name: Jay
 Last Name: Letterman
    Street: 12 Joplin St
      City: Neosho
     State: OK
       Zip: 66762
       Age: 33

After CAddress a1.swap(a2);
a1=
First Name: Jay
 Last Name: Letterman
    Street: 12 Joplin St
      City: Neosho
     State: OK
       Zip: 66762
       Age: 33

a2=
First Name: Jay
 Last Name: Smith
    Street: 23 Broadway
      City: Miami
     State: OK
       Zip: 22312
       Age: 21

-------------------------
Press any key to continue
*/