Unit 3: Inheritance
Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)
On this page
Unit summary
Inheritance lets a new class (derived) acquire the properties of an existing class (base), so code is reused rather than rewritten. This unit covers the modes of inheritance, the five types of inheritance and function overriding.
After this unit you can
- Explain inheritance and the base/derived class relationship
- Apply public, private and protected modes of inheritance
- Write programs using single, multiple, multilevel, hierarchical and hybrid inheritance
- Override base-class functions in a derived class
PTU syllabus topics
- Modes of inheritance
- single/multiple/multilevel/hierarchical/hybrid inheritance
- function overriding
Single
One base, one derived class
Multiple
One class inherits from two bases
Multilevel
A → B → C chain
Hierarchical
Several classes share one base
Hybrid
A combination of the above
Topic 1
Inheritance and modes of inheritance
Inheritance is the mechanism by which a derived (child) class acquires the data and functions of a base (parent) class. Syntax: class Derived : mode Base { … };
| Base member | Public mode | Protected mode | Private mode |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | not inherited (not accessible) | not accessible | not accessible |
Exam tip
Private members of a base class are never directly accessible in the derived class, whatever the mode.
Topic 2
Types of inheritance
Single
One base → one derived
Multiple
Two or more bases → one derived
Multilevel
A → B → C (chain)
Hierarchical
One base → many derived
Hybrid
A combination of two or more types
cppclass Student { // base
protected:
int roll;
public:
void getRoll(int r) { roll = r; }
};
class Exam : public Student { // multilevel: level 2
protected:
int m1, m2;
public:
void getMarks(int a, int b) { m1 = a; m2 = b; }
};
class Result : public Exam { // level 3
public:
void show() { cout << roll << " total = " << m1 + m2; }
};Ambiguity in multiple and hybrid inheritance: if two base classes inherit from the same grandparent, the derived class gets two copies (the diamond problem). It is solved with a virtual base class: class B : virtual public A.
Topic 3
Constructors in inheritance
When a derived object is created, the base class constructor runs first, then the derived class constructor. Destructors run in the reverse order. Arguments are passed to the base constructor in the derived constructor's initialiser list: Derived(int a, int b) : Base(a) { … }.
Topic 4
Function overriding
Function overriding means a derived class provides its own version of a function that already exists in the base class, with the same name and same parameters. The derived version hides the base version for derived objects. The base version can still be called with Base::show();.
Where
Same class
Base and derived classes
Signature
Different parameters
Same parameters
Binding
Compile time
Run time when used with virtual functions
Purpose
Same name, different inputs
Change inherited behaviour
Key terms
- Base class
- The existing class whose features are inherited
- Derived class
- The new class that inherits
- Visibility mode
- public, protected or private inheritance
- Virtual base class
- Prevents duplicate copies in the diamond problem
- Function overriding
- Redefining a base function in the derived class
Quick revision
- Five types: single, multiple, multilevel, hierarchical, hybrid.
- Private base members are never accessible in the derived class.
- Base constructor runs first; destructors run in reverse.
- Virtual base class solves the diamond problem.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Define inheritance and give its advantage.
- Q2.Differentiate between multiple and multilevel inheritance.
- Q3.What is the diamond problem?
- Q4.In what order are constructors called in inheritance?
- Q5.What is function overriding?
- Q6.Differentiate between protected and private members.
Long-answer questions
- Q1.Explain the different types of inheritance with diagrams and examples.
- Q2.Explain the modes of inheritance and the visibility of members in each.
- Q3.Write a program for the Student–Exam–Result multilevel inheritance chain.
- Q4.What is a virtual base class? Explain with the help of hybrid inheritance.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying BCA at Synetic.
