|
//file : CAddress02.cpp //date : 04/05/2002 //author: AOU #include <iostream.h> #include <time.h> #include <stdlib.h> #include <string.h> class CAddress { private: char *firstName; char *lastName; char *street; char *city; char *state; char *zip; public: CAddress(void); CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp); CAddress(CAddress &a); friend ostream & operator << (ostream &bob, const CAddress &a); }; void main(void) { CAddress a1; cout << a1 << endl; CAddress a2("John", "Smith", "123 Main Street", "Pittsburg", "KS", "66762"); cout << a2 << endl; CAddress a3(a2); cout << a3 << endl; } CAddress::CAddress(CAddress &a) { firstName = new char[strlen(a.firstName) + 1]; strcpy(firstName, a.firstName); lastName = new char[strlen(a.lastName) + 1]; strcpy(lastName, a.lastName); street = new char[strlen(a.street) + 1]; strcpy(street, a.street); city = new char[strlen(a.city) + 1]; strcpy(city, a.city); state = new char[strlen(a.state) + 1]; strcpy(state, a.state); zip = new char[strlen(a.zip) + 1]; strcpy(zip, a.zip); } CAddress::CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp) { firstName = new char[strlen(fn) + 1]; strcpy(firstName, fn); lastName = new char[strlen(ln) + 1]; strcpy(lastName, ln); street = new char[strlen(sa) + 1]; strcpy(street, sa); city = new char[strlen(ct) + 1]; strcpy(city, ct); state = new char[strlen(st) + 1]; strcpy(state, st); zip = new char[strlen(zp) + 1]; strcpy(zip, zp); } ostream & operator << (ostream &bob, const CAddress &a) { bob << "\nFirst Name: "; if (a.firstName != NULL) bob << a.firstName; bob << "\n Last Name: "; if (a.lastName != NULL) bob << a.lastName; bob << "\n Street: "; if (a.street != NULL) bob << a.street; bob << "\n City: "; if (a.city != NULL) bob << a.city; bob << "\n State: "; if (a.state != NULL) bob << a.state; bob << "\n Zip: "; if (a.zip != NULL) bob << a.zip; return bob; } CAddress::CAddress(void) { firstName = NULL; lastName = NULL; street = NULL; city = NULL; state = NULL; zip = NULL; } /* SAMPLE RUN: First Name: Last Name: Street: City: State: Zip: First Name: John Last Name: Smith Street: 123 Main Street City: Pittsburg State: KS Zip: 66762 First Name: John Last Name: Smith Street: 123 Main Street City: Pittsburg State: KS Zip: 66762 Press any key to continue */ |