How Many Types of Constructor are Available in C++? Explain them - CodeTextPro | Free Web Tutorial

Codetextpro Provides free Web tutorial, HTML, CSS, C, C++, Java, JavaScript, PHP, MySql, Python, Program, Programming Notes, Programming Questions and more.

Programming

Saturday, August 10, 2019

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;
}



No comments:

Post a Comment