Unit 3 of 4 · BCA Sem 2

Unit 3: Inheritance

Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)

3 min read4 topics10 exam questions
On this page
  1. Unit summary
  2. Inheritance and modes of inheritance
  3. Types of inheritance
  4. Constructors in inheritance
  5. Function overriding
  6. Key terms
  7. Quick revision
  8. Important questions

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
ClassificationTypes of inheritance
Inheritance
  • 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

1

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 memberPublic modeProtected modePrivate mode
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privatenot inherited (not accessible)not accessiblenot accessible

Exam tip

Private members of a base class are never directly accessible in the derived class, whatever the mode.

2

Topic 2

Types of inheritance

ClassificationTypes of inheritance
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.

3

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) { … }.

4

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();.

ComparisonOverloading vs overriding
Function overloading
Function overriding

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

  1. Q1.Define inheritance and give its advantage.
  2. Q2.Differentiate between multiple and multilevel inheritance.
  3. Q3.What is the diamond problem?
  4. Q4.In what order are constructors called in inheritance?
  5. Q5.What is function overriding?
  6. Q6.Differentiate between protected and private members.

Long-answer questions

  1. Q1.Explain the different types of inheritance with diagrams and examples.
  2. Q2.Explain the modes of inheritance and the visibility of members in each.
  3. Q3.Write a program for the Student–Exam–Result multilevel inheritance chain.
  4. 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.

WhatsApp us