You are on page 1of 21

INHERITANCE

Inheritance is an important mechanism in oops. The mechanism of deriving a new class from
an old class is known as inheritance (or derivation).

The old class is referred to as the base class and the new one is called derived class or sub
class.

There are various types of inheritances:

1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance

The below figure illustrates the various types of inheritances:

a) Single inheritance: A derived class with only one base class is called single
inheritance.

b) Multiple inheritance: A class which is derived from more than one base class is
called multiple inheritance.

A B

C
c) Hierarchical inheritance: A base class which is having more than one derived class is
called hierarchical inheritance.

B C D

d) Multilevel inheritance: The mechanism of deriving a class from another derived class is
known as multilevel inheritance.

e) Hybrid inheritance: Derivation of a class involving more than one form of inheritance
is known as hybrid inheritance.

B C

D
Defining derived classes:
A derived class can be defined by specifying its relationship with the base class
in addition to its own details.

The general form of defining a derived class is:

class derived-class-name : visibility-mode base-class-name

___________ //members of derived class

___________
___________
};

The colon (:) indicates that the derived class name is derived from the base-class-
name.
The visibility mode is optional and, if present, may be either private or public. The
default visibility mode is private.
Visibility mode specifies whether the features of the base class are privately derived
or publicly derived.

Examples:

1) class ABC : private XYZ // private derivation


{
Members of ABC ;
};
2) class ABC : public XYZ // public derivation
{
Members of ABC;
};

3) class ABC : XYZ // private derivation by default


{
Members of ABC;
};
When a base class is privately inherited by a derived class, public members of the
base class become private members of the derived class and therefore the public members
of the base class can only be accessed by the member functions of the derived class. They are
inaccessible to the object of the derived class.

On the other hand, when the base class is publicly inherited, public members of the
base class become public members of the derived class and therefore they are accessible to
the objects of the derived class.

In both the cases, the private members are not inherited and therefore, the private
members of a base class will never become the members of its derived class.

NOTE:

In inheritance, we are always creating an object for the derived class. Whenever the
derived class object is created, both the derived and base classes get memory
allocation. Using derived class object we can access public members of the derived
and base classes.
The base class can be inherited as private or public. By default it is private.
In inheritance, base class public members are directly used in derived class without
creating any object.

SINGLE INHERITANCE:

Example:

// publicly inherited
# include <iostream.h
class B
{
int a;
public:
int b;
void get_ab()
{
a=5;
b=10;
}
int get_a()
{
return a;
}
void show_a()
{
cout<<a=<<a<<\n;
}
};

class D : public B
{
int c;
public:
void mul()
{
c=b*get_a();
}
void display()
{
cout<<a=<<get_a()<<\n;
cout <<b=<<b<<\n;
cout <<c=<<c<<\n;
}
};

int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
getch();
return 0;
}
Fig : Adding more members to a class (by public derivation)

Class D
Private section

Public section inherited

b from B
B
get_ab()

get_a()

show_a()

mul()

display()
SINGLE INHERITANCE:

Example:
// privately inherited

# include<iostream.h>
class B
{
int a;//private not inheritable
public:
int b; // public, ready for inheritance
void get_ab()
{
cout<<enter values for a and b;
cin>>a>>b;
}
int get_a()
{
return a;
}
void show_a()
{
cout<<a=<<a<<\n;
}
};

class D : private B // private derivation


{
int c;
public:
void mul()
{
get_ab();
c=b*get_a();
}
void display()
{
show_a();
cout<<b=<<b<<\n;
cout <<c=<<c<<\n;
}
};

int main()
{
D d;
//d.get_ab(); wont work
d.mul();
//d.show_a();wont work
d.display();
//d.b=20; wont work b has become private
d.mul();
d.display();
return 0;
}
Fig : Adding more members to a class (by private derivation)

Class D
Private section

Inherited from B
b
B
get_ab()
Public section
get_a()

show_a()

Public section

Mul()

display()
MAKING A PRIVATE MEMBER INHERITABLE:

The private member of a base class cannot be inherited and therefore it is not available for
the derived class directly. C++ provides a third visibility mode, protected which serves a
limited purpose in inheritance. A member declared as protected is accessible by the member
functions within its class and any class immediately derived from it. It cannot be accessed by
functions outside the two classes. A class can now use all the three visibility modes as
illustrated below.

Syntax:

class alpha

Private: //optional

// visible to member functions

// within the class

Protected: //visible to member functions

//of its own and derived class

Public: // visible to all functions

// in the program

};

The below table summarizes how the visibility of base class members undergoes
modifications in all the three types of derivations:

Base class visibility Derived class visibility

