Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions (except the constructors and destructor) of the class it is based on.
In object-oriented programming, inheritance
is used to create an “is a” relationship between classes.
Inheritance is the mechanism, is used for deriving new
classes from an existing (old) classes. In inheritance, the derived class
inherits/access some or all the properties of the base class and has its own
properties.
Inheritance involves a base class and a derived class. The base
class is the general class and the derived class is the specialized
class. The derived class is based on, or derived from, the base class.
An existing class is known as “Base class” and
the new class is called as “Derived class”. The derived class can
be created by extending with the features of its base class, that is, a derived
class combines the features of both base and derived classes.
Inheritance provides reusability of code, that is, the base
class code can be used from the derived class.
Defining Derived Classes
A derived class is defined as follows:
Syntax:-
class derived_classname : access_specifier base_classname
{
Data members;
Member functions;
};
Here, the access_specifier is also known as visibility-mode.
It affects how the members of the base class are inherited by the derived
class. It can be either private, public or protected.
Private :
It is the highest level of data hiding. When a base class is
privately inherited, then public members become private in derived class. The
private members can’t be inherited.
Public :
It is the lowest and most open level of data hiding. When a
base class is publicly inherited, then the public members become public in derived
class. The private members can’t be inherited.
Protected :
It is less restrictive than private, and more restrictive
than public. The protected members of a class are accessed in the derived
class.
Protected members of a base class
are like private members, but they may be accessed by derived classes. The base
class access specification determines how private, public, and protected base
class members are accessed when they are inherited by the derived classes.
0 comments:
Post a Comment