Question: The code provided implements a class Person with subtypes Teacher and Student . Student's have an id number, we don't bother tracking anything else. Main

The code provided implements a class Person with subtypes Teacher and Student. Student's have an id number, we don't bother tracking anything else. Main creates an array of pointers to various people and then calls checkStudent on each one. Write the function void checkStudent(Person* p). It should use dynamic_cast to test to see if the pointer actually points to a student. If so, print out the id number of the student followed by a newline. Otherwise print out "Not student" and a newline.

Code:

#include using namespace std;

class Person { public: virtual void foo() {} //Force Person to be polymorphic };

class Teacher : public Person { };

class Student : public Person { public: Student(int idNum) { id = idNum; } int getNum() { return id; } private: int id; };

//Do not modify anything on or above the line below this //YOUR_CODE_BELOW //YOUR_CODE

//YOUR_CODE_ABOVE //Do not modify anything on or below the line above this

int main() { Person* people[6] = { new Person(), new Student(1), new Teacher(), new Student(3), new Student(2), new Teacher() };

for(int i = 0; i < 6; i++) { checkStudent(people[i]); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!