inheritance slides

31

Upload: awaisch888

Post on 11-Aug-2015

66 views

Category:

Self Improvement


0 download

TRANSCRIPT

inheritance

Mode of inheritance:

There are three mode of inheritance1. Public2. Private3. protected

Private Inheritance with private data member

Private Inheritance with public data members

Private Inheritance with protected data members

Public Inheritance with public data members

Public Inheritance with protected data members

Public Inheritance with private data members

Protected Inheritance with public data members

Protected Inheritance with private data members

Protected Inheritance with protected data members

Types of inheritance

There are 2 types of inheritance as shown below.1. Single Inheritance2. Multiple Inheritance

1. Single inheritance:

There are 3 types of single inheritance1. Simple Inheritance2. Multilevel Inheritance3. Hierarchical Inheritance

Simple InheritanceIn Simple Inheritance we have only one parent class and one child class.

Multilevel Inheritance

• Class derived from another derived class• Class B derived from A and Class C derived

from B

Syntax:

Example of multilevel inheritance: class A //base class class B : public A class C : public B

Multiple inheritance

• Class derived from more than one base class• Class C inherit features of A and B

Syntax:

Example of multiple inheritance: class A //base class class B //base class class C : public A , B //derived class

Hierarchical Inheritance

• More than one classes derived from single class• All classes inherit features of class A

syntax:

Example of hierarchical inheritance: class A //base class class B : public A class C : public A

Hybrid Inheritance

• Combination of two or more inheritance• If two or more inheritance exists

Constructor and destructor calling:

Constructor:–• Derived class constructor is called to create

derived class object• Invokes base class constructor before derived

class initializer list & constructor bodyDestructor:–• Destructor are called in Reverse order of constructor • Derived class destructor body is executed first• Then destructors body of base class executed

Derived-class Object

Derived-class constructor invokes base class constructor either• Implicitly • Explicitly

Implicit callclass B { public: B(){cout<<"Construct B";} ~B(){cout<<"Destruct B”; } };class D: public B{ public: D(){cout<<"Construct D";} ~D(){cout<<"Destruct D";} };int main(){ D obj; //object of derived class}

Output

Constructor B // implicitly call base class constructorConstructor DDestructor DDestructor B

Explicit callclass B { public: int x; B(int i) { x=i; cout<<“parameterize constructor B”; }};Class D: public B{ public: int y; D(int j) :B(j){ y=j; cout<<“parameterize constructor D”; }};

output:

int main(){ D obj(5); //object of derived class cout<<obj.x<<endl;}

Parameterize constructor BParameterize constructor D5

When a program creates a derived-class object

The derived-class constructor immediately calls the base-class constructor,

The base-class constructor’s body executes, Then the derived class’s member initializers

execute, and Finally the derived-class constructor’s body

executes