* Initialization section of constructors DayOfYear::DayOfYear(int theMonth, int theDay) : month(theMonth), day(theDay) { // empty } DayOfYear::DayOfYear() : month(1), day(1) { // empty } if DayOfYear had a MyString to keep track of the name of the month: DayOfYear::DayOfYear() : month(1), day(1), monthString("January") { // empty } DayOfYear::DayOfYear(int theMonth, int theDay) : month(1), day(1) { switch(month) { case 1: monthString = "January" ... } } * Inheritance * Person, Student, Employee, Teacher, Staff Person | |__Student | |__Employee | |__Teacher | |__Staff #include #include class Person { public: Person(); Person(string theName, string theSSN); string getName() const; string getSSN() const; void setName(string newName); void setSSN(string newSSN); void printInfo() const; private: string name; string ssn; }; Person::Person() : name(""), ssn("") { // empty } Person::Person(string theName, string theSSN) : name(theName), ssn(theSSN) { // empty } string Person::getName() const { return name; } string Person::getSSN() const { return ssn; } void Person::setName(string newName) { name = newName; } void Person::setSSN(string newSSN) { ssn = newSSN; } void Person::printInfo() const { cout << "\nERROR: printInfo called on an undifferentiated person.\n"; exit(1); } class Student : public Person { public: Student(); Student(string theName, string theSSN, int theYear); int getYear() const; void setYear(int newYear); void printInfo() const; private: int year; }; Student::Student() : Person(), year(0) { // empty } Student::Student(string theName, string theSSN, int theYear) : Person(theName, theSSN), year(theYear) { // empty } int Student::getYear() const { return year; } void Student::setYear(int newYear) { year = newYear; } void Student::printInfo() const { cout << "Name: " << getName() << endl; cout << "SSN: " << getSSN() << endl; cout << "Year: " << year << endl; } class Employee : public Person { public: Employee(); Employee(string theName, string theSSN, int theMonthsWorked); int getMonthsWorked() const; void setMonthsWorked(int newMonthsWorked); void printInfo() const; private: int monthsWorked; }; Employee::Employee() : Person(), monthsWorked(0) { // empty } Employee::Employee(string theName, string theSSN, int theMonthsWorked) : Person(theName, theSSN), monthsWorked(theMonthsWorked) { // empty } int Employee::getMonthsWorked() const { return monthsWorked; } void Employee::setMonthsWorked(int newMonthsWorked) { monthsWorked = newMonthsWorked; } void Employee::printInfo() const { cout << "\nERROR: printInfo called on an undifferentiated employee.\n"; exit(1); } class Teacher : public Employee { public: Teacher(); Teacher(string theName, string theSSN, int theMonthsWorked, double theSalary); double getSalary() const; void setSalary(double newSalary); void printInfo() const; private: double salary; }; Teacher::Teacher() : Employee(), salary(0.0) { // empty } Teacher::Teacher(string theName, string theSSN, int theMonthsWorked, double theSalary) : Employee(theName, theSSN, theMonthsWorked), salary(theSalary) { // empty } double Teacher::getSalary() const { return salary; } void Teacher::setSalary(double newSalary) { salary = newSalary; } void Teacher::printInfo() const { cout << "Name: " << getName() << endl; cout << "SSN: " << getSSN() << endl; cout << "Months Worked: " << getMonthsWorked() << endl; cout << "Salary: " << salary << endl; } class Staff : public Employee { public: Staff(); Staff(string theName, string theSSN, int theMonthsWorked, int theHoursPerWeek, double theWageRate); int getHoursPerWeek() const; double getWageRate() const; void setHoursPerWeek(int newHoursPerWeek); void setWageRate(double newWageRate); void printInfo() const; private: int hoursPerWeek; double wageRate; }; Staff::Staff() : Employee(), hoursPerWeek(0), wageRate(0.0) { // empty } Staff::Staff(string theName, string theSSN, int theMonthsWorked, int theHoursPerWeek, double theWageRate) : Employee(theName, theSSN, theMonthsWorked), hoursPerWeek(theHoursPerWeek), wageRate(theWageRate) { // empty } int Staff::getHoursPerWeek() const { return hoursPerWeek; } double Staff::getWageRate() const { return wageRate; } void Staff::setHoursPerWeek(int newHoursPerWeek) { hoursPerWeek = newHoursPerWeek; } void Staff::setWageRate(double newWageRate) { wageRate = newWageRate; } void Staff::printInfo() const { cout << "Name: " << getName() << endl; cout << "SSN: " << getSSN() << endl; cout << "Months Worked: " << getMonthsWorked() << endl; cout << "Hours Per Week: " << hoursPerWeek << endl; cout << "Wage Rate: " << wageRate << endl; } int main() { Teacher me("David Press", "555-55-5555", 2, 100000.0); Student you("John Doe", "444-44-4444", 3); Staff him("Jack Deer", "333-33-3333", 12, 40, 15.0); me.printInfo(); you.printInfo(); him.printInfo(); } * Constructors, Assignment Operators, Copy Constructors are not inherited * Private members are inherited as private. Derived class cannot use them. * Use "protected" if you want derived class to be able to access members of base. * You can inherit privately and protectedly. In those cases, public and protected members become private, and public members become protected respectively. This breaks the "is-a" relationship so not very useful. Only used for code reuse.