top of page

What are Constructors and Destructors in C++?

Constructors in C++



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


Whereas, Destructor on the other hand is used to destroy the class object.


Let's start with Constructors first, following is the syntax of defining a constructor function in a class:

class A{
    public:
    int x;// constructorA(){// object initialization}};

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


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

class A{
    public:
    int i;A(); // constructor declared};// constructor definition
A::A(){
    i = 1;}


Types of Constructors in C++

Constructors are of three types:

  1. Default Constructor

  2. Parametrized Constructor

  3. Copy COnstructor


Default Constructors

Default constructor is the constructor which doesn't take any argument. It has no parameter.


Syntax:

class_name(parameter1, parameter2, ...){// constructor Definition }

For example:

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

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

Output:

 10 

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

A default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.

class Cube{
    public:
    int side;};

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

output:

 0 or any random value 

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


Parameterized Constructors

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


For example:

class Cube{
    public:
    int side;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 parameterized construcor in above case, we have initialized 3 objects with user defined values. We can have any number of parameters in a constructor.


Copy Constructors

These are special type of Constructors which takes an object as argument, and is used to copy values of data members of one object into other object. We will study copy constructors in detail later.


Destructors in C++

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


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

class A{
    public:
    // defining destructor for class
    ~A()
    {
        // statement
    }
};

Destructors will never have any arguments.



Source: studytonight


The Tech Platform

0 comments

Comments


bottom of page