Task 1 — Read the common interface
- Person provides virtual introduce(). Student and Teacher each override it. showIntroduction() accepts a Person reference and calls introduce(). Keep this supplied structure.
WEEK 14 · LAB 6
Use one function to introduce a student and a teacher.
Weeks 7, 9, and 13: references, const member functions, virtual functions, and override. const Person& refers to the original object without copying it. Review the supplied virtual destructor before running the code; keep that line as provided.
Complete the TODOs in the starter code, then compare your result with the expected output. The starter is intentionally unfinished.
#include <iostream>
class Person {
public:
virtual void introduce() const {
std::cout << "I am a person." << '\n';
}
virtual ~Person() = default;
};
class Student : public Person {
public:
void introduce() const override {
// TODO 1: Print the student's introduction.
}
};
class Teacher : public Person {
public:
void introduce() const override {
// TODO 2: Print the teacher's introduction.
}
};
void showIntroduction(const Person& person) {
person.introduce();
}
int main() {
Student student;
Teacher teacher;
showIntroduction(student);
showIntroduction(teacher);
return 0;
}
Expected output after completing both introductions.
I am a student.
I am a teacher.