Public derivation Private derivation Protected derivation

Private not inherited not inherited not inherited

Protected protected private protected

Public public private protected


Multiple Inheritance

Example:

# include<iostream.h>

Class M
{
Protected:
int m;
Public:
void get_m()
{
cout<<enter m:;
cin>>m;
}
};

Class N
{
Protected:
int n;
Public:
void get_n()
{
cout<<enter n:;
cin>>n;
}
};

Class P : Public M, Public N


{
Public:
void disp()
{
cout<<m=<<m;
cout<<n=<<n;
cout<<m*n=<<m*n;
}
};

void main()
{
P p1;
p1.get_m();
p1.get_n();
p1.disp();
}
Multilevel Inheritance:

Example:

# include<iostream.h>
Class student
{
int rno;
char name[10];
Public:
void getdata()
{
cout<<enter details:;
cin>>rno>>name;
}
void putdata()
{
cout<<rno=<<rno;
cout<<name=<<name;

}
};

Class test : public student


{
Protected:
int m1, m2;
Public:
void getmarks()
{
cout<<enter marks:;
cin>>m1>>m2;
}
};

Class result : Public test


{
int tot;
Public:
void display()
{
tot=m1+m2;
putdata();
cout<<marks:<<m1<<m2;
cout<<total:<<tot;
}
};
void main()
{
result R;
R.getdata ();
R.getmarks ();
R.display ();
}

Hierarchical Inheritance:

Example:

# include<iostream.h>

Class A
{
Protected:
int a;
Public:
void geta()
{
cout<<enter a:;
cin>>a;
}
};

Class B: Public A
{
int b;
Public:
void getb()
{
cout<<enter b:;
cin>>b;
}
void add()
{
cout<<a=<<a;
cout<<b=<<b;
cout<<sum=<<a+b;
}
};
Class C: Public A
{
int c;
Public:
void getc()
{
cout<<enter c:;
cin>>c;
}
void mul()
{
cout<<a=<<a;
cout<<c=<<c;
cout<<product=<<a*c;
}
};

void main()
{
B b1;
C c1;
b1.geta();
b1.getb();
b1.add();
c1.geta();
c1.getc();
c1.mul();
}
Hybrid Inheritance:

Example:

# include<iostream.h>
Class student
{
int rno;
char name[10];
Public:
void getdata()
{
cout<<enter data:;
cin>>rno>>name;
}

void putdata()
{
cout<<rno:<<rno;
cout<<name:<<name;

}
};

Class test: Public student


{
Protected:
int m1, m2;
Public:
void getmarks()
{
cout<<enter marks:;
cin>>m1>>m2;
}
};

Class sports
{
Protected:
int score;
Public:
void getscore()
{
cout<<enter score:;
cin>>score;
}
};
Class result: Public test, Public sports
{
int tot;
Public:
void show()
{
tot=m1+m2+score;
putdata();
cout<<m1=<<m1;
cout<<m2=<<m2;
cout<<score=<<score;
cout<<total=<<tot;
}
};

void main()
{
result R;
R.getdata();
R.getmarks();
R.getscore();
R.show();
}
Virtual Base Classes: The duplication of inherited members due to multiple paths can be
avoided by making the common base class as virtual base class while declaring the direct or
intermediate base classes as shown below

class A //grand parent

________

_______

};

class B1 : virtual public A //parent1

_______

_______

};

class B2 : public virtual A //parent 2

______

______

};

class C : public B1, public B2 //child

______ // only one copy of A is inherited

______

};
Example :

# include<iostream.h>

class student

int rno;

char name[10];

public:

void getdata( )

cout<<enter data;

cin>>rno>>name;

void putdata( )

cout<<rno:<<rno;

cout<<name:<<name;

};

class test : virtual public student

protected :

int m1, m2;

public :

void getm( )

cout<<enter marks;

cin>>m1>>m2;
}

};

class sports : public virtual student

protected :

int score;

public:

void getscore( )

cout<<enter score;

cin>>score;

};

class result : pubic test, public sports

int tot;

public:

void show( )

tot = m1+m2+score;

putdata( );

cout<<marks=<<m1<<m2;

cout<<score=<<score;

cout<< total=<<tot;

};
void main( )

result R;

R.getdata( );

R.getm( );

R.getscore( );

R.show( );

}
Ambiguity Resolution in inheritance:

# include<iostream.h>

class A

public:

void display( )

cout<<A\n;

};

class B : public A

public:

void display( )

cout<<B\n;

};

int main( )

B b; //derived class object

b.display( ) //invokes display in B

b.A: : display( ); //invokes display in A

b.B: : display( ); //invokes display in B

return 0;

You might also like