c++

Constructors in C++

In this tutorial, we are going to learn about constructors.

Constructor is a special member function of a class, which is executed whenever we create new objects of that class.

A constructor have a very different syntax as compared to other member functions.

class students {
     public:
     // create a constructor
     students() {
     // code
     }
};

You may have noticed in the above code that the constructor have same name as its class i.e students .

Also a constructor don’t have any return type, not even void .

photo1_constructors

Default constructor

A default constructor do not contain any parameters, like the constructor student() in the above example.

Following example shows how a default constructor invoked.

Example:

#include 
using namespace std;
class students
{
 public:
 students()
 {
 cout<<"Default Constructor Invoked"<<endl;
 }
};
int main()
{
 students s1,s2;
 return 0;
} 

Output:

Default Constructor Invoked
Default Constructor Invoked

Explanation:: in above example, we have a default constructor students(), having a line of code in it. When we created two objects s1 and s2 in main(), the constructor got automatically called and executed two times without being called explicitly.


Parameterized Constructor

In C++, we can also have parameters in a constructor known as Parameterized constructor.

Look at the following example

Example:

#include <iostream>
using namespace std;
class students {
 public:
     string name;
     int roll_no;
     char grade;
     students(string n,int r, char g)
 {
     name = n;
     roll_no = r;
     grade = g;
 }
 void display()
 {
    cout<< name <<" "<< roll_no<<" "<< grade<< endl;
 }
};
int main(void) {
     students s1 =students(Sonoo,89,'A');
     students s2=students("Monoo",50,'B');
     s1.display();
     s2.display();
     return 0;
} 

Output:

Sonoo 89 A
Monoo 50 B

in above example, the constructor students is now parameterised hence we can pass values in it and get the result without calling them explicitly, just by creating objects.

Note:If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.


Copy Constructor

A copy constructor is a member function which initializes an object using another object of same class.

The syntax for copy constructor is given below.

class students{
    ... .. ...;
    public:
        students(students &obj){ //copy constructor
            ... .. ...;
}
};

Syntax:

className targetObject(sourceObject);

or

className targetObject=sourceObject;

lets understand it with help of an example.

Example:

In this program, we have a parameterized constructor and a copy constructor.

class students{
    int roll_no;
    char grade;
    public:
        students(int a, char b){ //parameterized constructor
            roll_no=a;
            grade=b;
}
        students(students &t){ //copy constructor
            roll_no=t.roll_no;
            grade=t.grade;
}
        int display(){
            cout<<"roll no : "<< roll_no << endl;
            cout<<"grade : "<< grade<< endl;
            return 0;
}
};
int main(){
        cout<<"parameterized constructor"<< endl;
        students s(12,'A');

 //object created and value passed to parameterized constructor

         s.display();
        cout<<"copy constructor"<< endl;
        students c(t);

//object created and value passed to copy constructor

         c.display();
         return 0;
}

In this program , first we are passing values to parameterized constructor by creating an object, S. then we are passing the same object to a copy constructor by creating its object, C. Hence, values of object S got copied in C.

Output:

parameterized constructor
roll no : 12
grade : A
copy constructor
roll no : 12
grade : A

When we need copy constructor ?

In C++, a Copy Constructor may be called in following cases:

  1. . When an object of the class is returned by value.
  2. When an object of the class is passed (to a function) by value as an argument.
  3. When an object is constructed based on another object of the same class.
  4. When the compiler generates a temporary object.