Sie sind auf Seite 1von 14

1

CHAPTER 6 - INHERITANCE
 Mechanism of transferring the properties of one class to another class is called
inheritance. That is, a new class is created from an existing class.
 Existing class is known as the base class or super class.
 New class is known as the derived class or sub class.
 Derived class can inherit the properties (data members & member functions) of base
class.
Advantages/Need for inheritance
1. Code reusability
2. Transitive nature
Code reusability
 Once when a base class is created and checked, it can be used in various situations
without rewriting or redefining.
 Advantages are faster development, easy maintenance and easy to extend.

Person Base class

Student Derived class of Person

PG UG Derived class of Student

 Here UG & PG classes can inherit the properties of class person and student.
 Once after including a function in person class, it can be accessed by the student, PG &
UG classes.
Transitive Nature
If a class „B‟ inherits the properties of class „A‟ then automatically all the subclasses of „B‟ will
inherit the properties of „A‟. This is known as the transitive nature of inheritance. If you make
any changes in the features of „A‟, then automatically that changes will get reflected in all its sub
classes.
Type of inheritance
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance

Single inheritance
 A derived class with only one base class is known as single inheritance.
A

B
Multiple inheritance
 When a class is derived from multiple base classes, it is known as multiple inheritance.

A B

C
2

Hierarchical inheritance
 When many derived classes are created from a single base class, it is known as
hierarchical inheritance
A

B C D
Multilevel inheritance
 The mechanism of deriving a class from another derived class is known as
multilevel inheritance.
 Transitive nature of inheritance is reflected in this type.
A Base class

B Derived class of A base class of C

C Derived class of B
Hybrid inheritance
 A combination of two or more types of inheritance is known as hybrid. It can be
any combination of multiple, multilevel or hierarchical
inheritance
A B C
A Hierarchical Multiple
B C D

Multiple Hierarchical
E F
D

Derived class syntax (using single inheritance)


class derived class name: visibility mode base-class name
{
//Body of derived class
}
 Visibility mode can be public, private or protected. This gives the visibility and
availability of inherited base class members in the derived class.

 If visibility mode = public, then public of base class becomes public to derived
class and protected of base class becomes protected to derived class.

 If visibility mode=private, then public and protected data of base class becomes
private to derived class.
3

 If visibility mode=protected, then public and protected of base class becomes


protected to derived class.

 In the syntax, visibility mode is optional. By default, it is private.

Inheritance and base class


In the case of inheritance, when we are defining a base class, we need to keep in mind
the following things.
 The members we want to be inherited and the members we want to make
available throughout the program should be declared as public.
 The members we want to be inherited and the members we don’t to be public,
should be declared as protected.
 The members we don’t want to be inherited should be declared as private.

Single inheritance
#include<iostream.h>
 Object can be created only for the derived class.
class base
 By using the object of the derived class, only the public
{
and protected data and member functions of base
public:
int x; class can be inherited in the derived class.
void set x (int i)
{
x=i;
}
void display()
{
cout<<x;
}
};
class derived: public base
{
int y;
public:
void set y (int j)
{
y=j ;
}
void dis ()
{
cout <<y;
}
};
void main ()
{ derived dl ;
dl.setx(10) ;
dl.display ();
dl.sety(20);
dl.disp();
}
Eg:10, 20
4

Multiple inheritance
 The syntax of defining a derived class using multiple inheritance is,
class derivedclassname : visibilitymode baseclass_1 , visibilitymode baseclass_2, ……
{
:
};
Example:
Let „A‟ be a base class which contains internal marks & „B‟ be another base class which contains
external marks. Class „C‟ is derived from both „A‟ & „B‟ and is used to find total marks.
class A
{
int internal;
public: A B
void setinternal( )
{
cin>>internal;
}
int getinternal( ) C
{
return internal;
}
};
class B
{
int external;
public:
void setexternal( )
{
cin>>external;
}
int getexternal( )
{
return external;
}
};
class C : public A, public B
{
int total;
public:
void cal( )
{
total = getinternal( ) + getexternal( );
cout<<total;
}
};
int main( )
{
C O1;
O1.setinternal( );
O1.setexternal( );
O1.cal( );
};
5

Inheritance and access control


 When a class inherits the members of another class, the access of inherited members can
be controlled by the way of defining the derived classes. We can derive a class publicly
or privately.
Access control in publicly derived class
 A class is publicly derived by giving the visibility mode as public.
 Syntax is,
class derivedclassname : public baseclassname
{
…..
};
 The visibility mode public tells that the public members of base class can be accessed
