Professional Documents
Culture Documents
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.
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
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 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:
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
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;
}
};
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
// in the program
};
The below table summarizes how the visibility of base class members undergoes
modifications in all the three types of derivations:
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;
}
};
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;
}
};
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 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
________
_______
};
_______
_______
};
______
______
};
______
};
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;
};
protected :
public :
void getm( )
cout<<enter marks;
cin>>m1>>m2;
}
};
protected :
int score;
public:
void getscore( )
cout<<enter score;
cin>>score;
};
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( )
return 0;