You are on page 1of 9

Inheritance

1|Page
One of the most important concepts in object-oriented programming is that of inheritance.
Inheritance is the process by which new classes called derived classes are created from existing classes
called base classes. The derived classes have all the features of the base class and the programmer can
choose to add new features specific to the newly created derived class.
This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an existing class. This
existing class is called the base class, and the new class is referred to as the derived class.
For example, a programmer can create a base class named fruit and define derived classes as mango,
orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of
the base class (fruit) with additional attributes or features specific to these newly created derived
classes. Mango would have its own defined features, orange would have its own defined features,
banana would have its own defined features, etc.

The object being inherited from is called the parent or base, and the object doing the inheriting is
called the child or derived object. In the above picture, fruit is the parent, and both apple and
banana are children.
This concept of Inheritance leads to the concept of polymorphism.
Features or Advantages of Inheritance:
Reusability:
Inheritance helps the code to be reused in many situations. The base class is defined and once it is
compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as
many derived classes from the base class as needed while adding specific features to each derived
class as needed.
Saves Time and Effort:
The above concept of reusability achieved by inheritance saves the programmer time and effort. Since
the main code written can be reused in various situations as needed.

Increases Program Structure which results in greater reliability.

Polymorphism

Inheritance

2|Page

Format forDerived Classes:


A derived class can be defined as follows:
class
{

<derived class> : access_specifier

<base_class>

data members of the derived class ;


member functions of the derived class ;
};
Here,
1. The colon (:) indicates that the class derived_class_name is derived from the class
base_class_name.
2. The access_specifier may be public, private or protected (will be discussed further).
3. If no access_specifier is specified, it is private by default.
4. The access_specifier indicates whether the members of the base class are privately derived or
publicly derived.
Access Control and Inheritance:
A derived class can access all the non-private members of its base class. Thus base-class members that
should not be accessible to the member functions of derived classes should be declared private in the
base class.
We can summarize the different access types according to who can access them in the following way:
Access
public
protected
private
Same class
yes
yes
yes
Derived classes
yes
yes
no
Outside classes
yes
no
no
A derived class inherits all base class methods with the following exceptions:

Constructors, destructors and copy constructors of the base class.


Overloaded operators of the base class.
The friend functions of the base class.

Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through public, protected or
private inheritance. The type of inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance but public inheritance is commonly used. While using
different type of inheritance, following rules are applied:
Public Inheritance: When deriving a class from a public base class, public members of the base class
become public members of the derived class and protected members of the base class become
protected members of the derived class. A base class's private members are never accessible directly
from a derived class, but can be accessed through calls to the public and protected members of the
base class.

Inheritance

3|Page

Protected Inheritance: When deriving from a protected base class, public and protected members of
the base class become protected members of the derived class.
Private Inheritance: When deriving from a private base class, public and protected members of the
base class become private members of the derived class.

Forms of Inheritance
Single Inheritance
If a class is derived from a single base class, it is called as single inheritance.
Multiple Inheritance
If a class is derived from more than one base class, it is known as multiple inheritance
Multilevel Inheritance
The classes can also be derived from the classes that are already derived. This type of
inheritance is called multilevel inheritance.
Hierarchical Inheritance
If a number of classes are derived from a single base class, it is called as hierarchical
inheritance
Hybrid Inheritance
In this type of inheritance, we can have mixture of number of inheritances

1. Single Inheritance
In "single inheritance," a common form of inheritance, classes have only one base class. i.e. One base
class having one derived class.
EXAMPLE:
[A]------->BASE CLASS
void main()
|
{
V
[B]------->DERIVED CLASS
clrscr();
B b;
Program Example: b.m();
class A
{
b.n();
public:
getch();
void m()
}
{
Cout << endl << Hello A
}
};
class B : public A
{
public:

Output:
Hello A
Hello B

void n()
{
Cout << endl << Hello B
}
};

Here object b of Class B can access


public members of Class A.

Inheritance

4|Page

Multilevel Inheritance
In this type of inheritance, there are number of level and it has used in that cases where we want to use
all properties in number of levels according to the requirement. For example, class A inherited in class
B and class B has inherited in class C for class B and so on. Where class A is base class of both B and
C.. In another way we can say B is derived class of A and a base class for C and A is a indirect base
class for C and C is indirect derived class for class A.
Program Example

Class A
{
Public:

void m()
{

Cout << endl << Hello A


}
};
class B : public A
{
public:
void n()
{
Cout << endl << Hello B
}
};
class C : public B
{
public:
void p()
{
Cout << endl << Hello C
}
};

