Which of the following, in C++, is inherited in a derived class from base class?
2015
Which of the following, in C++, is inherited in a derived class from base class?
- A.
constructor
- B.
destructor
- C.
data members
- D.
virtual methods
Attempted by 351 students.
Show answer & explanation
Correct answer: C
Answer: data members are inherited.
Explanation:
Data members: Non-static data members of the base class become part of the derived object and are therefore inherited. Private data members are inherited but are not directly accessible in the derived class.
Constructors: Constructors are not inherited by default. The base constructor is called to initialize the base subobject when constructing a derived object. Since C++11 you can explicitly inherit constructors with 'using Base::Base;'.
Destructors: Destructors are not inherited. The derived destructor runs first, then the base destructor. Make the base destructor virtual if objects will be deleted through base pointers to ensure proper derived cleanup.
Virtual methods: Virtual functions are inherited and remain virtual in the derived class; they can be overridden to provide polymorphic behavior.
Access rules and static members: Inheritance follows normal access control: public, protected, private affect accessibility in derived classes. Static data members are associated with the class and can be accessed via the derived class name.
Summary: The best single answer is that data members are inherited; constructors and destructors are not automatically inherited (though constructors can be brought in explicitly), while virtual methods are inherited and can be overridden.
A video solution is available for this question — log in and enroll to watch it.