IInheritance in Object Oriented Programming can be described
as a process of creating new classes from existing classes. New classes inherit some of the properties and behavior
of the existing classes. An existing class that is "parent" of a new
class is called a base class.
Types of Inheritance
1)
Single level Inheritance
2) )
Multi level Inheritance
3) Multiple
Inheritance
4) Hierarchical
Inheritance
5)
Hybrid Inheritance
Benefits of Inheritance
1)
Reusability of code
2) Reduce
the software cost and design time
3) Represents
the real world situation
4) Easy
maintenance and expansion of program
Syntax
1.
Single level Inheritance
Class base
{
..
..
}
class derived: public base
{
.
.
.
}
2.
Multi level Inheritance
class base
{
..
..
}
class derived1: public base
{
.
.
.
}
class derived2: public derived1
{
.
.
.
}
3.
Multiple Inheritance
class base1
{
..
..
}
class base2
{
.
.
.
}
class derived : public base1,base2
{
.
.
.
}
4.
Hierarchical Inheritance
class base
{
..
}
class derived1: public base
{
.
.
}
class derived2: public base
{
.
.
}
Single Inheritance
Multi level Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Visibility Mode
Public
mode: If a class is inherited in public mode there is no change in
access control of base class.
i.e
public members remain public
protected members remain
protected
private members remain private
Protected
mode: If a class is inherited in protected mode public members will become protected
and no change in access control of protected and private members of base class.
i.e
public members change to public
protected members remain
protected
private members remain private
Private
mode: If a class is inherited in
private mode public and protected
members will become private and no change in access control of private members of base class.
i.e
public members change to private
protected members change
to private
private members remain private
Tips to solve board pattern questions
1.
To calculate size of object
While we are calculating size of object we must know the
size of different data types
Integer 2
bytes
Float 4
bytes
Long 4
bytes
Double 8
bytes
Short 2 bytes
Char 1
bytes ( for string name[10]= 10x1=10 bytes, city[20]=20x1=20 bytes}
While we are calculating size of object first we should
calculate size of it’s own class then
check if some class is inherited in it or not if yes then calculate the size of inherited class
and add it on size of previously calculated size repeat this process this condition
is true (please be careful while calculating the size , don’t do counting
mistakes). While calculating size don’t bother about visibility mode.
Example
class A
{
}
class B:public A
{
}
class C:public B
{
}
If we have to find the size of object of class C
1)
Go to class c calculate its size then (2) check
if class is inherited or not here yes B is inherited, (3) calculate the size of
B( 4 )again check if some class is inherited in class B or not here again it is
true(5) so calculate size of A also (6) again check if some is inherited in
class A or not, here it is false so stop
the process . (7) add all three size it will be the size of object of class C
2. Members accessible from object
Always remember object can access only public members. while you are finding the members accessible from object first write down only PUBLIC member of its own class. then check if some class is inherited in it in PUBLIC Mode or not if it is then go to that class and then write the public members of this class, if some class is inherited in this class in public mode then go to that class also and write the public members.
2. Members accessible from object
Always remember object can access only public members. while you are finding the members accessible from object first write down only PUBLIC member of its own class. then check if some class is inherited in it in PUBLIC Mode or not if it is then go to that class and then write the public members of this class, if some class is inherited in this class in public mode then go to that class also and write the public members.
3. Members accessible from MEMBER FUNCTION
Remember function can access every thing of it's own class so write every thing of class which belongs to it
now check some class is inherited or not
first level it will entry with public, protected and private gate and access public and protected member(as pvt member cant be inherited)
if some class is inherited in its base class then it will go through only public and protected gate and access only public member from this class. go through this diagram: