Saturday, November 12, 2011

C++ Tutorial: Inheritance

Inheritance is a process of organizing information in a hierarchical form. Through inheritance we can crate new class, called derived class, from existing class called basic class. The derived class inherits all the features of the base class and can add new extra features. Inheritance allows new classes to be built from existing and less specialized class instead of writing from the start. So classes are created by first inheriting all the features(members) from base class and adding new features in the derived class (new Class).
Inheritance is an essential part of object oriented programming. Inheritance provides code reusability. Reusability is one of the striking features of C++. We don’t have to write code from the scratch each and every time while writing similar type of construct. The reused code is, of course, already tested, more reliable and less error prone. Also it saves time, effort and ultimately money. The C++ classes can be reused in many ways. Among them, inheritance is one of the reusability  feature in C++. Because of reusability, the classes distributed as library can be extended to form new class without modifying the library.

Base and Derived Class
Inheritance is a technique of building new classes from the existing classes. It is a technique of organizing information in a hierarchical form just like a child inheriting the features of its parents. In the process of inheritance, the existing classes that are used to derive new classes are called base classes and the new classes derived from existing classes are called derived classes. When a class is derived from a base class, the derived class inherits all the characteristics of base class and can add new features as refinements and improvements.
The great advantage of inheritance is reusability, which ensures ease of distributing class libraries. A base class is also called ancestor, parent or super class and derived class is also called as descendent, child or subclass.
Derivation is often represented graphically by a pointer from the derived class to its base class. This arrow indicates that this class is derived from the class pointed by the arrow. Because of inheritance, the base class is not changed. After inheritance, we can create objects of base class and object of derived class separately. The inheritance does not work in reverse order. The features in derived class cannot be accessed by the base class object. However, the features from base class are accessible in derived class.

1 comment: