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




C++ - ABSTRACT CLASSES & VIRTUAL METHOD


An abstract class in C++ is a class which cannot be instantiated and can have methods (i.e. Virtual Methods) which are not defined.


  1. To make a class abstract, just create a method that is not defined (i.e. Virtual Method).


  2. We cannot create objects of an Abstract class.


  3. The classes extending an abstract class has to define the undefined methods(i.e. the virtual methods).

We will clear the above points with the example :


Story :

The supreme god decided that in order to communicate, every living being should make some sound. i.e Humans will talk, dogs will bark, cats meao e.t.c.

Now, there was no way he could define the sound behavior as it will be different for all living beings. So he made this rule that every living being should have a sound behavior but the sound they makes will be determined by their own gods. And thus the class was prepared by Master God:

Example :



class LivingBeing 
{
    public:
    	void breathe() {
        	cout << "Breathes oxygen from air." << endl;
    	}
    	
    	virtual void sound() = 0; // The sound method is incomplete.
};



Now we can see the virtual keyword used in the undefined method sound(), and is initialised with 0.


virtual void sound() = 0;

And if a method is undefined or is marked as virtual the class is considered as Abstract.


Thus the LivingBeing class is also Abstract because it has a virtual method/behaviour(Incomplete method/behaviour).


And since the class is incomplete we cannot create any objects out of it.


In other words abstract classes cannot be instantiated.


Then, what is the purpose of this class from which we cannot create any objects? We will see it below:


Human Being Creator


Example :



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;
    	}
    
    	void sound()
    	{
    		cout << "Humans will speak" << endl; // This method has to be defined.
    	}
};



So, in the above example we have an abstract class LivingBeing.


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

		virtual void sound() = 0; // The sound method is incomplete.
};

So, as we know the child classes that inherits from it must define the abstract method sound().


virtual void sound() = 0;

And so we have done in the Human class.


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;
		}

		void sound()
		{
			cout << "Humans will speak" << endl; // This method has to be defined.
		}
};

We have defined the sound() method using the override keyword,


void sound()
{
	cout << "Humans will speak" << endl; // This method has to be defined.
}

Story :

So, the Human creator god is bound to define the sound() method/behaviour as he is extending the LivingBeing class. Naturally the purpose of the supreme god was solved. As he wanted all living beings should make some sound. What kind of sound? will be determined by their own Gods.

If the Human creator god wouldn't have defined the sound() method/behaviour the code would have ended up with some kind of error.

Now, let us write the complete code for the above example :


Example :



#include <iostream>
using namespace std;

class LivingBeing 
{
    public:
    	void breathe() {
        	cout << "Breathes oxygen from air." << endl;
    	}
    	
    	virtual void sound() = 0; // The sound method is incomplete.
};

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;
    	}
    
    	void sound()
    	{
    		cout << "Humans will speak" << endl; // This method has to be defined.
    	}
};

int main() {	

    Human human("John", "Burger", "English");
    human.breathe();
    human.sound();

	return 0;
}


Output :



  Breathes oxygen.
  Humans will speak