Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - INHERITANCE


Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.


Story :

God has created human beings. So, he has prepared the 'Human' class with states and behaviours. So that human beings could be created based on the stated behaviour.

Now let's say there is a supreme God(more powerful than the God who created Humans). He had decided how all living beings should behave(Human, Animal, Fish).

So, he created LivingBeing class.


Example :



class LivingBeing 
{
    public:
    	void breathe() {
        	cout << "Breathes oxygen from air.";
    	}
};

Story :

Now, the Supreme God(i.e. The creator of the above 'LivingBeing' class) made this rule that all living beings, be it Human or Animal everyone should breathe oxygen from air.

So, the God who has created Human is not going to define the breathe() behaviour again. And he decided to reuse it.

Now, LivingBeing is the parent class and Human class is going to be the child class.


Let us see in the below example.


Example :



#include <iostream>
using namespace std;

class LivingBeing 
{
    public:
    	void breathe() {
        	cout << "Breathes oxygen from air.";
    	}
};

class Human : public LivingBeing
{
	private :
    	string name;
		string food;
		string language;
        
	public :
        Human() {
            
        }
        
        Human(string nme, string fd, string lang) {
		    name = nme;
		    food = fd;
		    language = lang;
	    }

	    void eat() {
		    cout << name << " eats " << food << endl;
	    }

	    void speak() {
		    cout << name << " speaks " << language << endl;
	    }
};

int main() {	
	Human human1("John", "Burger", "English");

    human1.eat();
    human1.speak();

    human1.breathe();

	return 0;
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from air

So, in the above example, we have created a LivingBeing class that just has a Behaviour/Function i.e. breathe().


class LivingBeing
{
	public:
		void breathe() {
			cout << "Breathes oxygen from air.";
		}
};
java_Collections


Then we have created the Human class and the Human class should have the breathe() method from the LivingBeing class.


And since, the breathe() method is defined in the LivingBeing class. There is no point in redefining the breathe() method again.


Rather the Human class reuses it by inheriting all the members of the LivingBeing class.


class Human : public LivingBeing

The syntax is quite simple. We just place the class to be inherited(i.e. LivingBeing) followed by colon(i.e. :).

java_Collections

And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.

java_Collections

And along with eat() and speak() method, breathe() method also becomes a part of Human class.


Now, after creating the human1 object,


Human human1("John", "Burger", "English";
java_Collections


Now when we call the breathe() method from human1 object.


human1.breathe()

We get the below output.


Breathes oxygen from air