|
//file : CAddress03.cpp //date : 04/12/2002 //author: AOU #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", "6692", "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); friend ostream & operator << (ostream &bob, const CAddress &a); }; void testCAddressConstructorDefault(void); void testCAddressConstructorValue(void); void testCAddressConstructorCopy(void); void testCAddressConstructorRandom(void); void main(void) { srand(time(NULL)); testCAddressConstructorDefault(); testCAddressConstructorValue(); testCAddressConstructorCopy(); testCAddressConstructorRandom(); } void testCAddressConstructorRandom(void) { cout << "testCAddressConstructorRandom\n"; cout << "=============================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a1('r'); cout << a1 << 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 { this->m_firstName = NULL; this->m_lastName = NULL; this->m_street = NULL; this->m_city = NULL; this->m_state = NULL; this->m_zip = NULL; } } void testCAddressConstructorCopy(void) { cout << "testCAddressConstructorCopy\n"; cout << "===========================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a2('r'); cout << a2 << endl; CAddress a3(a2); cout << a3 << endl; cout << "-------------------------\n"; } } CAddress::CAddress(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; } 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; 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; } |