/* * person.cpp * CIS 250 * David Klick * 2/21/05 * * Demonstration of classes, constructors, constructor * overloading, accessor and mutator methods, static * and constant data members, dynamic memory alloaction, * and destructors. */ #include "person.h" int person::count = 0; person::person() { init("", 0L, 'M', 0); } person::person(const char* name, const long ssn, const char gender, const int age) { init(name, ssn, gender, age); } void person::init(const char* nm, const long ssn, const char gender, const int age) { name = NULL; setName(nm); setSSN(ssn); setGender(gender); setAge(age); count++; } person::~person() { if (name != NULL) delete[] name; count--; } void person::setName(const char* nm) { if (name != NULL) delete[] name; if (nm == NULL) { name = new char[1]; if (name != NULL) name[0] = '\0'; } else { name = new char[strlen(nm) + 1]; if (name != NULL) strcpy(name, nm); } } void person::setSSN(const long ssn) { // you may have to use the this pointer if // arg names match instance variable names if (ssn < 0L) this->ssn = 0L; else this->ssn = ssn; } void person::setGender(const char g) { if (g=='M' || g=='m') gender = 'M'; else gender = 'F'; } void person::setAge(const int age) { // you may have to use the this pointer if // arg names match instance variable names if (age < MINIMUM_AGE) this->age = MINIMUM_AGE; else if (age > MAXIMUM_AGE) this->age = MAXIMUM_AGE; else this->age = age; } const char* person::getName() const { return name; } long person::getSSN() const { return ssn; } int person::getAge() const { return age; } char person::getGender() const { return gender; } int person::getCount() { return count; }