- Create a Person, Student, and Teacher class. The Student and Teacher classes inherit from the Person class.
- The Person, Student, and Teacher classes all have a name and an age variable. The Student class also has a grade variable. Create constructors for assigning values to these variables.
- Each class should have a
print
function that displays all known information about the object. For example, calling theprint
function for a Teacher named “Joe” with an age of 45 should display the message: “Joe is a teacher aged 45 years old.”
- Create a Classroom class that contains a private vector of Person objects. The vector will need to store pointers or references; check out Polymorphism if you forget why.
- The Classroom class has a name variable, which should be assigned in the class’s constructor.
- This class should have a (public) function for adding Person objects to the private vector.
- Lastly, the class should have a
print
function that first sorts each Person in the class by age (high to low). Then, it should print the class name and call theprint
function on every Person in the sortedpeople
vector.- To sort, try either using
std::sort
or creating your own sorting function.
- To sort, try either using
Here’s how these classes could be used in main
:
int main() { Teacher joe("Joe", 45); Student madison("Madison", 15, 10); Student john("John", 18, 12); Student elizabeth("Elizabeth", 16, 11); Student carl("Carl", 17, 12); Classroom classroom("Math"); classroom.addPerson(&joe); classroom.addPerson(&madison); classroom.addPerson(&john); classroom.addPerson(&elizabeth); classroom.addPerson(&carl); classroom.print(); return 0; }
Program output:
Class: Math Joe is a teacher aged 45 years old. John is a grade 12 student aged 18 years old. Carl is a grade 12 student aged 17 years old. Elizabeth is a grade 11 student aged 16 years old. Madison is a grade 10 student aged 15 years old.