C++ Constructor basic concept



In this tutorial we will learnt about C++ constructor basic concept. Tutorial explains you everything about constructor in C++ in easier language.

C++ Constructor basic concept

  1. C++ constructors are the special member function of the class which are used to initialize the objects of that class
  2. The constructor of class is automatically called immediately after the creation of the object.
  3. Name of the constructor should be exactly same as that of name of the class.
  4. C++ constructor is called only once in a lifetime of object when object is created.
  5. C++ constructors can be overloaded
  6. C++ constructors does not return any value so constructor have no return type.
  7. C++ constructor does not return even void as return type.

C++ Constructor Types

  1. Default Constructor
  2. Parametrized Constructor
  3. Copy Constructor

We will discuss different C++ constructor types in detail –

C++ Constructor Types #1 : Default Constructor

  1. If the programmer does not specify the constructor in the program then compiler provides the default constructor.
  2. In C++ we can overload the default compiler generated constructor
  3. In both cases (user created default constructor or default constructor generated by compiler), the default constructor is always parameterless.

Syntax

class_name() {
-----
-----
}

Example of Default Constructor

Let us take the example of class Marks which contains the marks of two subjects Maths and Science.

#include<iostream>
using namespace std;

class Marks
{
public:
   int maths;
   int science;
            
   //Default Constructor
   Marks() {
      maths=0;
      science=0;
   }

   display() {
      cout << "Maths :  " << maths <<endl;
      cout << "Science :" << science << endl;
   }
};
   
int main() {
  //invoke Default Constructor
  Marks m;
  m.display();
  return 0;
}

Output :

Maths :  0
Science : 0

C++ Constructor Types #2 : Parametrized Constructor

This type of constructor can take the parameters.

Syntax

class_name(Argument_List) {
-----
-----
}

Example of Parametrized Constructor

Let us take the example of class ‘Marks’ which contains the marks of two subjects Maths and Science.

#include<iostream>
using namespace std;

class Marks
{
public:
   int maths;
   int science;
            
   //Parametrized Constructor
   Marks(int mark1,int mark2) {
      maths = mark1;
      science = mark2;
   }

   display() {
      cout << "Maths :  " << maths <<endl;
      cout << "Science :" << science << endl;
   }
};
   
int main() {
  //invoke Parametrized Constructor
  Marks m(90,85);
  m.display();
  return 0;
}

Output

Maths :  90
Science : 85

C++ Constructor Types #3 : Copy Constructor

  1. All member values of one object can be assigned to the other object using copy constructor.
  2. For copying the object values, both objects must belong to same class.

Syntax

class_Name (const class_Name &obj) {
   // body of constructor 
}

Example of Copy Constructor

Let us take the example of class ‘Marks’ which contains the marks of two subjects Maths and Science.

#include<iostream>
using namespace std;

class marks
{
public:
    int maths;
    int science;
          
    //Default Constructor
    marks(){
      maths=0;
      science=0;
    }
          
    //Copy Constructor
    marks(const marks &obj){
      maths=obj.maths;
      science=obj.science;
    }
          
    display(){
      cout<<"Maths :   " << maths
      cout<<"Science : " << science;
    }
};

int main(){
    marks m1;

    /*default constructor gets called 
         for initialization of m1  */      
    marks m2(const marks &m1);

    //invoke Copy Constructor
    m2.display();

    return 0;
}

Output

Maths :  0
Science : 0