Fortunately,
C++ strongly supports the concept of re-usability. The C++ classes can be reused
in several ways. Once a class has been written and tested, it can be adapted by
other programmers to suite their requirements. This is basically done by
creating new class, reusing the properties of the existing once. The mechanism
of deriving a new class from an old one is called inheritance or derivation.
BASE CLASS &
DERIVED CLASS
When creating a class, instead of writing completely new data
members and member function, the programmer can designate that the new class
should inherit the members of an existing class. The existing class is called
the base class, and the new class is referred to as the derived class.
The
derived class inherits some or all of the traits from the base class. A class
can also inherit properties from more than one class or from more than one
level.
DEFINING DERIVED
CLASSES
A derived
class can be defined by specifying its relationship with the base class in
addition to its own details. The general form of defining a derived class is:
class derived –name : visibility-mode
base-class-name
{
……//
…..// members of derived class
…..//
}
The colon indicates that the derived-class-name is
derived from the base- class –name. The visibility-mode is optional and, 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.
TYPES OF INHERITANCE
- Single Inheritance: A derived class with one base class, is called single inheritance.
- Multiple Inheritance: One class with several base classes is called multiple inheritance.
- Hierarchical Inheritance: One class may be inherited by more than one class, this process is known as hierarchical class.
- Multilevel Inheritance: The mechanism of deriving a class from another derived class is known as multilevel inheritance.
- Hybrid Inheritance: Hybrid inheritance is the combination of more than one type of inheritance.
0 comments:
Post a Comment