// Problem Set 1 Solutions // by // Jose M. Vidal // // Define the top-level USCMember class #include #include class USCMember { char * name; // the person's name long int ssn; // social security number char sex; // M for male, F for female, U for unknown int firstYear; // year person started at USC int firstMonth; // month (1-12) person started at USC int firstDay; // day of the month (1-31) person started friend ostream &operator<<(ostream &, const USCMember &); public: USCMember(char * theName, long int theSSN, char theSex); virtual ~USCMember(); void setStartDate (int month, int day, int year); virtual void prettyPrint(); // sends this person to cout, printing nicely. }; //The constructor USCMember::USCMember(char * theName, long int theSSN, char theSex) { name = new char [strlen(theName) + 1]; if (name == NULL) { cerr << "Out of memory" << endl; return; }; strcpy (name, theName); ssn = theSSN; sex = theSex; firstYear = 0; firstMonth = 0; firstDay = 0; } //The destructor USCMember::~USCMember() { delete [] name; } void USCMember::setStartDate(int month, int day, int year) { firstDay = day; firstMonth = month; firstYear = year; } void USCMember::prettyPrint() { cout << this->name << "\t" << this->ssn << "\t" << this->firstMonth << "/" << this->firstDay << "/" << this->firstYear%100 << "\t" << this->sex ; } ostream &operator<<(ostream &output, const USCMember &person) { if (person.firstYear == 0) output << person.name; else output << person.name << " start date:" << person.firstMonth << "/" << person.firstDay << "/" << person.firstYear%100 << endl; return output; } // A class for USC employees // It inherits from USC members. // class employee : public USCMember { char *department; //department employee works for int level; // 1-15 seniority level. public: employee(char * theName, long int theSSN, char theSex, char *theDept, int theLevel); virtual ~employee(); virtual void prettyPrint(); }; employee::employee(char * theName, long int theSSN, char theSex, char *theDept, int theLevel) : USCMember(theName, theSSN, theSex) { department = new char[strlen(theDept)+1]; if (department == NULL) { cerr << "Out of memory." << endl; return; } strcpy(department, theDept); level = theLevel; } employee::~employee() { delete [] department; } void employee::prettyPrint() { this->USCMember::prettyPrint(); cout << "\t" << department << "\t" << level; } // A class for faculty members, it inherits from employee class faculty : public employee { char * researchArea; // reseach area of faculty int numClasses; //number of classes faculty teaches public: faculty(char * theName, long int theSSN, char theSex, char *theDept, int theLevel, char * researchArea, int numClasses); ~faculty(); void prettyPrint(); }; //constructor faculty::faculty(char * theName, long int theSSN, char theSex, char *theDept, int theLevel, char * theResearchArea, int theNumClasses) : employee(theName, theSSN, theSex, theDept, theLevel) { researchArea = new char[strlen(theResearchArea)+1]; if (researchArea == NULL) { cerr << "Out of memory" << endl; return; } strcpy(researchArea, theResearchArea); numClasses = theNumClasses; } //destructor faculty::~faculty() { delete [] researchArea; } // override the virtual AND call the parent virtual function void faculty::prettyPrint() { this->employee::prettyPrint(); cout << "\t" << researchArea << "\t" << numClasses; } // a class for students, it inherits from USCMember class student : public USCMember { double gpa; //the student's GPA char * major; //the student's major int year; // 1=fresh 2=soph 3=junior 4=sen 5=sen+ 6=sen++ 10=grad. public: student(char * theName, long int theSSN, char theSex, double theGPA, char * theMajor, int theYear); virtual ~student(); virtual void prettyPrint(); }; //constructor student::student(char * theName, long int theSSN, char theSex, double theGPA, char * theMajor, int theYear) : USCMember(theName, theSSN, theSex) { major = new char[strlen(theMajor)+1]; if (major == NULL) { cerr << "Out of memory" << endl; return; } strcpy(major, theMajor); gpa = theGPA; year = theYear; } //destructor student::~student() { delete [] major; } void student::prettyPrint() { this->USCMember::prettyPrint(); cout << "\t" << major << "\t" << gpa; } // Here I test void main() { USCMember alexa("Alexa Davidson", 123456789, 'F'); cout << alexa << endl; alexa.setStartDate(8,15,1998); cout << alexa << endl; USCMember tony("Tony Thompson", 987654321, 'M'); cout << tony << endl; tony.setStartDate(1,1,2000); // are you Y2K compliant? cout << tony << endl; tony.prettyPrint(); cout << endl; employee joe("Joe Carpenter", 777334444, 'M', "Carpentry", 7); joe.prettyPrint(); cout << endl; joe.setStartDate(1,15,2002); joe.prettyPrint(); cout << endl; faculty larry("Larry Stephens", 333224444, 'M', "ECE", 7, "AI", 1); larry.prettyPrint(); cout << endl; larry.setStartDate(8,15,1990); larry.prettyPrint(); cout << endl; student susan("Susan Conry", 333012222, 'F', 3.3, "Music", 2); susan.prettyPrint(); cout << endl; susan.setStartDate(3,4,1995); susan.prettyPrint(); cout << endl; // Change "Your Name" to be your name, and change the other fields to // something reasonable (does not have to be the truth). student you("Your Name", 100000000, 'U', 4.0, "?????", 0); you.prettyPrint(); cout << endl; you.setStartDate(3,4,1995); you.prettyPrint(); cout << endl; }