Chapter 8. Inheritance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Chapter 8.

Inheritance

8.1 Introduction:
The mechanism of deriving a new class from an old one is called inheritance ( or
derivation ). The concept of inheritance provides the idea of reusability. This means that
we can add additional features to an existing class without modifying it. The old class is
referred to as the base class and the new one is called the derived class or sub class.
The derived class inherits some or all the features from the base class. A class can also
inherits properties from more than one class or from more than one level. A derived
class with only one class is called single inheritance and one with several base classes
is called multiple inheritance. If the features of one class may be inherited by more than
one class , the process is known as hierarchical inheritance. The mechanism of deriving
a class from another derived class is known as multilevel inheritance.

Forms of inheritance:
( a )Single Inheritance : ( b )Multiple Inheritance :

A A B

B C

( c ) Hierarchical Inheritance : ( d ) Multilevel Inheritance :


A A

B
B C D

C
( e ) Hybrid Inheritance :

B C

8.2 Defining derives classes:


A derived class can be defined by specifying its relationaship with the base class in
addition to its own detail. The general form of defining a derived class is :

class derived_class_name:visibility_modebase_class_name
{
----------------
---------------- // members of the derived class
----------------
};

The colon indicates that the derived_class_name is derived from the base_class_name.
The visibility_mode is optional. 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:
class ABC:private XYZ // private derivations
{
members of ABC
};
class ABC:public XYZ // public derivations
{
members of ABC
};
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. Therefore the public
members of the base class only be accessed by the member functions of the derived
class. They can not be accessible to the objects of the derived class.
When a base class is publicly inherited, ‘public members’ of the base class
becomes ‘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 private
members of a base class will never become the members of its derived class. Hence by
using inheritance, some of the base class data elements and member functions are
‘inherited’ in to derived class. So that we can either add our own data and member
functions and thus extends the functionality of the base class.

8.3 Single inheritance:

If a new class is derived with only one base class is known as single inheritance.
Consider the following program, where ‘class B ‘is a base class and ‘class D’ is a
derived class.
#include<iostream.h>
class B
{
int a; //private data member; not inheritable
public : int b; // public; can be used for inheritance
void get_ab( );
intget_a( );
void show_a ( );
};
class D:public B // public derivation
{
int c;
public : void mul( );
void display( );
};
void B::get_ab( )
{
a=5;
b=10;
}
int B::get_a( )
{
return a;
}
void B::show_a( )
{
cout<<”\n a = “<<a;
}
void D::mul( )
{
c=b*get_a( );
}
void D::display( )
{
cout<<”\n a = “<<get_a( );
cout<<”\n b = “<<b;
cout<<”\n c = “<<c;
}
void main( )
{
D d;
d.get_ab( );
d.mul ( );
d.show_a( );
d.display( );
d.b=20;
d.mul( );
d.display( );
}

Output:
a = 5;
a = 5;
b = 10;
c = 50;

a = 5;
b = 20;
c = 100;

The class D is a public derivation of the base class B. Therefore, ‘D’ inherits all
the public members of ‘B’ and retains their visibility. Thus the public member of the base
class ‘B’ is also a public member of the derived class D. The private members of ‘B’ can
not be inherited by D.
Although the data member ‘a’ is private in ‘B’ can not be inherited, objects of ‘D’
are able to access it through an inherited member function of B.

//Program for single inheritance using private derivation

#include<iostream.h>
class B
{
int a; //private data member; not inheritable
public : int b; // public; can be used for inheritance
void get_ab( );
intget_a( );
void show_a ( );
};
class D:private B // private derivation
{
int c;
public : void mul( );
void display( );
};
void B::get_ab( )
{
cout<<”\n Enter the values for a and b :”;
cin>>a>>b;
}
int B::get_a( )
{
return a;
}
void B::show_a( )
{
cout<<”\n a = “<<a;
}
void D::mul( )
{
get_ab( )
c=b*get_a( ); // ’a’ can not be used directly, since it is a private
// member of class B
}
void D::display( )
{
show_a( ); // output value of ‘a’
cout<<”\n b = “<<b;
cout<<”\n c = “<<c;
}
void main( )
{
D d;
// d.get_ab( ); won’t work
d.mul ( );
// d.show_a( ); won’t work
d.display( );
// d.b=20; won’t work
d.mul( );
d.display( );
}
Output :
Enter values for a and b : 4 6
a=4
b=6
c = 24
Enter the values of a and b : 12 20
a = 12
b = 20
c = 240

8.4 Making private member inheritable :

Private member of a base class can not be inherited and therefore it is not
available for the derived class directly. But private data member can be made inherited
by a derived class by modifying the visibility limit of the private member by making it
public. Since this would make it accessible to all the other functions of the program thus
taking away the advantage of data hiding.
The above problem can be solved by using the visibility modifier as ‘ protected’.
A member declared as ‘protected’ is accessible by the member functions within its class
and any class immediately derived from it.
Example:
class Test
{
private : ---------------- // optional visible to member functions with in
----------------- // its class
-----------------
protected : --------------- // visible to member functions of its own
----------------- // class and derived class
---------------
public : ----------------- // visible to all functions in the program
-----------------
----------------
};
When a protected member is inherited in public mode, it becomes protected in
the derived class too and therefore is accessible by the member functions of the derived
class. It can be used for further inheritance.
A protected member, inherited in the private mode, becomes private in the
derived class. It can be used for member functions of the derived class. But can not be
used for further inheritance (since private members can not be inherited.)

