ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!

  • google app store apple app store
  • тЬЦ

      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
      More IT Operating System Questions
      ask-question