directly by the objects of derived class.
 Protected members of base class become protected members of derived class and hence
can be accessed only by the member functions and friend functions of derived class.
 Private members of base class can be indirectly accessed using the public or protected
member functions of base class.
 Example
class employee
{
char ename[20];
int empnum;
public:
void getdata1( )
{
cin>>ename>>empnum;
}
void putdata1( )
{
cout<<ename<<empnum<<salary;
}
protected:
float salary;
void getsalary( )
{
cin>>salary;
}
};
class manager : public employee
{
char design[20];
public:
void getdata2( )
{
getsalary( );
cin>>design;
}
void putdata2( )
{
cout<<design;
}
};
6

int main( )
{
manager m1;
m1.getdata1( );
m1.getdata2( );
m1.putdata1( );
m1.putdata2( );
return 0;
}
Access control in privately derived class
 A class is privately derived by giving the visibility mode as private.
 Syntax is,
class derivedclassname : private baseclassname
{
…..
};
 Here the public & protected members of base class become private in derived class. So it
cannot be accessed directly by the objects of derived class. It can be accessed by the
member functions or friend functions of derived class.
 Example
class manager : private employee
{
char design[20];
public:
void getdata2( )
{
getdata1( );
getsalary( );
cin>>design;
}
void putdata2( )
{
putdata1( );
cout<<design;
}
};
int main( )
{
manager m1;
m1.getdata2( );
m1.putdata2( );
return 0;
}
Some facts of inheritance
1. If the name of a data member is same as a global variable, then global variable
becomes hidden inside the class.
int t = 10;
class A
{
int t;
public:
void getdata( )
{
t= 20;
7

cout<<”Class member t =”<<t;


cout<<”\nGlobal t = “<<::t;
}
};
void main( )
{
A O1;
O1.getdata( );
}
2. When a class is derived publicly, we cannot selectively deny access to some of the
base class members by re-declaring them in the private part of derived class.
Example
class Base
{
public:
int x,y;
};
class Derived : public Base
{
public:
int a;
private:
Base::x; //Invalid
};
3. When a class is derived privately, we can selectively allow access to some of the base
class members. This is done by re-declaring the selected base class member under
the public section of the derived class.
Example
class Base
{
public:
int x,y;
void f1( )
{

}
void f1(char ch)
{

}
};
class Derived : private Base
{
public:
int a;
Base::x; //Valid
Base::f1; //Valid
};
void main( )
{ Derived d;
d.x = 10;
d.f1( );
d.f1(„A‟);
}
8

 In the case of ANSI compiler, to change the access type, the keyword “using”
need to be used.
i.e. using Base::x;
4. Abstract class
An abstract class is a class that serves only as a base class from which other classes can
be derived and no objects exists for this base class.
Making a private member inheritable
This can be done in 2 ways.
1. By making the access specifier of the private member as public.
Then it becomes accessible throughout the program and this eliminates the concept of
data hiding.
2. By making the access specifier of the private member as protected.
Here the concept of data hiding is retained and at the same time makes it inheritable.
Overriding base class functions in derived class
 If the derived class defines a function with the same name as that of base class, then the base
class function becomes hidden or shadowed in derived class. This situation is called as
function overriding or shadowing.
 Example
class Base
{
….
public:
void f1( )
{
cout<<”Base class f1”;
}
void f2(char ch)
{
cout<<”Base class f2”;
}
void f3( )
{
cout<<”Base class f3”;
}
};
class Derived : public Base
{….
public:
void f1(double d)
{
cout<<”Derived class f1”;
}
void f2(char ch)
{
cout<<”Derived class f2”;
}
};
void main( )
{Derived O1;
O1.f1(32.5); //Derived class function is invoked
O1.f1( ); //Invalid
O1.f2(„A‟); //Derived class function is invoked
O2.f3( ); //Base class function is invoked
}
9

 In case if we want to access the Base class f1 & f2 functions, it can be possible using the syntax,
O1.Base::f1( );
O1.Base::f2(„A‟ );
 If a function is defined with the same name as that of the base class in derived class, then
irrespective of the argument list and return type, derived class hides the base class function.

 To make functions with the same name of the base class available in derived class, declare it
again in derived class with the help of „using‟ keyword.
Syntax is,
using Baseclassname::functionname;
This is possible in code blocks c++ compiler.
Example:
class Derived : public Base
{

public:
using Base::f1;
using Base::f2;
void f1(double d)
{
….
}
void f2(char ch)
{
….
}
};
void main( )
{
Derived O1;
O1.f1( );
}
Constructors in multiple inheritance
 If base class is not having any constructors with arguments, then it is not necessary for a
