You are on page 1of 4

Advantages of inheritance

1. Reusability: Inheritance help 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 form
the base class as needed while adding specific features to each derived class as
needed.
2. Save time & 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 situation as needed.
3. Data hiding: the base class can decide to keep some data private so that it cannot
be altered by the derived class.
4. Extensibility: it extensibility the base class logic as per business logic of the
derived class.
5. Easy to understand: It’s easy to understand large program with use of inheritance.
6. Reliability: increases program structure which result in greater reliability.
7. Maintainability: It is easy to debug a program when divided in parts. Inheritance
provides an opportunity to capture the program.

Abstract class
An abstract class is a class that serves only as a base class form which classes are derived.
No object of an abstract base class are created. A class that contains at least one pure
virtual function is said to be abstract. Because an abstract class contains one or more
functions for which there I no definition (that is, a pure virtual function), no object of an
abstract class may be created. Instead, an abstract class constitutes an incomplete type
that is used as a foundation for derived classes
Although we cannot create object of an abstract class, we can create pointers and
references to an abstract class. This allows abstract classes to support run-time
polymorphism, which relies upon base-class pointers and references to select the proper
virtual function

Ambiguity in multiple inheritance

Base2
{
…..
public:
disp( );
};
Base1
{
…..
public:
disp( );
};
Two base classes may have member functions with the same name, while a class derived
form both base classes has no. function with this name. how do objects of the derived
class access the correct base class function?

Program to illustrate ambiguities in multiple inheritance


#include<iostream.h>
Class base1
{
public:
void dip(void)
{
cout<<”base1”<<end1’
}
}:
class base2
{
public
void dip(void)
{
cout<<”base2”<<end1’
}
}:
class base2
{
class derived:public base1,public base2
public:
void display(void)
{
cout<<”base1”<<end1’
}
}:
void main ( )
{
derived der;
der.disp( );//ambiguous
}

Here the reference to disp( ) is ambiguous because the computer does not know whether
disp( )refer to the member in the class base1 or base2. the problem is resolved using the
scope-resolution operator to specify the class in which the function lies.
Void main( )
{
derived der;
der.base1::disp( ); //invokes disp( ) of class base1
der.base1::disp( ); //invokes disp( ) of class base1
}

Program to illustrate resolving ambiguities in multiple inheritance


#include<iostream.h>
#include<conio.h>
Class base1
{
public:
void dip(void)
{
cout<<”base1”<<end1’
}
}:
class base2
{
public
void dip(void)
{
cout<<”base2”<<end1’
}
}:
class base2
{
class derived:public base1,public base2
public:
void display(void)
{
cout<<”base1”<<end1’
}
}:
void main ( )
{
derived der;
der.base1::disp( );
der.base2::disp( );
der.display( );
getch( );
}

Output
base1
base2
derived
Types of derivation
inheritance is the process of deriving new class from an existing class. While
deriving the new class from the base class, the access control specifier giving the total
control over the data members and member functions of the base class. A derived class
can be define with one of the access specifiers. There are three access specifiers private,
public and protected

Public inheritance
When you inherit a base class publicly, all member keep their original access
specifications. Private member stay privete, protected, members stay protected, and
public members stay public.
The general syntax of the public derivation is :

Class base_class_name {
---------------------
---------------------
};
Class derived_class_name:public base_class name
{
--------------------
--------------------
};
The thing to remember:
1. Derived classes can not directly access private members of the base class.
2. The protected access specifier allows deriver classes to directly access members
of the base class while not exposing those members to the public.
3. The derived class uses access specifiers form the base class
4. The outside uses access specifiers form the derived class.

To summarize in table form:


Public inheritance
Base access specifie Derived access specifier Derived class access? Public
access?
Public Public Yes Yes
Private Private No No

Protected Protected Yes No

You might also like