In real life inheritance is what we acquire from our parents. For example our lifestyle, way of talking etc…
In Object Oriented Programming Inheritance is the process by which objects of one class acquire the properties and functionality of objects of another class. It supports the concept of hierarchical classification. For example, the bird robin is a part of the class flying bird which is again a part of the class bird.
The principle behind this sort of division is that each derived class shares common characteristics with the class from which it is derived. In Object Oriented Programming (OOP), the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. The real appeal and power of the inheritance mechanism is that it allows the programmer to reuse class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any undesirable side effects into the rest of the classes. It is noted that each sub-class defines only those features that are unique to it. Without the use of classification, each class would have to explicitly include
all of its features.
For example
class Bird { char fly[30]; }; class Parrot: Bird { int eatMango; };
Now the parrot inherits the functionality of bird to fly in the air. So what ever the fly value is, the parrot will have the same fly functionality.
Inheritance
Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, the bird robin is a part of the class flying bird which is again a part of the class bird. The principle behind this sort of division is that each derived class shares common characteristics with the class from which it is derived. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. The real appeal and power of the inheritance mechanism is that it allows the programmer to reuse class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any undesirable side effects into the rest of the classes. It is noted that each sub-class defines only those features that are unique to it. Without the use of classification, each class would have to explicitly include all of its features.
class Circle : public Shape { public: Circle(); Draw(); private: int rad; }; class Rectangle : public Shape { public: Rectangle(); draw(); private: int width, length; };
Circle and rectangle would inherit the operations and datafields from Shape, so it is not necessary to repeat them. I’ll just assume that the relevant operations can be straightforwardly written. Note that the class ‘Shape’ is not a class that you’d have instances of. It is just defined so that specific shapes like circle and rectangle can be defined much more simply. Such classes are referred to as abstract classes.