6 Inheritance And Polymorphism

Object Oriented Programming · Unit 6

Inheritance and Polymorphism

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

What this unit covers

  • Inheritance definition and purpose
  • Single-level inheritance
  • Multi-level inheritance
  • Multiple inheritance
  • Hybrid inheritance
  • Public, private, and protected inheritance
  • Virtual base class and ambiguity resolution
  • Aggregation versus inheritance

Multi-level inheritance

208210 marks

Explain inheritance with suitable example.Differentiate between single, multiple, and hybrid inheritance.Write a program to show constructor invocation order in multilevel inheritance.[1+4+5]

Inheritance is an Object-Oriented Programming (OOP) concept where a new class (called the derived class or child class) acquires the properties and behaviors (data members and member functions) of an existing class (called the base class or parent class). K...

Full solved answer →
2080.110 marks

Differentiate between single level inheritance and multiple inheritance?Write a C++ program to calculate the percentage of a student using multi-level inheritance. Accept the marks of three subjects in base class "Capture". Derive class "Total" from "capture" which should include a function to find the total marks obtained and derive class "Final" from "Total" which should calculate and display the percentage of student.[3+7]

Feature Single Level Inheritance Multiple Inheritance --------- Definition A derived class inherits from only one base class A derived class inherits from two or more base classes Syntax class B : public A {} class C : public A, public B {} Complexity Simpl...

Full solved answer →

Virtual base class and ambiguity resolution

20825 marks

Explain ambiguity problem in multiple inheritance with example. How is it resolved using virtual base class? [5]

When a class inherits from two or more base classes that themselves share a common base class, the derived class ends up with multiple copies of the common base class members. This creates ambiguity because the compiler cannot determine which copy to use. T...

Full solved answer →
05 marks

What is meant by ambiguity? Explain ambiguity in multiple inheritance and its solution with example. [5]

Ambiguity refers to a situation in object-oriented programming where the compiler is unable to determine which version of a function or member to use when the same name exists in more than one base class or path. It creates confusion during compilation and ...

Full solved answer →

Inheritance definition and purpose

208110 marks

List any two types of inheritance with example.Describe the order of execution of constructors and destructors in derived class.[5+5]

--- (a) Two Types of Inheritance with Examples When a derived class inherits from only one base class, it is called single inheritance. Syntax: Example: Diagram: --- When a derived class inherits from more than one base class, it is called multiple inherita...

Full solved answer →

Public, private, and protected inheritance

20805 marks

What is the difference between public, private and protected inheritance in C++? [5]

In C++, when a derived class inherits from a base class, an access specifier is used to control how the inherited members are accessible in the derived class and further down the hierarchy. Syntax: --- The inherited member's accessibility in the derived cla...

Full solved answer →
2080.15 marks

What is the role of protected access specifiers in inheritance? Explain with example. [5]

The protected access specifier is an access control modifier that makes class members accessible: - Within the same class - Within derived (child) classes - NOT accessible from outside the class hierarchy (unlike public) It sits between private and public i...

Full solved answer →

Aggregation versus inheritance

20795 marks

Differentiate between aggregation and inheritance. Why do we need virtual base class? [5]

--- Feature Aggregation Inheritance --------- Relationship "Has-a" relationship "Is-a" relationship Nature One class contains an object of another class as a member One class derives properties and behaviors from another class Code Reuse Achieved by using o...

Full solved answer →

Single-level inheritance

207810 marks

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions read and display value of data members. Derive a class names typist from above class. The class should contain a private data members. Finally create two objects of typist class and read and display their values.[10]

This problem demonstrates Inheritance in Object-Oriented Programming (OOP): - Base Class: Employee (with private members: Eid, Ename, Salary) - Derived Class: Typist (inherits from Employee, adds its own private member) - Two objects of Typist are created, ...

Full solved answer →
010 marks

Create a class Employee with private data members Eid, Ename, and Salary. Include public member functions to read and display value of data members. Derive a class names Typist from above class. The class should contain a private data member typing_speed and public member functions to read and display values of data members. Finally create two objects of typist class and read and display their values.[10]

This problem demonstrates single inheritance in C++, where: - Employee is the base class (parent) - Typist is the derived class (child) that inherits from Employee - Private members of base class are accessed through public member functions --- --- --- Feat...

Full solved answer →