[#13] – Inheritance – Questions

C-plus-plus-programming-questions-techhyme

Inheritance is one of the key concept in the Object Oriented Programming language like C++, that enables you to organize classes in a hierarchical form. Just like a child inherits the characteristics of his parents and add certain new characteristics of his own.

Index:

In the similar way in C++, a new class can inherit the data members and member functions of the existing class and can also add members of its own.

This process of creating a new class from an existing class is known as Inheritance.

Key Points To Remember –

  1. Inheritance is a process of creating a new class from an existing one. The new class is known as a derived class and the existing class as a base class. All the data members and member functions in the base class are automatically included in the derived class along with its own members.
  2. Reusability is the key advantage of inheritance which results in saving time and increases the reliability of the program.
  3. A base class can be inherited using public, private or protected access specifier.
  4. Inheritance only works in one direction. The derived class has a very clearly defined relationship to base class but the base class does not know anything about the derived class. This means that objects of the base cannot access even the public members of the derived class.
  5. In public inheritance, the public members of the base class becomes public members of the derived class and protected members of the base class becomes protected member of the derived class. However, the private member of the base class remain private in the base class.
  6. In private inheritance, all the public and protected members of the base class becomes private members of the derived class and hence are accessible only in the derived class and not outside the base and derived classes. However, the private members of the base class remain private in the base class.
  7. In protected inheritance, all the public and protected members of the base becomes protected members of the derived class. The private members of the base class remain private in the base class and are inaccessible directly to its derived class and other parts of the program.
  8. The main difference between public and private inheritance is that in case of public inheritance, the public members of base and derived classes can be accessed directly by the object(s) of the derived class, whereas in case of private inheritance only the public members of the derived class can be accessed directly by the objects of the derived class.
  9. The main difference between protected and private inheritance is that in the former case continued access to the base class members further down the descendent derived classes which you might have derived from an existing derived class is possible. This is not so in case of private inheritance.
  10. When the member function with the same exists in both the base class as well as in the derived class, then the object of the derived class will invoke the member function defined in the derived class with the same name and parameters as that defined in the base class. This is known as overriding member function.
  11. In single inheritance, a class is derived from only one base class.
  12. In multilevel, a class can be derived from an existing derived class.
  13. In hierarchical inheritance, a single base class can be used to derive multiple classes which in turn can act as a base class for lower level multiple derived classes and so on.
  14. In multiple inheritance, a class can be derived from two or more unrelated base classes.
  15. Combining two or more forms of inheritance to design a program is known as hybrid inheritance.
  16. When a class is inherited as virtual, the compiler takes essential caution to avoid duplicate copies of its members. Thus multiple classes derived from it share only one copy of its member.
  17. When a base class and derived class both have constructors then the constructors are executed in the order of derivation i.e. execution of construction takes place from base class to derived class.
  18. In multiple inheritance, the base class constructors are executed in the same order as specified in the derived class’s derivation list.
  19. Destructors are executed in the reverse order as compared to constructors i.e. from derived class to base class.
  20. When a class contains objects of other classes as its data members then this known as object composition.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. How can you access the public members of the base through an object of derived class from main() ?
    • It is possible only if the class is derived publicly. This is because with public inheritance, public members of the base class become public of the derived class.
  2. What is the difference between public and private inheritance ?
    • The difference is that in case of public inheritance, the public member of the base and derived classes can be accessed directly by the objects of the derived class whereas in case of private inheritance only the public members of the derived class can be accessed directly by the objects of the derived class.
  3. What is the difference between protected and private inheritance ?
    • In the protected inheritance, continued access to the base class members further down the descendent derived classes which you might have derived from an existing derived class is possible. This is not so in the case of private inheritance.
  4. When should base class data members be made protected rather than private or public ?
    • If we went that certain members of the base class to be accessed directly in the base class as well as its derived class, but not from outside the base or derived classes (i.e. main() then such members of the base class should be declared as protected.
  5. What is the size of the object of derived class ?
    • This size of the object of the derived class is equal to the sum of sizes of data members of the base class and derived class.
  6. If a class is derived publicly from a base class and both classes contain a same named public member function f1() then can an object of the derived class the base’s same named member function from main() ?
    • Yes, using the scope resolution operator as
      derv_obj.base_class_name::();
  7. If both base class and derive class contains same named public member function f1(), then which version of f1() will be invoked when called through an object of the derived class ?
    • The object of derive class invokes the fI() version defined in the derived class as compiler always first searches the same named member function in the derived class and then in the base class.
  8. Is there any kind of relationship exists between classes that form the Inheritance ?
    • Yes there is kind of relationship exist between a derive class and a base class.
  9. What is order of execution of constructors and destructors in inheritance ?
    • Constructors are executed in the order of derivation in inheritance whereas destructors are executed in the reserve order of their creation.
  10. What kind of relationship exists between classes involved in composition ?
    • has a relationship

Other Similar Questions – 

  1. What do you mean by Inheritance ? Explain the need of inheritance with example.
  2. Describe base class and derived class ?
  3. How would you declare a derived class ?
  4. What is the significance of access specifiers for deriving classes ?
  5. What do you mean by public inheritance ? Explain using an example.
  6. Explain private inheritance using an example.
  7. Explain the concept of protected inheritance using example ?
  8. Can a base class access members of a derived class ? If no, give reasons .
  9. What are the different types of access specifiers used for deriving classes supported by C++ ?
  10. Compare public and private Inheritance using an example.
  11. What is the access control mechanism of the members of the base class if the base class is inherited as
    • public
    • private
    • protected
  12. Can we access the private members of the base by objects of derived class ?
  13. What are the difference between inheriting a class with public and protected access specifiers ?
  14. Why do you declare members of the base class as protected ?
  15. Can private members of base class be accessible in derived class ?
  16. Explain how base class member functions can be invoked in a derived class if a derived class also has a member function with a same name .
  17. What are the different forms of inheritance ? Explain them with examples.
  18. What is the difference between multilevel and multiple Inheritance ?
  19. Does multiple inheritance leads to ambiguity ? how can it be removed ?
  20. What is Hybrid Inheritance ?
  21. What is virtual base class ? Why are they needed ?
  22. Using an example, explain the concept of virtual Inheritance .
  23. In what order the class constructors are called when a derived class object is created ?
  24. How are the constructors executed in
    • Multiple Inheritance
    • Multilevel Inheritance
  25. How are the arguments passed to the base class constructor in multiple inheritance ?
  26. Explain the calling sequence of destructors in inheritance.
  27. What is composition ? How does it different from inheritance ?
  28. Describe the process of creating an object of a class that contain objects of other classes as data members.
  29. What are the advantages and disadvantages of inheritance ?
  30. State with reasons whether the following statements are TRUE or FALSE
    • Only derived class can nave constructors.
    • Constructors are executed in the order starting from the top base class to derived classes in multilevel inheritance.
    • Destructors are executed in the reverse order of constructors.
    • When a derived class is instantiated only the derived class constructors are invoked.
    • If a base class has parameterized constructors only then it must be explicitly invoked from a derived class.
    • No parameter constructor of the base class is invoked when a derived class in instantiated.

Leave a Reply