Which of the following cannot be inherited from the base class?

Options
- Constructor
- Friend
- Both a and b cannot be inherited
- Both a and b can be inherited


CORRECT ANSWER : Both a and b cannot be inherited

Discussion Board
C++ - Constructor & Friend Function

Constructor cannot be inherited but a derived class can call the constructor of the base class. In C++, friend is not inherited. If a base class has a friend function, then the function does not become a friend of the derived class(es).

Prajakta Pandit 01-24-2017 06:12 AM

reason constructors aren’t inherited

Constructors are different from other class methods in that they create new objects, whereas other methods are invoked by existing objects. This is one reason constructors aren’t inherited. Inheritance means a derived object can use a base-class method, but, in the case of constructors, the object doesn’t exist until after the constructor has done its work.

career 06-13-2014 02:57 AM

Inheritance of base class contractor

I have always disagreed with the notion that constructors cannot be inherited.

Think in terms of implementation. When one derives a subclass from a non-virtual base class, one desires to inherit implementation. Example...

class baseClass {
public:
baseClass() : m_baseVar(5) {}
int baseVar() { return m_baseVar; }
private:
int m_baseVar;
};

class subClass : private baseClass {
public:
subClass() : m_subVar(2) {}
int subVar() { return m_subVar * baseVar(); }
private:
int m_subVar;
};

Calling subVar() on an instance of subClass returns the value 10. Surely baseClass() has something to do with that behaviour.

In the example, subClass INHERITS implementation from baseClass, which includes the initialization of baseclass member variables performed by the baseclass ctor. To say that subClass inherits baseClass::baseVar() implementation, but not baseClass() ctor implementation would be absurd. The baseclass ctor is inherited just as any other member method.

gds 06-12-2014 06:25 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement