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




C++ - CONSTRUCTOR


In simple words it is the Constructor which is helping the object to be created.


There are two types of Constructor :

  1. Default Constructor or the no argument Constructor.

  2. Constructor with arguments or Parameterized Constructor.

Constructor in detail


Default Constructor or the no argument Constructor


By definition, it is said that it is the Constructor that helps an object to be created.


But the example, we have seen earlier, there was not constructor defined.


Can't remember ?


Let's rewrite the example,


Human Class


Example :



#include <iostream>
using namespace std;

class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
	    void setName(string nme) {
            name = nme;
        }

        string getFood() {
            return food;
        }

        void setFood(string fd) {
            food = fd;
        }

        string getLanguage() {
            return language;
        }

        void setLanguage(string lang) {
            language = lang;
        }
	
		void eat()
		{
			cout << name << " eats " << food << endl;
		}

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

int main() {	
	Human human;
	
	return 0;
}



We have seen how to create an object of Human class,


Human human;

But where is the Constructor of Human class?


Well ! It is hidden from you


So, how does it look like?


It looks like a C++ method. With a little difference.


Human()
{

}

Well ! This is how a Constructor of Human class looks like. It should always have the class name as the constructor name, with no return type. It is also called as the default constructor.


Now, let us modify the above example and include a default constructor in it(Just to check it is really there).


Default Constructor


Example :



#include <iostream>
using namespace std;

class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
        Human(){
            cout << "Default constructor is called everytime a new object is created";
        }
};

int main() {	
	Human human;

	return 0;
}


Output :



  Default constructor is called everytime a new object is created

So, in the above code, we have removed all the getters and setters, just to show that the default constructor is called every time an object is created.


Below is the default constructor from the above code,


Human(){
	cout << "Default constructor is called everytime a new object is created";
}

So, the main method has nothing other than a line that is we have created a Human object,


Human human;

And when the human object is created, the default constructor is called, printing the below output,

Output :



  Default constructor is called everytime a new object is created

Next, let us look at the Constructors with arguments.


Constructors with arguments or Parameterised Constructors


Lets modify the Human class with constructors:


Example :



#include <iostream>
using namespace std;

class Human
{
	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;
	    }
};



So, in the above class we have two contructors. One with no arguments(default constructor), which the compiler inserted by default in the previous Human class.


Human()
{

}

Note : If you are using the Constructor with arguments, then you must specify the default constructor.

And the other is the constructor with three arguments:


Human(string nme, string fd, string lang)
{
	name = nme;
	food = fd;
	language = lang;
}

How to create an object with the constructor with arguments?


Say we are going to create two Human objects out of the above Human class. i.e. John from America and Wang Chu from Bhutan.


Example :



#include <iostream>
using namespace std;

class Human
{
	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; //Calls the no-argument/default Constructor
	Human human2; //Calls the no-argument/default Constructor
	Human argumentHuman1("John","Burger","English"); //Calls the constructor with arguments
	Human argumentHuman2("Wang Chu","Chowmein","Dzongkha"); //Calls the constructor with arguments
		
	argumentHuman1.eat();
	argumentHuman1.speak();
		
	argumentHuman2.eat();
	argumentHuman2.speak();

	return 0;
}


Output :



  John eats Burger
  John speaks English
  Wang Chu eats Chowmein
  Wang Chu speaks Dzongkha

Lets see whats happens in the above code.


So when we create the first object of Human class,


Human human1;

The name, food and language variables of human1 object are not assigned any values.

java_Collections

Because human1 is created using the default constructor,


Human() {

}

Similarly, the name, food and language variables of human2 object are not assigned any values.


Human human2;
java_Collections


Again let us relook at the below Image:


Now in the next line,


Human argumentHuman1("John","Burger","English");

We have created an object of Human class with some values. And what happens is the parameterised constructor gets called.


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

So, "John" gets assigned to nme, Burger to fd and English to lang.

java_Collections

Next, if you see the lines inside the constructor.


	name = nme;
	food = fd;
	language = lang;

The value inside nme i.e "John" gets assigned to name.

java_Collections

Note : name is referring to the name property of Human class.

And the argumentHuman1 object is populated with the values.

java_Collections

Similarly, Burger is assigned to this.food, which is the food property of Human class and English is assigned to language property of Human class.


Human argumentHuman2("Wang Chu","Chowmein","Dzongkha");
java_Collections