C++ inheritance
C++ inheritance
The most important concept in object-oriented programming is inheritance. Generally, Inheritance means something(characteristic or anything else) passed down from the parent to the children.
In the same way in the c++ language, derive from the other class(base ) is known as inheritance. The base class is known as parent class and the drived is known as child class.
The derived class has all the features of the base(parent) class and also has its own features.
Why inheritance used in c++?
Let’s take an example to understand the concept of inheritance.
Suppose three characters in your project gunman, swordman, and fighterman.you will create three class of each member. Notice that all the characters have common characteristics like speak, eat, age, and sleep.
If we make one common class of the common characteristics of all three characters then work will be easier . Otherwise, write code differently for all three characters. It is time-consuming and memory consuming. The chance of the error becomes high.
We take common properties like sleep, eat, age, and, speak and make one class. This class is used when any character is used. This is called the base or parent class in this case which has all the features that are found in the derived classes or child classes.
Here is some graphics work which helps in understanding the concept of inheritance.
Take common properties and make a base class
How to make derive classes?
Now, its time to take different properties of one character like gunman has gun other characters do not have gun. Derive class created of gunman with a property that he has a gun.
Likewise, take another character swordman, and make the derived with an extra property that only specifies with swordman. He has a sword.
Take fighter man character: He has the ability to fight with enemies and make the derived of the fighterman.
Now,one is the parent or base class and three derived or child classes. The derived class has all the properties of the parent but the parent does not have all the features of the child class.
How to implement inheritance in the c++ programming?
The syntax of the inheritance in c++:
In the above code, base class or parent class with three derived classes gunman, swordman, and fighter man.
The derived class declares with the class name followed by the colon then public keyword with parent.
Comments
Post a Comment