How Many Types of Constructor are Available in C++? Explain them



There are mainly three types of constructor available in C++ are the following:


1. Default Constructor - A constructor that accepts no parameter is known as default constructor. If no constructor is defined then the compiler supplies a default constructor. It means that C++ compiler has an implicit constructor which create an object even though it was not defined in the same class.



student :: student(){}
student :: student()
{
roll=0;
marks=0.0;
}


2. Parameterized Constructor - A constructor that receives one or more than arguments/parameter, is called a parameterized constructor.


student :: student(int r, float m)
{
roll=r;
marks=m;
}


3. Copy Constructor - A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates an alias of the passed object or reference to the passed object.


student :: student(student &t)
{
roll=t.roll;
marks=t.marks;
}



Post a Comment

1 Comments

  1. What about the move constructor? Unlike the copy constructor which copies the member variables, the move constructor moves them completely to a new variable

    ReplyDelete