📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    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) {}`. Correct Answer Incorrect Answer
    B Remove the constructor from `Person`. Correct Answer Incorrect Answer
    C Make `name` a `public` member in `Student`. Correct Answer Incorrect Answer
    D Add `this->name = n;` inside the `Student` constructor. Correct Answer Incorrect Answer
    E Make `Person` an abstract class. Correct Answer Incorrect Answer

    Solution

    The correct answer is A

    Practice Next
    ask-question