Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Online Practice Test
>
CPP 2
« Previous
Next »
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
« Previous
Next »
Write your comments
*
*
Email must be in the form someone@domain.com
*
*
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
Related Content
C test (42)
C (12)
C++ for beginners (10)
C++ (20)
C++ (24)
C++ (18)
C++ (14)
C++ (12)
C++ (15)
C++ (15)
Data structure (20)
Data structure (10)
Data structure (10)
Data structure (10)
PERL (25)
VC++ (11)
Oops (30)
Mainframe (41)
Mainframe (15)
Embedded systems (15)
OOAD (49)
Advertisement
▲