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.

                
c-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.


                            
inheritance-cpp




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.


Example of inheritance in c++ programming:


#include <iostream>
using namespace std;

class character                                    //create a class of character
{
     public:                                            //public data members
        string profession;
        int age;

        character() {                                            //constructor
profession=" nothing ";
age=20;
}
        void display()                                            //function
        {
             cout << "My profession is: " << profession << endl;
             cout << "My age is: " << age << endl;
             cout << "I can eat." << endl; 
         cout << "I can sleep." << endl;
        }
        
};


class gunman : public character                    //derived from character
{
    public:
       void gun() { cout << "I have ability to fight with gun " << endl; }
};


class fightman : public character                                           //derived from character
{
    public:
       void fight() { cout << "I have power to fight" << endl; }
};

class swordman : public character                                    //derived from character      
{
    public:
       void sword() { cout << "I ability to fight with sword ." << endl; }
};

int main()
{
     gunman man;
     man.profession = "gunman";
     man.age = 26;
     man.display();
     man.gun();

     fightman Fighter;
     Fighter.profession = "Fighter";
     Fighter.age = 30;
     Fighter.display();
     Fighter.fight();

     return 0;
}

Output:

My profession is: gunman My age is: 26 I can eat. I can sleep. I have ability to fight with gun My profession is: Fighter My age is: 30 I can eat. I can sleep. I have power to fight

In the above code, one base character and derive three classes gunman,fightman,and swordman. During the inheritance (:) colon is used with access specifiers and at last class name of the base class.
The compiler identifies the base and derive by the syntax and create a base first then derive class created.

Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

How to format in HTML