C++ INHERITANCE

PREVIOUS                                                                                                               NEXT

INHERITANCE

Q: What is inheritance?
A: Inheritance allows one class to reuse the state and behavior of another class. The derived class
inherits the properties and method implementations of the base class and extends it by overriding
methods and adding additional properties and methods.

Q: When should you use multiple inheritance?
A:There  are  three  acceptable  answers:-  "Never,""Rarely,"  and  "When  the  problem  domain
cannot  be  accurately  modeled  any  other  way."  Consider  an  Asset  class,  Building  class,  Vehicle
class,  and  CompanyCar  class.  All  company  cars  are  vehicles.  Some  company  cars  are  assets
because  the  organizations  own  them.  Others  might  be  leased.  Not  all  assets  are  vehicles.  Money
accounts  are  assets.  Real  estate  holdings  are  assets.  Some  real  estate  holdings  are  buildings.  Not
all  buildings  are  assets.  Ad  infinitum.  When  you  diagram  these  relationships,  it  becomes
apparent  that  multiple  inheritance  is  a  likely  and  intuitive  way  to  model  this  common  problem
domain.  The  applicant  should  understand,  however,  that  multiple  inheritance,  like  a  chainsaw,  is
a useful tool that has its perils, needs respect, and is best avoided except when nothing else will
do.

Q: Explain the ISA and HASA class relationships. How would you implement each in a class
design?
A: A specialized class "is" a specialization of another class and, therefore, has the ISA
relationship with the other class. This relationship is best implemented by embedding an object
of the Salary class in the Employee class.

Q: When is a template a better solution than a base class?
A: When you are designing a generic class to contain or otherwise manage objects of other
types, when the format and behavior of those other types are unimportant to their containment or
management, and particularly when those other types are unknown (thus, the generality) to the
designer of the container or manager class.

Q: What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
A: Multiple Inheritance is the process whereby a child can be derived from more than one parent
class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of
more than one base class thus allowing for modeling of complex relationships.
The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity)
when two base classes implement a method with the same name.

Q: What a derived class inherits or doesn't inherit?
A: Inherits:
Every data member defined in the parent class (although such members may not always be
accessible in the derived class!)
Every ordinary member function of the parent class (although such members may not always be
accessible in the derived class!)
The same initial data layout as the base class.
Doesn't Inherit :
The base class's constructors and destructor.
The base class's assignment operator.
The base class's friends

PREVIOUS                                                                                                                   NEXT

No comments:

Post a Comment