In simple words it is the Constructor which is helping the object to be created.
There are two types of 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,
#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;
Well ! It is hidden from you
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).
#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; }
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,
Next, let us look at the Constructors with arguments.
Lets modify the Human class with constructors:
#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() { }
And the other is the constructor with three arguments:
Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; }
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.
#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; }
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.
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;
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.
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.
And the argumentHuman1 object is populated with the values.
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");