You are on page 1of 36

INHERITANCE

INTRODUCTION
It is an important feature of Object Oriented Programming.

The mechanism by which object of one class acquires the properties of object of some other class is called inheritance. Basically new classes are created and they acquires the properties of existing classes and also adds some new properties of their own.
The existing class is known as the Base class.

INTRODUCTION
The new class is known as the derived class.

The derived class acquires some or all properties from the base class.
A derive class can also inherit properties from more than one class. There are 5 different types of inheritance: Single, multiple, hierarchical, multilevel and hybrid inheritance.

TYPES OF INHERITANCE
Single inheritance: a derived class with only base class. class A{ .
B

}; class D: public A{ ..

};

TYPES OF INHERITANCE
Multiple inheritance: a derived class with more than one base class.
B1 B2

TYPES OF INHERITANCE
class B1{ ..}; class B2{ ..};

class D: public B1, public B2{


..};

TYPES OF INHERITANCE
Hierarchical inheritance: one base class may be inherited by multiple derived classes.
B

D1

D2

D3

TYPES OF INHERITANCE
class B{ ..}; class D1:public B { ..};

class D2:public B {
..}; class D3:public B { ..};

TYPES OF INHERITANCE
Multilevel inheritance: a class is derived from another derived class.
B1

B2

TYPES OF INHERITANCE
class B1{ ..}; class B2:public B1 { ..};

class D:public B2 {
..};

TYPES OF INHERITANCE
Hybrid inheritance: Its an combination of hierarchical, multilevel and multiple.
B

B1

B2

TYPES OF INHERITANCE
class B{ ..}; class B1:public B { ..};

class B2:public B {
..}; class D:public B1, public B2 { ..};

DEFINING DERIVED CLASSES


A derived class can be defined by specifying its relationship with the base class together with its own details. class derived-classname:visibility mode baseclassname{

// members of derived class


}; Visibility mode specifies the access specifier. It may be either public or private by default.

DEFINING DERIVED CLASSES


Here is the rules related to derivation
Base class Derived class derivation derivation Public derivation Private derivation Private Not inherited Not inherited Public Public private

DEFINING DERIVED CLASSES


Class A Private: int a; Public: int b; f1(); f2();
A

Class B Private: int c; Public: int d; f3(); f4();

Class B Private: int c; Public: int b; f1(); f2() int d; f3(); class B: public A f4();

DEFINING DERIVED CLASSES


class A { int a; public:

int b;
void get_ab(){a=5; b=10;} int get_a() {return a;} void show() {cout<<a<<a;} };

DEFINING DERIVED CLASSES


class B: public A { int c; public:

void mul(){c=b*get_a();}
void display() {cout<<a<<get_a(); cout<<b<<b;

cout<<c<<c;
};

DEFINING DERIVED CLASSES


int main() {B B1; B1.get_ab(); B1.mul(); O/p: a=5; a=5; b=10 c=50 a=5 b=20 c=100

B1.show();
B1.display(); B1.b=20; B1.mul(); return 0;}

MAKING A PRIVATE MEMBER INHERITABLE This can be done using a 3rd visibility modifier known as protected

A member declared as protected is accessible by member functions within its class and any class immediately derived from it.
It cannot be accessed by the functions outside these two classes.

MAKING A PRIVATE MEMBER INHERITABLE Here is the rules related to derivation


Base class derivation Private Public Derived class derivation Public Private Protected derivation derivation derivation

Not inherited
Public

Not inherited private

Not inherited Protected

class B

private protected public

Not inheritable;

class D2: private B private protected

class D1: public B


private protected public private

public

protected
public class X: public D1, protected D2

AMBIGUITY RESOLUTION IN INHERITANCE Case I class M { public: void display(){cout<< class M;} };

class N
{ public: void display(){cout<< class N;} }; class p: public M, public N { public: void display(){M::display();}

};

AMBIGUITY RESOLUTION IN INHERITANCE Case I class p: public M, public N { public: void display(){M::display();} };

Which display() function is used by the derived class?


To solve this we have to use a name instance with the derived class using a scope resolution operator.

AMBIGUITY RESOLUTION IN INHERITANCE Case II class A { public: void display(){cout<< class A;} };

class B: public A
{ public: void display(){cout<< class B;} };

AMBIGUITY RESOLUTION IN INHERITANCE Case II int main() { B b; b.display();

b.A::display();
b.B::display(); return 0; };

VIRTUAL BASE CLASS


A

B1

B2

VIRTUAL BASE CLASS


Consider a case where multilevel, multiple and hierarchical inheritance happened together All the public and protected members of class A are inherited into the C twice, one set via B1, another set of same properties by B2. To avoid duplication of inherited members due to these multiple path, the common base class have to be inherited as virtual class.

Virtual base class ensures that upon inheritance, only one copy of it will be copied.

VIRTUAL BASE CLASS


class A{ }; class B1:virtual public A{ }; class B2:virtual public A{ }; class C{ };

ABSTRACT CLASS
This is a class that is not used to create objects

The can be act only as a base class.

CONSTRUCTORS IN DERIVED CLASSES If the base class constructor takes no argument then it is not required to the derived class to have constructor If the base class constructor takes any argument then it is mandatory for the derived class to have constructor with one or more arguments and they passes the arguments to the base class constructor. When both the derived class and base classes contains constructors then base constructor is executed first then the derived class constructor is executed.

CONSTRUCTORS IN DERIVED CLASSES The base classes are constructed in the order in which they appear in the declaration of the derived class. The constructor will be executed in order of inheritance.

derived constructor(arglst1, arglst2,...argelistN)


: base1(arglst1), base2(arglst2),.,baseN(arglstN){ //Body of default constructor }

CONSTRUCTORS IN DERIVED CLASSES Example: D(int a1, ine a2, float b1, float b2, int d1): A(a1, a2), B(b1, b2) {

d=d1;
}

CONSTRUCTORS IN DERIVED CLASSES class alpha { int x;

public: alpha(int i){ x=i;


cout<<alpha initialized;} void show_x(){ cout<<x=<<x;}}; class beta{ float y;

public: alpha(float j){ y=j;


cout<<beta initialized;} void show_y(){ cout<<y=<<y;} };

CONSTRUCTORS IN DERIVED CLASSES class gamma:public beta, public alpha { int m,n;

public: gamma(int a, float b, int c, int d):


alpha(a), beta(b){ m=c; n=d; cout<<gamma initialized;} void show_mn(){ cout<<x=<<x;

cout<<x=<<x;}
};

CONSTRUCTORS IN DERIVED CLASSES int main(){ gamma g(5,10.75,20,30);

g.show_x();
g.show_y(); g.show_mn(); return 0; }

ASSIGNMENTS
How the constructor of derived class will be written in case of multilevel inheritance?

What is initialization list? How an initialization list is used in constructors of derived class?

You might also like