Task 1 — Read Person and Student
- Person stores a name and provides getName(). Student publicly inherits Person and adds a score. Their constructors are provided.
WEEK 12 · LAB 5
Extend a Person class with a student's score.
Week 11: base and derived classes, public inheritance, and member initialization lists. In : Person(studentName), score(studentScore), the base-class part is initialized before the derived data member. Both constructors are supplied.
Complete the TODOs in the starter code, then compare your result with the expected output. The starter is intentionally unfinished.
#include <iostream>
#include <string>
class Person {
private:
std::string name;
public:
Person(std::string personName) : name(personName) {}
std::string getName() const {
return name;
}
};
class Student : public Person {
private:
int score;
public:
Student(std::string studentName, int studentScore)
: Person(studentName), score(studentScore) {}
void show() const {
// TODO 1: Print the inherited name and this student's score.
}
};
int main() {
Student first("Alex", 85);
Student second("Sam", 92);
first.show();
second.show();
return 0;
}
Expected output for the original scores.
Alex 85
Sam 92