C++ Inheritance introduction
Introduction:
The Reusability is an another important feature of the Object Oriented programming. it is always good if we could re-use something that already exists rather than trying to create the same all over again and again. It would not only save money and time and but also reduce frustration
and also increase the reliability. for instance the re-use of a class that has already been tested, debugged and used many times can save us the effort of developing and testing the same again.
Fortunately , C++ strongly support the concept of reusability(inheritance). The c++ language classes can be reused in several ways. once a class has been written and tested, it can be use by other programmers to suit their requirements . This is basically done by creating new classes, reusing the properties of the existing class. The mechanism of deriving a new class by using an old one is called inheritance. The old class is referred to as the base class or parent class and the new one is called the derived class or subclass
The derived class inherits some or all of the features of the base class. A class can also inherit properties from more than one class and of from more than one level.
A derived class also known as(child class) with only one base class, is called single level inheritance and one with several base classes is called multiple inheritance. on the other hand , the features of one class may be inherited by more than one class. This process is know as hierarchical inheritance.
in the below figure we will show the types of inheritance
Defining derived Class:
A derived class can be defined by specifying its relationship with the base in addition to its own details. The general form of defining a derived class is
class derived-class-name : visibility-mode base-class-name
{
………..
………..
………..
………..
};
|
The colon indicates that the derived class(subclass) name is derived from the base class(super class) name
The visibility-mode is optional and if present, may be either public or the private . but The default visibility mode is private. visibility mode specifies the features of the base class are privately derived or publicly derived to the derived class.
Let us show example
class ABC: private XYZ
{
members of ABC
};
class ABC: public XYZ
{
members of ABC
};
class ABC: XYZ
{
members of ABC
};
|
When a base class is privately inherited by a derived class, public members of the base class becomes private members of the derived class and therefore the public members of the base class can only be accessed by the member function of the derived class.
They are inaccessible to the objects of the derived class. But Remember that , a public member of a class can be accessed by its own objects using the dot(.) operator. The result is that no member of the base class is accessible by using the objects of the derived class.
on the other hand, when the base class is publicly inherited, public members of the base class will be become public members of the derived class and therefore they are accessible by the object of the derived class. in both the class, but the private members are not inherited and therefore, the private members of a base class will never become the members of its derived class
In the inheritance some data elements of the base class and members functions are inherited into the derived class. you can add your own data and member functions and thus extend the functionality of the base class. inheritance, when used to modify and extend the capabilities of the existing classes, becomes a very powerful tool for incremental program development