constructor in the derived class.
 If base class contains constructors with arguments, then it is must that the derived class should
have a constructor for passing values to base constructor.
This is because constructors/destructors cannot be inherited.
Example:
class A
{ int x;
public:
A(int i)
{
x=i;
}
};
class B : public A
{public:
B(int i) : A(i)
{

}
};
10

 The derived class constructor receives the entire list of arguments and passes them to the base
class constructor.
 The base class constructor body is executed before executing the body of derived class
constructor.
 Syntax is,
Derived-constructor(argument list) : base1(argument list1), base2(argument list2),……,baseN(argument listN)
{
……….
}
 Example
class B1
{
public:
B1(int i)
{
….
cout<<”\nConstructor of B1”;
}
~B1( )
{
cout<<”\nDestructor of B1”;
}
};
class B2
{
public:
B2(int i)
{
….
cout<<”\nConstructor of B2”;
}
~B2( )
{
cout<<”\nDestructor of B2”; Output
} Constructor of B1
}; Constructor of B2
class Der : public B1, public B2 Derived constructor
{ Derived destructor
public: Destructor of B2
Der(int a, int b, int c) : B1(a), B2(b) Destructor of B1
{
cout<<”\nDerived constructor”;
}
~Der( )
{
cout<<”\nDerived destructor”;
}
};
void main( )
{
Der D1(10,20,30);
}
11

Virtual Base classes


When two or more objects are derived from a common base class, we can prevent multiple
copies of the base class being present in an object derived from those objects by declaring the
base class as virtual when it is being inherited. Such a base class is known as virtual base class.
This can be achieved by preceding the base class name with the word „virtual‟.

Base | int a

Derived1 | int b Derived2 | float c

Derived3 | int d

If virtual keyword is not used


Derived3 O1;
Sizeof(O1) = 12 bytes

If virtual keyword is used while deriving Derived1 & Derived2,


Sizeof(Derived1) = 4 bytes + sizeof virtual pointer
Sizeof(Derived2) = 6 bytes + sizeof virtual pointer
Sizeof(Derived3) = 10 + sizeof virtual pointer1 + sizeof virtual pointer 2
Example
class B
{
public:
int a;
};
class D1 : public B
{
public:
int b;
};
class D2 : public B
{
public:
int c;
};
class D3 : public D1, public D2
{

};
void main( )
{
D3 O1;
O1.a=5; //Invalid because copy of ‘a’ exists in D1 & D2

}
12

There are two ways to solve this problem


1. Use :: operator
O1.D1::a=30;
O1.D2::a=50;
2. Suppose that our requirement is that there should be only one copy of „a‟. then use the
concept of virtual base classes.

class B
{
public:
int a;
};
class D1 : virtual public B
{
public:
int b;
};
class D2 : virtual public B
{
public:
int c;
};
class D3 : public D1, public D2
{

};
void main( )
{
D3 O1;
O1.a=5; //valid
}

 Virtual & public keywords can be placed in any order


class D1 : virtual public B
or
class D1 : public virtual B

How a class gets the properties of another class


1. Through inheritance
2. Through the concept of nested class. [That is a class is defined in another class]
3. A class contains objects of other classes as its members. This is called as
containership, containment or aggregation.
Example
class x
{ …..
};
class y
{ ……
};
class z
{ x O1;
y O2;
….
};
13

If class „x‟ has a constructor with arguments, then arguments are passed through the
constructor of „z‟.
Example
class z
{
x O1;
y O2;
public:
z(int a, int b, int c) : O1(a), O2(b)
{
….
}
};
Explicit and implicit calls to the constructor
1.
class B
{
public: If a class inherits from another class,
B(int a)
then the derived class has is-a
{
….. relationship with its base class.
}
};
class D1: public B
{
public:
D(int a, int b) : B(a)
{
…. Explicit call
}
};
2.
class B
{
public: If a class contains objects of another
B(int a)
class, then the enclosing class has has-a
{
….. relationship with the contained class.
}
};
class D1
{
B O1;
public:
D(int a, int b) : O1(a)
{
…. Implicit call
}
};
14

3.
If a class indirectly contains objects of another class through pointer, then it is said to
have holds-a relationship with the other class.
Example
class A
{
….
};
class B
{
A *op1;
….
public:
B(int a, int b, A *p) : op1(p)
{
….
}
};

Das könnte Ihnen auch gefallen