Docsity

Hierarchy of Inheritance - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Hierarchy of Inheritance - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 26

Hierarchy of Inheritance

  • We represent the classes involved in inheritance relation in tree like hierarchy

Direct Base Class

  • A direct base class is explicitly listed in a derived class's header with a colon (:)

class Child1:public Parent

...

Indirect Base Class

  • An indirect base class is not explicitly listed in a derived class's header with a colon (:)
  • It is inherited from two or more levels up the hierarchy of inheritance

class GrandParent{}; class Parent1: public GrandParent {}; class Child1:public Parent1{};

Example

class GrandParent{

int gpData;

public:

GrandParent() : gpData(0){...} GrandParent(int i) : gpData(i){...} void Print() const;

};

Example

class Parent1: public GrandParent{

int pData;

public:

Parent1() : GrandParent(), pData(0) {…}

};

Overriding

  • Child class can override the function of GrandParent class

Example

GrandParent Print()

Parent

Child Print()

Example

int main(){

Child1 obj; obj.Print(); obj.Parent1::Print(); obj.GrandParent::Print(); return 0;

}

Output

  • Output is as follows

Child1::Print

GrandParent::Print

GrandParent::Print

Public Inheritance

Member access in

Base Class Derived Class

Public Public

Protected Protected

Private Hidden

class Child: public Parent {…};

Protected Inheritance

Member access in

Base Class Derived Class

Public Protected

Protected Protected

Private Hidden

class Child: protected Parent {…};

Private Inheritance

  • If the user does not specifies the type of inheritance then the default type is private inheritance

class Child: private Parent {…}

is equivalent to

class Child: Parent {…}

Private Inheritance

  • We use private inheritance when we want to reuse code of some class
  • Private Inheritance is used to model “Implemented in terms of” relationship