Alternatively, we can say that a square is a shape, a circle is also a shape and rectangle is also a shape. Shapes exist in the form of square, a circle, rectangle, polygons etc. We can move to more realistic example.Ĭonsider the example of a shape. Now we have basic understanding of how inheritance basically works. In the same way we called parentfunction() from the other child C. Still we are able to call it because B is inheriting A. We have firstĬalled parentfunction() from object b although class B doesn’t have it. Note, here we do not have defined any object of the parent class A but yet we will be able to access its function parentfunction() through the base classes. In the main function we have defined an object b of class B and object c of class c. In the child classes B and C we have defined one function each, named childfunction(). In Example 1, we have defined a function named parentfunction() inside the parent class A. This means that class B is inheriting class A publicly and is able to access all the members and functions of class A. We will have a class A which will be the parent class and two child classes B and C. We will explain the above mentioned concept with simplest possible example. Child class is often known as the derived class. One parent class can have as many child classes as it want. In child class you have access to all the attributes and functions defined in the base class. Parent class is also called base classĬhild class is a class that inherits base class. Inheritance fosters reusability and code is more organized. The parent class and you will not have to write that piece of code again. As aforementioned, you inherit some attributes from you parents but in case of parent class, all the inheriting or child classes will inherit all the attributes of Following are some extremely important concepts that need to be understood beforeĪ parent class is a class which acts as a parent for the classes which inherit from it. A typical example of which is that a square ‘is a’ shape, a man ‘is a’ human, a pilot ‘is an’ employee and so on. Inheritance is based on the principle of is-a relationship. In C++ we can define a parent child relationship between classes in such a way that child class will inherit all theĪttributes of its parent class plus it will have its own attributes and functions. Replace human beings with objects and you will understand what does that mean. Inheritance in programming is somewhat similar concept. Yes, exceptions are always there, but it has been genetically proved that human being inherit certain characteristics of If your parents are tall, it is more likely that you will be taller than a person whose parents are shorter. Take your example you inherit several characteristicsįrom your parents. Think of inheritance from a general perspective. Inheritance allows you to organize and reuse code effectively. Inheritance is an extremely useful and exciting object oriented programming feature.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |