5 Inheritance

Object Oriented Programming · Unit 5 · 7 hrs

Inheritance

Exam-focused notes for Inheritance (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.

What this unit covers

  • Introduction to inheritance
  • Derived Class and Base Class
  • Access Specifiers (private, protected, and public)
  • Types of inheritance
  • Public and Private Inheritance
  • Constructor and Destructor in derived classes
  • Aggregation

Types of inheritance

208010 marks

State the use of new operator. An educational institute wishes to maintain a data of its employee. The hierarchical relationships of related classes are as follows. Define all the classes to represent above hierarchy and define functions to retrieve individual information as and when required.[10]

The new operator in C++ is used for dynamic memory allocation. It allocates memory at runtime from the heap (free store) and returns a pointer to the beginning of the allocated memory block. Key uses: - Allocates memory for a variable or object during progr...

Full solved answer →
207810 marks

How ambiguity arises in multipath inheritance? How can you remove this type of ambiguity? Explain with suitable example.[10]

Multipath inheritance (also called diamond problem or hybrid inheritance) occurs when a derived class inherits from two or more base classes that themselves share a common ancestor. This creates multiple paths through which the derived class can access the ...

Full solved answer →
207610 marks

Write a program according to the specification given below:- Create a class Teacher with data members id & subject and member functions for reading and displaying data members.- Create another class Staff with data members id & position, and member functions for reading and displaying data members.- Derive a class Coordinator from Teacher and Staff and the class must have its own data member department and member functions for reading and displaying data members.- Create two object of Coordinator class and read and display their details.[10]

This program demonstrates multiple inheritance in C++, where a derived class Coordinator inherits from two base classes Teacher and Staff. The derived class has its own data member department along with inherited members from both base classes. --- --- --- ...

Full solved answer →
20755 marks

Briefly explain types of inheritance used in object oriented programming. [5]

Inheritance is one of the key features of Object Oriented Programming that allows a new class (derived/child class) to acquire the properties and behaviors of an existing class (base/parent class)., "through inheritance, we can eliminate redundant code and ...

Full solved answer →

Constructor and Destructor in derived classes

20805 marks

Describe the chain of constructors and destructors in inheritance. [5]

In inheritance, when an object of a derived class is created or destroyed, constructors and destructors of both the base class and the derived class are automatically invoked in a specific order. --- When a derived class object is created, constructors are ...

Full solved answer →

Aggregation

207910 marks

What is aggregation? Write a program for implementing following: Create a class author with attributes name and qualification. Also create a class publication with name. Form these classes to derive a class book having attribute title and price. Each of three classes should have a getdata() method to get their data from user. The classes should have putdata() method to display the data. Create instances of the class book in main.[10]

Aggregation is a special form of association in Object-Oriented Programming where one class contains objects of other classes as its members, but the contained objects can exist independently of the container class. It represents a "has-a" relationship betw...

Full solved answer →

Access Specifiers

20795 marks

What are the various class access specifies? How public inheritance differs from private inheritance? [5]

A class uses access specifiers to control the visibility of its data members and member functions. There are three access specifiers in C++: --- - Members declared as private are visible only to member functions of their own class. - They cannot be accessed...

Full solved answer →
2080.15 marks

Explain the practical implication of protected specifier in inheritance. List advantages and disadvantages of inheritance. [5]

"Protected access specifier is similar to that of private access specifiers, the difference is that the class member declared as protected are inaccessible outside the class but they can be accessed by any subclass (derived class) of that class." The protec...

Full solved answer →

Public and Private Inheritance

207510 marks

Depict the difference between private and public derivation. Explain derived class constructor with suitable program.[10]

--- When a derived class inherits from a base class, the visibility of inherited members in the derived class depends on the type of derivation used. Feature Public Derivation Private Derivation --------- Base class public members Remain public in derived c...

Full solved answer →