Question

The following C++ code has a common inheritance-related issue. How can it be debugged to ensure the derived class constructor properly initializes the base class? #include #include class Person { public:     std::string name;     Person(std::string
n) : name(n) {} }; class Student : public Person { public:     int studentId;     Student(std::string n, int id) { // Problematic: Base class not initialized         studentId = id;     }     void display() {         std::cout

A Change `Student(std::string n, int id)` to `Student(std::string n, int id) : Person(n), studentId(id) {}`.
B Remove the constructor from `Person`.
C Make `name` a `public` member in `Student`.
D Add `this->name = n;` inside the `Student` constructor.
E Make `Person` an abstract class.
Practice Next

Hey! Ask a query