Effect of inheritance on the visibility of members :

Base Derived Class Visibility


Class Public Derivation Private Derivation Protected Derivation
Visibility
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected
class B

private
protected
public

class C: private B
class A: public B

private
private
protected
protected
public
public

class D:public A, protected C

private
protected
public

8.5 Multilevel Inheritance :

When a class is derived from more than one base class is called multilevel
inhertitance. A derived class with multilevel inheritance is declared as follows :

class A {-----------} // base class


class B : public A { --------} // B derived from A
class C : public B { -------- } // C derived from B

This process can be extended to any numbers of class levels. Consider an example :
assume that the test results of a batch of students are stored in different classes. Class
‘Student’ stores the roll_no, class ‘Test’ stores the test; the class ‘Result’ can inherit the
details of the marks obtained in the test and the roll_no of students through multilevel
inheritance.

class Student
{
protected : introll_no;
public : void get_number(int);
void put_number(int);
};
void Student::get_number(int a)
{
roll_no = a;
}
void Student::put_number( )
{
cout<<”\n Roll Number : “<<roll_no;
}
class Test:public Student //first level derivation
{
protected: float sub1;
float sub2;
public : void get_marks(float,float);
void put_marks( );
};
void test::get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
class Result: public Test
{
float total;
public : void display( );
};
/* The class ‘Result’ after inheritance would contain the following members :
private : float total; // own member
protected : introll_no; // inherited from Student via Test;
float sub1; // inherited from Test
float sub2; // inherited from Test
public : void get_number(int); // inherited from Student via Test;
void put_number(int); // inherited from Student via Test;
void get_marks(float,float); // inherited from Test
void put_marks( ); // inherited from Test
void display( ); // own member */
void Result::display( )
{
total=sub1+sub2;
put_number( );
put_marks( );
cout<<”\n Total = “<<total;
}
void main( )
{
Result S;
S.get_number(111);
S.get_marks(75,89);
S.display( );
}

8.6 Multiple inheritance :

A class can inherit the attributes of two or more classes. This is known as
multiple inheritance. Multiple inheritance allows us to combine the features of several
existing classes for defining new classes. It is like a child inheriting the physical features
of one parent and the intelligence of another.

B-1 B-2 B-n

Syntax of a derived class with multiple base class is:

class D:visibility b1,visibility b2,………….,visibility Bn


{
-----------------
----------------- // body of D
-----------------
};
where visibility may be either public or private.

Example :
class P:public M, public N
{
public : void display( );
};
Classes ‘M’ and ‘N’ have been specified as follows :
class M
{
protected : int M;
public : void get_m(int);
};
void M::get_m(int x)
{
m = x;
}
class N
{
protected : int n;
public : void get_m(int);
};
void N::get_n(int y)
{
n = y;
}

The derived class P, as declared above contain all the members of ‘M’ and ‘N’ in
addition to its own members.
class P
{
protected : int m; // from M
int n; // from N
public : void get_m(int); //from M
void get_n(int); //from N
void display(void); //own member
};

The member function display( ) can be defined as


void P::display( )
{
cout<<”\n m = “<<m;
cout<<”\n n = “<<n;
cout<<”\n m+n = “<<m+n;
}
void main( )
{
P p;
p.get_m(10);
p.get_n(20);
p.display( );
}
8.7 Hierarchical inheritance :

Inheritance can be used to modify a class when it did not stratify the
requirements of a particular problem. Additional members are added through
inheritance to extend the capabilities of a class. Another interesting application of
inheritance is to use it as a support to the hierarchical design of a program, where
certain features of one level are shared by many others below that level.
Example: Hierarchical classification of Student

Students

Arts Engineering Medical

Computer Civil Mechanical

In C++ such problems can be easily converted into class heirachices. The base
class will include all the features that are common to the subclasses. A subclass can be
constructed by inheriting the properties of the base class. A sub class can serve as a
base class for the lower level classes and so on.

8.8 Hybrid Inheritance:

There could be situations where we need two or more types of inheritance to


design a program. For example, consider the case of processing the student result
considering their performance in sports. Assume weightage for sports is stored in a
separate class called Sports. The new inheritance relationship between the various
classes would be shown.
Student

Test Sports

Result

8.9 Ambiguity Resolution in Inheritance :

Occasionally, we may face a problem in using the multiple inheritances, when a


function with the same name appears in more than one base class. Consider the
following two classes:
class M
{
public : void display( )
{
cout<<”\n Class M “;
}
};
class N
{
public : void display( )
{
cout<<”\n Class N “;
}
};
Which display( ) function is used by the derived class when we inherit these two
classes.
We can solve this problem by defining a named instance within the derived class, using
the class resolution operator with the function as shown below :

class P: public M,public N


{
public : void display( ) // overrides display( ) of M and N
{
M::display( );
}
};

We can now use the derived class as follows :


void main( )
{
P p;
p.display( );
}

Ambiguity also arises in single inheritance applications. For instance, consider the
following situation:
class A
{
public : void display( )
{
cout<<”\n A “;
}
};
class B : public A
{
public: void display( )
{
cout<<”\n B “;
}
};
In this case, the function in the derived class overrides the inherited function and
therefore, a simple call to display( ) by B type object will invoke function defined in B
only.
However, we may invoke the function defined in A by using the scope resolution
operator to specify the class.
Example:
void 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
}
This will produce the following output :
B
A
B

You might also like