void main()
{
clrscr();
C obj;
obj.m();
obj.n();
obj.p();
getch();
}
Here object obj of Class C can
access public members of both
classes A. and B.
Output:
Hello A
Hello B
Hello C

Inheritance

5|Page

Multiple Inheritance
In this type of inheritance, number of classes has inherited in a single class. Where two or more classes
are, know as base class and one is derive class. This makes the members of the base classes accessible
in the derived class, resulting in better integration and broader re-usability.
Program Example:
Class A
{
Public:
void m()
{
Cout << endl << Hello A
}
};
Class B
{
Public:

void main()
{
clrscr();
C obj;
obj.m();
obj.n();
obj.p();
getch();

void n()
{
Cout << endl << Hello B
}
};

Class C : public A, public B


{
public:
void p()
{
Cout << endl << Hello C
}
};

Here object obj of Class C can


access public members of both
classes A. and B.
Output:
Hello A
Hello B
Hello C

Inheritance

6|Page

Hierarchical Inheritance
When two or more classes are derived from a single base class, then Inheritance is called the
hierarchical inheritance. The representation of the hierarchical inheritance is shown in the following
Fig.

In the above Fig., student is a base class, from which the three classes viz. arts, science and commerce
have been derived.
Now, let us write a program that illustrates the hierarchical inheritance.
Class A
{
Public:

void main()
{

void m()
{

clrscr();
B obj1;
obj1.m();
obj1.n();
C obj2;
obj2.m();
obj2.p();
D obj3;
obj3.m();
obj3.q();

Cout << endl << Hello A


}
};
class B : public A
{
public:
void n()
{
cout<< endl <<Hello B;
}
};
class C : public A
{
public:

getch();
}

void p()
{
cout<< endl <<Hello C;
}
};
class D : public A
{
public:
void p()
{
cout<< endl <<Hello D;
}
};

Output:
Hello A
Hello B
Hello A
Hello C
Hello A
Hello D

Inheritance

7|Page

Hybrid Inhritance
In this type of inheritance, we can have mixture of number of
inheritances but this can generate an error of using same name
function from no of classes, which will bother the compiler to how
to use the functions. Therefore, it will generate errors in the
program. This has known as ambiguity or duplicity.

In the fig. Class A is the base class for both B and C, each of
which is formed with single inheritance. Class D inherits from
both B and C. This enables objects of class D to provide the functionality of both B and C. In multipleinheritance hierarchies, the situation described in fig. is referred to as diamond inheritance. Because
classes B and C each inherit from A, a potential problem exists for D. Class D could contain two
copies of the members of class Aone inherited via class B and one inherited via class C). Such a
situation would be ambiguous and would result in a compilation error, because the compiler would not
know which version of the members from class A to use.
Program Example - demonstrates the ambiguity that can occur in diamond inheritance.
class A
{
public:
void m()
{
cout<< endl <<Hello A;
}
};
class B : public A
{
public:
void n()
{
cout<< endl <<Hello B;
}
};
class C : public A
{
public:
void p()
{
cout<< endl <<Hello C;
}
};

Inheritance

8|Page

class D : public B, public C


{
public:
void q()
{
cout<< endl <<Hello B;
}
};
void main()
{
1.
clrscr();
2.
D obj;
3.
obj.m();
4.
getch();
}

// ERROR - Ambiguous

Here, class D inherited from class B and class C. In the main() program, at line #3, an obj of class D
calls the function m() of class A. During calling the function m(), the compiler does not know in
advance which version of function m() from class A to use ( either via from class B or via from class
C). This leads to compilation error which generates the ambiguous situation.
To solve this problem, C++ allows us to use virtual inheritance.

Inheritance

9|Page

Eliminating Duplicate Subobjects with virtual Base-Class Inheritance


The problem of duplicate subobjects is resolved with virtual inheritance. When a base class is inherited
as virtual, only one subobject will appear in the derived classa process called virtual base-class
inheritance. Revised program to use a virtual base class.
class A
{
public:
void m()
{
cout<< endl <<Hello A;
}
};
class B : public virtual A
{
public:
void n()
{
cout<< endl <<Hello B;
}
};
class C : virtual public A
{
public:
void p()
{
cout<< endl <<Hello C;
}
};
class D : public B, public C
{
public:
void q()
{
cout<< endl <<Hello B;
}
};
void main()
{
1.
clrscr();
2.
D obj;
3.
obj.m();
4.
getch();
}

Output: Hello A

// NO ERROR Only one copy of function m() is created

You might also like