Constructor

 What is constructor? 
A constructor is a special type of member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class because it does not have any return type.


How constructors are different from a normal member function?

A constructor is different from normal functions in following ways: 

  • Constructor has same name as the class itself
  • Constructors don’t have return type
  • A constructor is automatically called when an object is created.
  • It must be placed in public section of class.
  • If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).

 

 

 

Let us understand the types of constructors in C++ by taking a real-world example.

Suppose we went to a shop to buy a marker. When we want to buy a marker, what are the options? 

The first one we go to a shop and say give us a marker. So just saying give us a marker mean that we did not set which brand name and which color, we didn’t mention anything just say we want a marker. So when we said just I want a marker so whatever the frequently sold marker is there in the market or in his shop he will simply hand over that. And this is what a default constructor is!

The second method we go to a shop and say I want a marker a red in color and XYZ brand. So we are mentioning this and he will give us that marker. So in this case we have given the parameters. And this is what a parameterized constructor is!

Then the third one we go to a shop and say we want a marker like this(a physical marker on your hand). So the shopkeeper will see that marker. Okay, and he will give a new marker. So copy of that marker. And that’s what copy constructor is! 



Default Constructor

A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
 
Example
 
    #include <iostream>
    using namespace std;
    class Employee
     {
       public:
            int a, b;
            Employee()
            {
                a = 10;
                b = 20;
                cout<<"Default Constructor Invoked"<<endl;
            }
    };
    int main(void)
    {
        Employee e1; //creating an object of Employee
        cout << "a: " << e1.a << endl
                << "b: " << e1.b;
        return 0;
    }
 
Output
 
 
 

Parameterized Constructor

A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct object.

 Example

 #include <iostream>
using namespace std;
class Employee {
   public:
       int id;//data member (also instance variable)
       string name;//data member(also instance variable)
       float salary;
       Employee(int i, string n, float s)
        {
            id = i;
            name = n;
            salary = s;
        }
       void display()
        {
            cout<<id<<"  "<<name<<"  "<<salary<<endl;
        }
};
int main(void) {
    Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
    Employee e2=Employee(102, "Nakul", 59000);
    e1.display();
    e2.display();
    return 0;
}
 

Output

 

 
 
 

No comments:

Post a Comment