Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C++

Learn step by step



Constructors

Constructors are the special class functions which performs initialization of every object. Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to object.

class A
{
int x;
public:
A(); //Constructor
};

While defining a contructor we must remeber that the name of constructor will be same as the name of the class, and contructors never have a return type.

Constructors can be defined either inside class definition or outside class definition using class name and scope resolution(::) operator.

class A
{
int i;
public:
A(); //Constructor declared
};

A::A() // Constructor definition
{
i=1;
}
Types of Constructors

Constructors are of three types :

  • Default Constructor
  • Parametrized Constructor
  • Copy COnstructor

Default Constructor

It is a constructor which doesn't take any argument.
Syntax :

class_name ()
{ Constructor Definition }

class Cube
{
int side;
public:
Cube()
{
side=10;
}
};

int main()
{
Cube c;
cout << c.side;
}

Output

10

In this case, as the object is created constructor is called which initializes its data members.

Default constructor is important for initialization of object members, that even if we don't define a constructor explicitly, the compiler will provide a default constructor implicitly/automatically.


class Cube
{
int side;
};

int main()
{
Cube c;
cout << c.side;
}

Output


0

In that case, default constructor provided by compiler will be called which will initialize the object data members to default value, in this case it will be 0.

Parametrized Constructor

Parametrized Constructor are the constructors with parameter. Using this Constructor we can provide different values to data members of different objects, by passing appropriate values as argument.


class Cube
{
int side;
public:
Cube(int x)
{
side=x;
}
};

int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
cout << c1.side;
cout << c2.side;
cout << c3.side;
}

Output


10   20   30

By using this in above case, we have initialized 3 objects with values that are user defined. We can have any number of parameters in a parameterized constructor.

Copy Constructor

It is a type of constructor which is used to create a copy of an already existing object of a class type. It is usually in the form X (X&), where X is the class name.he compiler provides a default Copy Constructor to all the classes.
Syntax of Copy Constructor

class-name (class-name &amp;)
{
. . . .
}

As it is used to create an object, that is why it is called a constructor. It creates a new object, which is exact copy of the existing copy, that is why it is called copy constructor.

Fig 1.

Constructor Overloading

Just like other member functions, constructor is also a type of function so, constructors can also be overloaded. Infact when we have both default and parameterized constructors defined in our class we are having Overloaded Constructors, one with no parameter and other one with parameter.

We can have any number of Constructors in a class that differ in parameter list.

class Student
{
int rollno;
string name;
public:
Student(int x)
{
rollno=x;
name="None";
}
Student(int x, string str)
{
rollno=x ;
name=str ;
}
};

int main()
{
Student A(10);
Student B(11,"Ram");
}

Above case we have defined 2 constructors with different parameters, hence overloading the constructors.

One more important thing, if we define any constructor explicitly, then the compiler will not provide default constructor and we will have to define it our-self.

In above case if we write Student S; in main(), it will lead to a compile time error, because we haven't defined default constructor, and compiler will not provide its default constructor because we have defined other parametrized constructors.

Destructors

It is a special class function which destroys the object as soon as the scope of object ends. Destructor is called automatically by compiler when object goes out of the scope.

Syntax for destructor is same as constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.

class A
{
public:
~A();
};

Destructors never have any arguments.


class A
{
A()
{
cout << "Constructor called";
}

~A()
{
cout << "Destructor called";
}
};

int main()
{
A obj1; // Constructor Called
int x=1
if(x)
{
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj1

In the example we will use default argument to have a single definition for both default and parametrized constructor.


class Dual
{
int a;
public:
Dual(int x=0)
{
a=x;
}
};

int main()
{
Dual obj1;
Dual obj2(10);
}

Here, in this program, a single Constructor definition will take care for both these object initializations. We don't need separate default and parametrized constructors.



Related Videos