// person-stu-teach.cc // Person (base) class and // Student and Teacher derived classes // implementation #include #include #include "person-stu-teach.h" // Implementation of the Person class Person::Person(char *n) { setName(n); } void Person::setName(char *n) { strcpy(name, n); } void Person::print() { cout << name << endl; } // Implementation of the Teacher class // initialize static data member at file scope int Teacher::MaxCourses = 3; // constructors are not inherited from base class // use member initialization notation to call // base class constructor function // else call to base constructor will implicitly be // done with default values Teacher::Teacher(char * newname) : Person(newname) { numcourses = 0; } // notice no setName function here // this function is in herited from Person int Teacher::addCourse() { if (numcourses < MaxCourses) { numcourses++; return 1; // indicates success } else return 0; } // notice that no print function is defined here // this function is inherited from Person // implementation of the student derived class // this class redefines the Person print function // constructor must be defined // uses member initialization syntax to call // base class constructor Student::Student(char *newname) :Person(newname) { } // notice no setName function void Student::setYear(int inyear) { year = (inyear > 0 && inyear < 7) ? inyear : 1; } // redefinition of the print function -- not // taking print function from base class // notice call to base class's print function void Student::print() { cout << "Student: "; Person::print(); } // because a student "is a" Person, member // functions from Person class may be used // directly